液体模板地图过滤器 [英] Liquid Template Map Filter
问题描述
如何使用Liquid中的地图过滤器?我在Jekyll使用它。
---
my_array:[apple,banana,orage]
my_map:
hello:world
foo:bar
my_string:这是如何工作的?
---
{{page.my_map | map ...}}
这就是我迷失的地方。我似乎无法找到任何在文档或其他地方在线使用它的例子。
顺便说一下,我不知道Ruby,但是,所以源代码对我而言并不清楚,要么。
从过滤器测试看起来像下面这样应该会产生一些东西,但是在GitHub上,我什么也没有得到:
{{site.posts | map:'title'| array_to_sentence_string}}
我希望我能得到类似于:
我的第一篇博文,还有另一篇文章和第三篇文章
<$ c $ c> {{site.posts | map:'to_liquid'| map:'title'| array_to_sentence_string}}
解释:
I认为 site.posts
正在返回一个 Post 实例,而不是返回它们的 to_liquid
版本(这感觉很奇怪 - 可能它是一个错误,你应该报告它) 。该类的实例不 respond_to? :title
,并且由于Liquid代码检查它,所以 map
返回所有实例的零。
How, exactly does one use the map filter in Liquid? I'm using it in Jekyll.
---
my_array: [apple, banana, orage]
my_map:
hello: world
foo: bar
my_string: "how does this work?"
---
{{ page.my_map | map ... }}
That's about where I get lost. I cannot seem to find any example of its usage in the docs or anywhere else online for that matter.
By the way, I do not know Ruby, yet, so the source code is not clear to me, either.
From the filter tests it looks like the following should produce something, but on GitHub, I am getting nothing:
{{ site.posts | map: 'title' | array_to_sentence_string }}
I would expect that I should get something like:
My First Blog Post, Yet Another Post, and Third Posts
I was able to do what you want this way:
{{ site.posts | map: 'to_liquid' | map: 'title' | array_to_sentence_string }}
Explanation:
I think that site.posts
is returning an array of Post instances instead of returning their to_liquid
version (which feels very weird - probably it is a bug, you should report it). Instances of that class don't respond_to? :title
, and since the Liquid code checks for that, the map
returns nil for all instances.
这篇关于液体模板地图过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!