在液体标签中使用前物质数据
我有一个使用液体用于模板的高度项目。它包括一个图像库,该图像库使用官方插件进行响应式图像,并通过短码使用以下参数生成图像标记:(
{% for p in collections.projects %}
{% for i in p.data.images %}
{% image i, "alt text", "output path", "class list", "sizes" %}
{% endfor %}
{% endfor %}
“ {%image ...”开头的行是实际的快捷代码函数)
以 我“手动”提供参数,只有迭代器变量(i)来自for语句,一切正常。但是,在某些情况下,我需要从Markdown Content File的前遗物获得图像路径,ALT文本或输出路径。在这种情况下,我不想迭代项目P的图像,而只是使用p.data.images [0]在数组中获取第一个。
我首先尝试了最直接的方法,
{% image p.data.images[0], p.data.alts[0] , p.data.outs[0], "class list", "sizes" %}
但这无效,而是给我一个无效的语法错误(指向第一个参数)。
我还尝试在前面的语句中设置变量,例如:
{% assign path = p.data.path %}
{% assign img = p.data.images[0] %}
{% image img, "alt text", path, "class list", "sizes" %}
但这给了我一个错误,指出“ SRC”(短代码中的第一个位置参数)是必需的。而且我知道P.Data.Path和P.Data.Images [0]的数据是因为我可以将其打印为这样的对象:
{{ p.data.path }}
{{ p.data.images[0] }}
那么我在这里做错了什么?我必须有一种方法可以在这些液体标签中访问我的前物数据吗?例如,我为什么不这样做以下操作:
{% for p in collections.projects %}
{% image p.data.images[0], p.data.alts[0], p.data.outs[0], "class list", "sizes" %}
{% endfor %}
I have a project in eleventy using Liquid for templates. It includes an image gallery which uses the official plugin for responsive images, invoked through a shortcode for generating the image markup with the following arguments:
{% for p in collections.projects %}
{% for i in p.data.images %}
{% image i, "alt text", "output path", "class list", "sizes" %}
{% endfor %}
{% endfor %}
(the line beginning with "{% image ..." is the actual shortcode function)
As long as I provide the arguments "manually", with only the iterator variable (i) coming from the for statement, everything works okay. However, there are instances where I need to obtain the image path, alt text or output path from the front-matter of a markdown content file. In this case, I wanted to not iterate through the images of project p, but rather just get the first one in the array using p.data.images[0].
I first tried the most straightforward approach
{% image p.data.images[0], p.data.alts[0] , p.data.outs[0], "class list", "sizes" %}
But that didn't work, and gave me an invalid syntax error instead (pointing at the first argument).
I also tried setting variables in preceding statements, like this:
{% assign path = p.data.path %}
{% assign img = p.data.images[0] %}
{% image img, "alt text", path, "class list", "sizes" %}
But this gives me an error stating that "src" (the first positional argument in the shortcode) is required. And I know the data for p.data.path and p.data.images[0] is there because I can print it just fine as an object like this:
{{ p.data.path }}
{{ p.data.images[0] }}
So what am I doing wrong here? There must be a way I can access my front matter data in these Liquid tags right? Why can I not do the following for example:
{% for p in collections.projects %}
{% image p.data.images[0], p.data.alts[0], p.data.outs[0], "class list", "sizes" %}
{% endfor %}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案:
@person_v1.32在评论中暗示,问题是我集合的所有元素都定义了访问的字段。因此,我试图做的是通过添加围绕快速代码的if语句来解决的,如下所示:
Solution:
As hinted in comment by @person_v1.32, the issue was that not all elements of my collection had defined the accessed fields. So what I was attempting to do was solved by adding an if statement surrounding the shortcode, as follows: