Liquid:我可以从数组中获取随机元素吗?
我正在尝试从数组中选择一个随机元素——使用 Liquid/Jekyll 可以吗?
我可以创建一个数组 - 并访问给定的索引...但是有没有办法“洗牌”数组,然后选择一个索引,从而从数组中获取随机元素?
prefix: ["Foo", "Bar", "Baz"]
---
{{ page.prefix[1] }}
# outputs "Bar"
I'm trying to pick a random element from an array -- is this possible using Liquid/Jekyll?
I can create an array -- and access a given index ... but is there a way to "shuffle" the array and then select an index, and thus get a random element from the array?
prefix: ["Foo", "Bar", "Baz"]
---
{{ page.prefix[1] }}
# outputs "Bar"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
2018 年的答案是
这是一个 Jekyll 特定的过滤器,记录在 https://jekyllrb。 com/docs/liquid/filters/#sample
The 2018 answer is
This is a Jekyll-specific filter which is documented at https://jekyllrb.com/docs/liquid/filters/#sample
Liquid 没有用于从数组或整数区间中选取随机元素的过滤器。
如果您希望 Jekyll 这样做,您必须创建一个扩展来添加该液体过滤器。
但是,我必须指出,这样做会在每次生成页面时选择一个随机元素,但不是每次查看页面时都会选择一个随机元素。
如果您想在每次访问页面时获得不同的随机值,最好的选择是使用 javascript 并让客户端选择一个随机值。不过,您可以使用 Liquid 来生成相关的 javascript。
Liquid doesn't have a filter for picking a random element from an array or an integer interval.
If you want Jekyll to do that, you would have to create an extension to add that liquid filter.
However, I must point out that doing so would pick a random element every time the page is generated, but not every time the page is viewed.
If you want to get different random values every time you visit a page, your best option is using javascript and letting the client pick a random value. You can use liquid to generate the relevant javascript though.
您也许可以在 Liquid 中做到这一点,但它可能不像@Brendan 提供的那样通用解决方案。根据这篇文章,您可以生成一个介于 min 和 to 之间的随机液体数。最大限度。很简单:
这是一个例子,获取你的随机数组索引:
然后找到你的随机值:
You may be able to do that just in Liquid, but it could less of generic solution like the one provided by @Brendan. According to this article, you can generate a random liquid number between min & max. So simply:
Here is an example, get your random array index:
Then find your random value:
您可以创建一个插件来获取随机元素。像这样的事情:
然后在模板中执行类似的事情来实现:
You can create a plugin to get a random element. Something like this:
Then do something like this in your template to implement:
不使用插件(例如,如果您使用 github 页面,这可能是一个要求)并且不希望仅在构建/重建时设置该选择。
它使用集合作为数据源,并在页面前面设置一些功能标志。
我使用 markdownify 来处理内容,使用 jsonify 来使其 JavaScript 安全,然后附加一个逗号来创建我的数组。
然后 JavaScript 在页面加载时随机填充一个。
向 config.yml 添加集合
向页面添加标志
集合内容项 (test.md)
Without using a plugin (which might be a requirement if you are using github pages for example) and don't want the choice to be set only at build/rebuild time.
This uses collections as it's data source and some feature flags set in the page front matter.
I use markdownify to process the content, jsonify to make it JavaScript safe and then append a comma to make my array.
The Javascript then populates one randomly at page load.
Add collection to config.yml
Add flag to page
collection content item (test.md)
您可以调整
Liquid::Drop
和白名单 Ruby 的示例
方法。请参阅https://github.com/Shopify/liquid/ blob/master/lib/liquid/drop.rb#L69:
您需要将:更改
为:
接下来您可以使用:
You could adapt
Liquid::Drop
and whitelist Ruby'ssample
method.See https://github.com/Shopify/liquid/blob/master/lib/liquid/drop.rb#L69:
You would need to change:
to:
Next you could just use: