69
votes

To be clear, assuming:

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

I would like to append string to my_var to get something like 123 - 456

What I have tried so far:

{% assign my_var = my_var + " - " + another_var %}
2

2 Answers

130
votes

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 %}
47
votes

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).