19

What does {% capture var %} do in Jekyll?

Can I for example, in a .md file do:

{% capture head %}
I am the head
{% endcapture %}

and then in the .html file do:

{{head}}

Am I using capture correctly?

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
biw
  • 3,000
  • 4
  • 23
  • 40

1 Answers1

22

capture lets you assign text to a variable name. Later on when referencing that variable you can output that text.

In your above example head is the variable name. So you're saying, place all of the text between the opening and closing capture tags in a variable named head.

Then later in {{head}} you're saying you'd like to dump the contents of that variable onto the page. There is nothing special about the name head and you could rename it to something else entirely.

You can find more information about capture on this Liquid for Designers page

Zach Dennis
  • 1,754
  • 15
  • 19
  • 2
    The [Shopify documentation for Liquid](https://docs.shopify.com/themes/liquid-documentation/tags/variable-tags#capture) is also an excellent resource. In my opinion it is organized better than the Liquid for Designers page and usually has better descriptions, examples, and sample output. – Erik Gillespie Jun 04 '15 at 14:47