【发布时间】:2014-03-17 21:50:07
【问题描述】:
我正在尝试使用 ~(波浪号)运算符连接 Twig 中的字符串。这是我的情况,我尝试了不同的方法:
{% set class = 'original_text' %}
{# First try : the most obvious for me, as a PHP dev #}
{% class ~= 'some_other_text' %}
{# Second try #}
{% class = class ~ 'some_other_text' %}
{# Third try #}
{% class = [class, 'some_other_text'] | join(' ') %}
{# Fourth try : the existing variable is replaced #}
{% set class = [class, 'some_other_text'] | join(' ') %}
{#
Then do another bunch of concatenations.....
#}
以上都不起作用。
我还有一些条件,每次都需要添加一些文字。像这样工作的东西:
{% set class = 'original_text ' %}
{% class ~= 'first_append ' %}
{% class ~= 'second_append ' %}
{% class ~= 'third_append ' %}
结果
{{ class }}
应该是:
original_text first_append second_append third_append
你知道怎么做吗?
谢谢!
编辑:原来是 CSS 错误,连接进行得很顺利....
【问题讨论】:
标签: variables join append twig concatenation