如何连接/追加一个字符串到另一个在 Jekyll/肃清?

澄清一下,假设:

{% assign my_var = "123" %}
{% assign another_var = "456" %}

我想给 my_var添加字符串以得到类似于 123 - 456的内容

到目前为止我所做的努力:

{% assign my_var = my_var + " - " + another_var %}
47413 次浏览

You could use the capture logic tag:

{% capture new_var %}\{\{ my_var }} - \{\{ another_var }}{% endcapture %}

It is also possible to use the append filter, as Ciro pointed:

{% assign new_var = my_var | append: ' - ' | append: another_var %}

append: filter

This is more convenient than capture for short concatenations:

{% assign x = 'abc' %}
{% assign y = 'def' %}
{% assign z = x | append: ' - ' | append: y %}
\{\{ z }}

Output:

abc - def

Tested on jekyll 3.0.4 (github-pages 75).

All the answers so far are correct, but they fail to mention that you can also inline the append instead of having to assign a new variable:

<a href="\{\{ foo | append: ' - ' | append: bar }}">Link</a>