传递参数以包含在Liquid模板中 [英] Passing parameters to inclusion in Liquid templates

查看:268
本文介绍了传递参数以包含在Liquid模板中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以Jekyll为动力的网站上,我有一个包含功能类似的物件,但我不知道如何正确传递参数.

当我使用{% include ... %}时,像这样传递参数.

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

..它只是传递文字字符串{{ baz.quux }},这当然不是我想要的!我想传递baz.quux的值.我该怎么做?

谢谢!

解决方案

有两种方法可以实现此目的.我已经针对Jekyll的github页版本测试了这两种方法.

使用捕获

假设您在包含代码中引用了{{ foo }},则需要在调用包含之前为foo分配一个值.

例如:

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

使用要包含的参数

这使您可以控制变量的范围,看起来像您想要的那样.在模板文档中有一些详细的设置方法.. >

您在语法上几乎是正确的,在您将要使用的模板中:

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

缺少的部分是变量需要在包含文件的代码中以不同的方式引用,您需要使用{{ include.foo }}

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!

解决方案

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

这篇关于传递参数以包含在Liquid模板中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆