在Twig中将文本追加到变量

Cry*_*org 2 variables join concatenation append twig

我正在尝试使用〜(波浪号)运算符在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.....
#}
Run Code Online (Sandbox Code Playgroud)

以上都不是。

我还有其他条件,并且每次都需要添加一些文本。像这样工作的东西:

{% set class = 'original_text ' %}

{% class ~= 'first_append ' %}
{% class ~= 'second_append ' %}
{% class ~= 'third_append ' %}
Run Code Online (Sandbox Code Playgroud)

结果为

{{ class }}
Run Code Online (Sandbox Code Playgroud)

将会 :

original_text first_append second_append third_append
Run Code Online (Sandbox Code Playgroud)

关于如何执行此操作的任何想法?

谢谢 !

编辑:原来是CSS错误,连接进行得很好。

Nis*_*sam 5

您可以使用set标记将字符串与变量连接在一起。从您的示例中,我们可以重写这些行,

{% set class = 'original_text' %}
{% set class = class ~ ' some_other_text'%}
Run Code Online (Sandbox Code Playgroud)

我们可以通过打印这样的新变量来显示,

{{class}} 
Run Code Online (Sandbox Code Playgroud)

它将显示这样的输出,original_text some_other_text