I am trying to use a variable to call a specific macro name.
I have a macro file that is imported
{% import 'form-elements.html.twig' as forms %}
Now in this file there are all macros of form elements: text, text field, selection, radio, etc.
I have an array variable that is passed in that contains elements in it:
$elements = array( array( 'type'=>'text, 'value'=>'some value', 'atts'=>null, ), array( 'type'=>'text, 'value'=>'some other value', 'atts'=>null, ), ); {{ elements }}
what I'm trying to do is generate these elements from macros. they work 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 tried the following, which led to 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 causes 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 scheme to use would be very helpful.