Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to concatenate / append a string to another one in Jekyll / Liquid?

Tags:

jekyll

liquid

People also ask

How do I combine two string values?

You concatenate strings by using the + operator. For string literals and string constants, concatenation occurs at compile time; no run-time concatenation occurs. For string variables, concatenation occurs only at run time.


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>