Skip to main content

TIL: Use the {% capture %} tag to assign complex strings to variables

If you want to get a string that’s semi-complicated to construct, you can put a “mini-template” in the {% capture %} tag to build it over multiple lines.

You can use the capture tag to create a new variable:

{% capture variable %}
  value
{% endcapture %}

This could be quite useful for building up complex variables with several conditionals or loops, inside the template.

I first saw this variable being used in an article by Jesse Squires, and although I don’t have a use for it right now, it’s a useful one to remember.