在树枝中连接字符串和变量

concat string and variable in twig

我的模板中有这个

    {{ ad.title_de }}

现在我要合并多种语言,所以 'title_de' 必须改变 我还有一个变量 'tld' 是 de, en , fr

之一

所以我想要

    {% if tld == 'fr' %} 
    {{ ad.title_fr }} 

等 有什么想法吗?

试试这个:

{{ ad["title_" ~ tld] }}

尝试使用 attribute 函数。

http://twig.sensiolabs.org/doc/functions/attribute.html

attribute(ad, 'content_'~tld) 应该可以。