Search code examples
twig

Using twig variable to dynamically call an imported macro sub-function


I am attempting if use a variable to call a specific macro name.

I have a macros file that is being imported

{% import 'form-elements.html.twig' as forms %}

Now in that file there are all the form element macros: text, textarea, select, radio etc.

I have an array variable that gets passed in that has an elements in it:

$elements = array(
    array(
        'type'=>'text,
        'value'=>'some value',
        'atts'=>null,
    ),
    array(
        'type'=>'text,
        'value'=>'some other value',
        'atts'=>null,
    ),
);

{{ elements }}

what im trying to do is generate those elements from the macros. they work just fine when called by name:

{{ forms.text(element.0.name,element.0.value,element.0.atts) }}

However what i want to do is something like this:

{% for element in elements %}
{{ forms[element.type](element.name,element.value,element.atts) }}
{% endfor %}

I have tried the following all resulting in the same error:

{{ forms["'"..element.type.."'"](element.name,element.value,element.atts) }}
{{ forms.(element.type)(element.name,element.value,element.atts) }}
{{ forms.{element.type}(element.name,element.value,element.atts) }}

This unfortunately throws the following error:

 Fatal error: Uncaught exception 'LogicException' with message 'Attribute "value" does not exist for Node "Twig_Node_Expression_GetAttr".' in Twig\Environment.php on line 541

Any help or advice on a solution or a better schema to use would be very helpful.


Solution

  • I just thought other people may want the answer to this, as provide by fabpot:

    This is indeed something that is not supported: calling a macro with a dynamic name (I have added a proper exception to be clearer about the issue).

    If you really want to do that, you can do so with the following code:

    {{ attribute(forms, element.type, [element.name,element.value,element.atts]) }}

    -fabpot

    https://github.com/twigphp/Twig/issues/922#issuecomment-11133299