Ad

How To Interpolate In Liquid Shopify

I am editing title tags that was written by another developer in Shopify Liquid, unfortunately i am so confused about the syntax. I am trying to do ruby style interpolation in one of the edits. for example,

{% assign title_content = teacher.name %}
{% include "layout/page_title", title: title_content %}

in pure ruby, it would be something like this

{% assign title_content = "the name of the teacher is #{teacher.name}" %}

which will out give the output "the name of the teacher is bla bla". I would like to know if can do something like this with liquid shopify.

Ad

Answer

You can use the capture tag to build your variable as follows:

{% capture title_content %}
the name of the teacher is {{ teacher.name }}
{% endcapture %}

Alternatively,e>{% capture title_content %} the name of the teacher is {{ teacher.name }} {% endcapture %}

Alternatively, you should be able to use the append filter during assignment:

{% assign title_content = "the name of the teacher is " | append: teacher.name %}

In any case, Liquid is supposed to be a "safe" templating language. As such, it will be less expressive then e.g. ERB which allows you to use arbitrary Ruby code. This however ensures that normal users can enter and update the templates without risking arbitrary code execution on the server.

Ad
source: stackoverflow.com
Ad