Using twig variable to dynamically call an importe

2020-08-15 11:01发布

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.

标签: twig
2条回答
冷血范
2楼-- · 2020-08-15 11:04

Dynamic macros may not be supported in Twig.

But there is a simple workaround since you can dynamically include other templates.

Example:
Let's say you have a bunch of content modules or content blocks (or however you wanna call them) for your site. And you have Twig macros responsible of rendering each of these modules.

{# modules.twig #}

{% macro module1(config) %}
  <div>module one</div>
{% endmacro %}

{% macro module2(config) %}
  <div>module two</div>
{% endmacro %}

{% macro module3(config) %}
  <div>module three</div>
{% endmacro %}

Now, what you need to dynamically call these macros is to add an extra template for each, like so:

{# module1.twig #}

{% import "modules.twig" as modules %}
{{ modules.module1(config) }}
{# module2.twig #}

{% import "modules.twig" as modules %}
{{ modules.module2(config) }}
{# module3.twig #}

{% import "modules.twig" as modules %}
{{ modules.module3(config) }}

Finally, in your actual page template you just include the template instead of calling the macro.

{# template.twig #}

{# this is the macro's name to be called #}
{% set macro = 'module2' %}
{# this is just a config object to be passed to the macro #}
{% set config = {} %}

{% include macro ~ '.twig' with { config: config } only %}

Et voilá, (dynamically produced) output will be <div>module two</div>.

查看更多
地球回转人心会变
3楼-- · 2020-08-15 11:21

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

查看更多
登录 后发表回答