Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing twig variable to dynamically call an imported macro sub-function
    text
    copied!<p>I am attempting if use a variable to call a specific macro name.</p> <p>I have a macros file that is being imported</p> <pre><code>{% import 'form-elements.html.twig' as forms %} </code></pre> <p>Now in that file there are all the form element macros: text, textarea, select, radio etc.</p> <p>I have an array variable that gets passed in that has an elements in it:</p> <pre><code>$elements = array( array( 'type'=&gt;'text, 'value'=&gt;'some value', 'atts'=&gt;null, ), array( 'type'=&gt;'text, 'value'=&gt;'some other value', 'atts'=&gt;null, ), ); {{ elements }} </code></pre> <p>what im trying to do is generate those elements from the macros. they work just fine when called by name:</p> <pre><code>{{ forms.text(element.0.name,element.0.value,element.0.atts) }} </code></pre> <p>However what i want to do is something like this:</p> <pre><code>{% for element in elements %} {{ forms[element.type](element.name,element.value,element.atts) }} {% endfor %} </code></pre> <p>I have tried the following all resulting in the same error:</p> <pre><code>{{ 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) }} </code></pre> <p>This unfortunately throws the following error: </p> <pre><code> 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 </code></pre> <p>Any help or advice on a solution or a better schema to use would be very helpful.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload