[BUG] Custom Volt functions not working inside macros
It happens when the custom function is called through $this (the Volt instance).
For example, I set up this function:
$this->_compiler->addFunction('t', function($resolvedArgs, $exprArgs) {
return '$this->getDI()->get(\'application\')->translate(' . $resolvedArgs . ')';
});
It works fine as long as it's not used inside a macro, for example:
{%- macro textfld(field, title) %}
<label>{{ t(title) }}</label>
{{ text_field(field }}
{%- endmacro %}
Calling the t() function will produce this error: Fatal error: Using $this when not in object context
It happens because the macros are compiled into PHP functions and calling them loses the object context. This could be resolved by passing the Volt instance (for example, as $volt) as a parameter to the compiled macro function and making the Volt instance available via $volt in the general template scope as well. Then in the custom functions we could use $volt instead of $this.
My current workaround is to access the DI via $GLOBALS['di'] in the compiler function, but this feels so wrong.
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
Apparently also some "built-in" functions do not work inside macros, including url().
This template code inside a macro:
{{ url(action) }}
compiles into this:
<?php echo $this->url->get($action); ?>
and produces the same Fatal error as posted above.
Here's how I fixed it myself by extending the compiler. It now uses the $volt variable, which is available in the main template scope, as well as the macros. Would definitely appreciate a proper fix though.
class LiveVolt extends \Phalcon\Mvc\View\Engine\Volt
{
public function getCompiler()
{
if (empty($this->_compiler))
{
$GLOBALS['volt'] = $this;
$this->_compiler = new LiveVoltCompiler($this->getView());
$this->_compiler->setOptions($this->getOptions());
$this->_compiler->setDI($this->getDI());
}
return $this->_compiler;
}
}
class LiveVoltCompiler extends \Phalcon\Mvc\View\Engine\Volt\Compiler
{
protected function _compileSource($source, $something = null)
{
$variable = '<' . '?php $volt = $GLOBALS[\'volt\']; ?' . '>';
$compiled = parent::_compileSource($source, $something);
if (is_array($compiled))
{
$compiled[0] = $variable . $compiled[0];
foreach ($compiled as &$entry)
{
$entry = $this->replaceThis($entry);
}
}
else
{
$compiled = $this->replaceThis($variable . $compiled);
}
return $compiled;
}
protected function replaceThis ($source)
{
if (is_array($source))
{
return $source;
}
$source = str_replace('$this', '$volt', $source);
$source = preg_replace('/function vmacro_([^\)]+)\)[\s]+\{/', 'function vmacro_$1) { global $volt; ', $source);
return $source;
}
}
What can you see this?
$this->_compiler->addFunction('t', function($resolvedArgs, $exprArgs) use ($this) {
return $this->getDI()->get('application')->translate($resolvedArgs);
});
I see this PHP error: Fatal error: Cannot use $this as lexical variable
I can rewrite it this way and it "does not fail":
$that = $this;
$this->_compiler->addFunction('t', function($resolvedArgs, $exprArgs) use ($that) {
return $that->getDI()->get('application')->translate($resolvedArgs);
});
However it's not the same thing, as this function is then called only once at compilation time and hardcodes in the compiled template whatever result the translate function returned - as opposed to calling it every time the template is rendered.
I test it's OK.
$app = $this->getDI()->get('application');
$this->_compiler->addFunction('t', function($resolvedArgs, $exprArgs) use ($app) {
return $app->translate($resolvedArgs);
});
Well, it "works", but it does not produce the right result. The result of this function would be a string in the compiled template code (PHP), however it has to be a function call of the translate() method from template every time it's rendered.
Sorry, My English is not very good, so not quite understand what you mean. Please can you upload the code to reproduce this problem what?
This bug also happens when trying to use:
{{ myString|length }}
inside a macro
Hi Guys,
I'd like to add my solution to this.
I made the macro accept a "me" parameter, and then passed "this" into it.
Example:
{%- macro output_name(team, me) %}
{% return me.tag.linkto("club/view/" ~ team.Club.Id, "View club details") %}
{%- endmacro %}
And call with:
{{ output_name(fixture.Team, this) }}
Agreed, would be nice to have a proper solution, but hope this helps in the meantime.
Thanks, Rich
+1
Every parameter you pass on to the view is not available inside the macro's scope. Which also pushed me to simquad's solution although it's not very pretty
Can and will this be fixed in 2.0?
This is fixed in Phalcon 2.0.4