Passing parameters to inclusion in Liquid templates

前端 未结 4 723
抹茶落季
抹茶落季 2021-01-30 12:49

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 {% incl

相关标签:
4条回答
  • 2021-01-30 12:58

    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.

    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 }}

    0 讨论(0)
  • 2021-01-30 13:05

    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>
    
    0 讨论(0)
  • 2021-01-30 13:18

    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

    0 讨论(0)
  • 2021-01-30 13:22

    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.

    0 讨论(0)
提交回复
热议问题