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

Posted by Chausser on Stack Overflow See other posts from Stack Overflow or by Chausser
Published on 2012-12-04T21:37:40Z Indexed on 2012/12/10 23:05 UTC
Read the original article Hit count: 230

Filed under:

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.

© Stack Overflow or respective owner

Related posts about twig

  • Another Twig Improvements

    as seen on Oracle Blogs - Search for 'Oracle Blogs'
    Hi all! We are here again to intorduce you some of our new NetBeans 7.3 features. Today we'll show you some another Twig improvements. So let's start! Code Templates First feature is about Code Templates. We added some basic templates to improve your Editor experience. You will be really… >>> More

  • Symfony2 Include in html in Twig

    as seen on Stack Overflow - Search for 'Stack Overflow'
    I am developing an application usin Symfony2 and twig for templates. I am using a 3 level structure for templates. Base.html.twig, layout.html.twig and childtemplate.html.twig. The problem is I am trying to include one example.html (common html file) in the next child template by using include but… >>> More

  • Twig Code Completion

    as seen on Oracle Blogs - Search for 'Oracle Blogs'
    Hi all! After few weeks we have a new feature for you which will be available in upcoming NetBeans 7.3. It's about new code completion in Twig files! So let us introduce it a bit. Now we hopefully support all of Twig built-in elements. It means Tags, Filters, Functions, Tests and Operators… >>> More

  • Twig Template For loop Results

    as seen on Stack Overflow - Search for 'Stack Overflow'
    I am trying to print out a the contents of an array but am not getting the expected results. any help is much appreciated: PHP code: $list[1]['first_name'] = 'Joe'; $list[1]['last_name'] = 'Smith'; $list[2]['first_name'] = 'John'; $list[2]['last_name'] = 'brand'; $data = array( 'customer'… >>> More

  • Twig templates, inheritances and block usage

    as seen on Stack Overflow - Search for 'Stack Overflow'
    I've created three templates using Twig. The first one has block A defined in it, the second one extends from the first one, but includes a third template which sets the content of block A. When loading, through the browser, the url which renders b.html.twig, the content in block A (defined by the… >>> More