简体   繁体   English

如何在Twig中制作宏?

[英]How to make a macro in Twig?

I use a big piece of code for displaying friends and I use it in several templates, so I wanted to make it a macro, but the syntax is a way unusal and I don't know if there is a way this to be done. 我使用了一大段代码来显示朋友,并且在几个模板中使用了它,所以我想将其设为宏,但是语法是一种不常用的方式,我不知道是否有这样做的方法。

The part of the code, which I want to seperate is: 我要分开的代码部分是:

       {% if(fr.email != null) %}
            <p>
                <span class ="label">Email Address: </span> 
                <a  class="email" href="{{ path('friend_email', {'id': fr.id}) }}">
                    {{ fr.email }}
                </a>
            </p>
        {% endif %}

        {% if(fr.phone != null) %}
            <p>
                <span class="label">Phone: </span>
        {{ fr.phone }}
            </p>
        {% endif %}

and so on for about 10 variables. 依此类推,大约有10个变量。 In another template I use this, but instead of fr.email, fr.phone and so on, I need friend.email, friend.phone... 在另一个模板中,我使用了此模板,但是我需要使用friend.email,friend.phone而不是fr.email,fr.phone等。

I tried this but without success: 我尝试了这个但没有成功:

{% macro display_friend(fr) %}

    {% if({{ fr }}.email != null) %}
            <p>
                <span class ="label">Email Address: </span> 
                <a  class="email" href="{{ path('friend_email', {'id': {{ fr }}.id}) }}">
                    {{ {{ fr }}.email }}
                </a>
            </p>
        {% endif %}

        {% if({{ fr }}.phone != null) %}
            <p>
                <span class="label">Phone: </span>
        {{ {{ fr }}.phone }}
            </p>
        {% endif %}
{% endmacro %}

If necessary, I can use fr.email, fr.phone, fr.* ... in each template, so maybe inheritance will work? 如有必要,我可以在每个模板中使用fr.email,fr.phone,fr。* ...,那么继承是否可以工作?

So my question is: is there a way to make this part of code macro and if yes will it be better or inheritance will be better? 所以我的问题是:有没有办法使代码的这一部分成为宏,如果可以,它会更好还是继承会更好?

In your situation, rather than a using a macro I would just include a twig file, using the with option. 根据您的情况,而不是使用宏,我只会使用with选项来包含一个树枝文件。 For example, you can do: 例如,您可以执行以下操作:

{% include 'AcmeDemoBundle:Demo:showFriend.html.twig' with {'fr': friend} %}

and showFriend.html.twig will be: 而showFriend.html.twig将是:

   {% if(fr.email != null) %}
        <p>
            <span class ="label">Email Address: </span> 
            <a  class="email" href="{{ path('friend_email', {'id': fr.id}) }}">
                {{ fr.email }}
            </a>
        </p>
    {% endif %}

    {% if(fr.phone != null) %}
        <p>
            <span class="label">Phone: </span>
    {{ fr.phone }}
        </p>
    {% endif %}

Within the macro you can just use fr.var so 宏中,您可以仅使用fr.var这样

{% if({{ fr }}.email != null) %}

will become 会变成

{% if fr.email %}

As you can see, specifying != null is not required (I even doubt if it'll work, it probably should be fr.email not is null ) 如您所见,不需要指定!= null (我什至怀疑它是否可以工作,它可能应该是fr.email not is null )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM