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?
Capture allows you to write complex logic and strings and then capture the output of a block of liquid to a variable. This means you can write functions for your liquid in one place and then use the variable in much simplified liquid.
What is Liquid? Liquid is a templating language used in Jekyll to process your site's pages. In other words, it helps you make your HTML pages a bit more dynamic, for example adding logic or using content from elsewhere. This doesn't require any setup - we can just start using it.
Front matter is a snippet of YAML placed between two triple-dashed lines at the start of a file. You can use front matter to set variables for the page: --- my_number: 5 --- You can call front matter variables in Liquid using the page variable.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With