Passing parameters to inclusion in Liquid templates

JekyllLiquid

Jekyll Problem Overview


On my Jekyll-powered website I have an inclusion that does something function-like, but I can't figure out how to pass it parameters correctly.

When I use {% include ... %}, passing parameters like this..

{% include function.liquid foo="{{ baz.quux }}" %}

..it just passes the literal string {{ baz.quux }} which is, of course, not what I wanted! I want to pass the value of baz.quux. How do I do this?

Thanks!

Jekyll Solutions


Solution 1 - Jekyll

There are two ways to achieve this. I have tested both approaches against the github-pages version of Jekyll.

Using Capture

Assuming you are referencing {{ foo }} in the code of your include, you need to assign a value to foo before calling the include.

Such as:

{% capture foo %}{{ baz.quux }}{% endcapture %}
{% include function.liquid %}

Using parameters to Include

This allows you to control the scope of the variable, which it looks like you want. There is some detail of how to set this up in [the templates documentation][1].

You were nearly right with the syntax, in the template you would use:

{% include function.liquid foo=baz.quux %}

The part that was missing is that the variable needs to be referenced differently in the code of the include file, you need to use {{ include.foo }}

In Shopify Liquid

As of 2021, you can do the following:

{% include 'snippet-file' with bar, foo: baz, foo2: baz2 %}

and in the snippet file, retrieve bar with {{ snippet-file }} and baz with {{ foo }}.

For Liquid statements, just use the variable name without {{ }}, example:

{% if snippet-file==0 and foo2==':)' %}

Note that it won't work as include.foo2 like it was shown in the previous section.

Also note that include is deprecated, and for new development you should use render in a similar fashion.

[1]: http://jekyllrb.com/docs/templates/ "Templates "

Solution 2 - Jekyll

Alongside David Hutchison's, there is a third solution:

Using assign

{% assign foo = baz.quux %}
{% include function.liquid %}

Now you can reference {{ foo }} in your included file.

Solution 3 - Jekyll

Enter the variable without quotes or parentheses, like that:

{% include footer.html param="value" variable-param=page.variable %}

In your case:

{% include function.liquid foo=baz.quux %}

It works in my site.

From: https://github.com/jekyll/jekyll/issues/3577#issue-61988857

Solution 4 - Jekyll

As September 1rst of 2020, it did worked this way:

jekyll v4.1.1

{% assign title = 'this is my tittle' %}
{% include subscribeBtn.html title = title %}

Then in the Template:

<h3>{{ title }}</h3>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNickView Question on Stackoverflow
Solution 1 - JekyllDavid HutchisonView Answer on Stackoverflow
Solution 2 - JekyllluvejoView Answer on Stackoverflow
Solution 3 - JekyllOskar ŚwieradView Answer on Stackoverflow
Solution 4 - JekyllDespertawebView Answer on Stackoverflow