0

I have a custom function that's being added to the Twig object like so:

...
    add_filter('get_twig', array(get_class(),'add_twig_functions'));
...

public static function add_twig_functions($twig)
{
    $twig->addFunction(new Twig_SimpleFunction('get_table', array(get_class(), 'get_table')));

    return $twig;
}

...

public static function get_table($cycle, $visible = true, $id = null)
{
    ....
}

Inside a twig template, I am trying to call it like so:

{{ get_table('mth', user_data.cycle = 'Monthly', 'table-mth') }}

However I'm getting an error:

PHP Fatal error: Uncaught exception 'Twig_Error_Syntax' with message 'A parameter name must be a string, "Twig_Node_Expression_GetAttr" given

My intended result is that the $visible parameter is passed as true if the user_data.cycle context variable is equal to 'Monthly', but clearly I'm not doing it correctly.

I also tried changing my call to:

{{ get_table('mth', (user_data.cycle = 'Monthly'), 'table-mth') }}

However this just gives me:

PHP Fatal error: Uncaught exception 'Twig_Error_Syntax' with message 'An opened parenthesis is not properly closed. Unexpected token "operator" of value "=" ("punctuation" expected with value ")")

Is there an expected way to pass a boolean expression to a twig function? Timber is at version 1.1.6, for what it's worth. All the documentation I've been able to find so far on extending with functions is at http://twig.sensiolabs.org/doc/advanced.html, but it doesn't really cover parameters.

1 Answer 1

0

Well this is just me misremembering how comparisons work in twig... I had the impression that = in Twig was the equivalent of PHP's ==, and == in twig was the equivalent of PHP's ===, but I don't know where I got that impression because it's wrong.

{{ get_table('mth', (user_data.cycle == 'Monthly'), 'table-mth') }}

works fine and as expected. There goes a pointless hour of troubleshooting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.