cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

[BUG] Custom Volt functions not working inside macros

Open integry opened this issue 13 years ago • 13 comments

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.

integry avatar Aug 30 '13 13:08 integry

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.

integry avatar Sep 01 '13 12:09 integry

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;
    }
}

integry avatar Sep 01 '13 13:09 integry

What can you see this?

$this->_compiler->addFunction('t', function($resolvedArgs, $exprArgs) use ($this) {
    return $this->getDI()->get('application')->translate($resolvedArgs);
});

dreamsxin avatar Sep 03 '13 09:09 dreamsxin

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.

integry avatar Sep 03 '13 11:09 integry

I test it's OK.

$app = $this->getDI()->get('application');
$this->_compiler->addFunction('t', function($resolvedArgs, $exprArgs) use ($app) {
    return $app->translate($resolvedArgs);
});

dreamsxin avatar Sep 04 '13 00:09 dreamsxin

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.

integry avatar Sep 04 '13 04:09 integry

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?

dreamsxin avatar Sep 04 '13 08:09 dreamsxin

This bug also happens when trying to use:

{{ myString|length }}

inside a macro

quasipickle avatar Nov 15 '13 21:11 quasipickle

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

simquad avatar Jan 23 '14 21:01 simquad

+1

michanismus avatar Aug 07 '14 13:08 michanismus

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

lucvht avatar Oct 07 '14 12:10 lucvht

Can and will this be fixed in 2.0?

nurtext avatar Mar 18 '15 11:03 nurtext

This is fixed in Phalcon 2.0.4

andresgutierrez avatar Jun 25 '15 03:06 andresgutierrez