Skip to main content

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

  • Tagged with liquid
  • Posted

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][squires], and although I don't have a use for it right now, it's a useful one to remember.

[capture tag]: https://shopify.dev/docs/api/liquid/tags/capture
[squires]: https://www.jessesquires.com/blog/2021/06/06/rss-feeds-jekyll-and-absolute-versus-relative-urls/