Liquid: Can I get a random element from an Array?

旧城冷巷雨未停 提交于 2020-01-01 08:02:09

问题


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"

回答1:


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.




回答2:


The 2018 answer is

{% assign prefix = page.prefix | sample: 2 %}
{{ prefix[0] }}

As the OP asked about Jekyll, this can be found at: https://jekyllrb.com/docs/templates/




回答3:


You can create a plugin to get a random element. Something like this:

module Jekyll
  module RandomFilter
    # Use sample to get a random value from an array
    #
    # input - The Array to sample.
    #
    # Examples
    #
    #   random([1, 2, 3, 4, 5])
    #   # => ([2])
    #
    # Returns a randomly-selected item out of an array.
    def random(input)
      input.sample(1)
    end
  end
end

Liquid::Template.register_filter(Jekyll::RandomFilter)

Then do something like this in your template to implement:

{% assign myArray = '1|2|3|4|5 | split: '|' %}
{% assign myNumber = myArray | random %}



回答4:


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:

  • Assign the min to 0 and max to your array's length.
  • Loop over the array till you find your random number and pick you element.

Here is an example, get your random array index:

{% assign min = 0 %}
{% assign max = prefix.size %}
{% assign diff = max | minus: min %}
{% assign randomNumber = "now" | date: "%N" | modulo: diff | plus: min %}

Then find your random value:

{{ prefix[randomNumber] }}



回答5:


You could adapt Liquid::Drop and whitelist Ruby's sample method.

See https://github.com/Shopify/liquid/blob/master/lib/liquid/drop.rb#L69:

You would need to change:

blacklist -= [:sort, :count, :first, :min, :max, :include?]

to:

blacklist -= [:sort, :count, :first, :min, :max, :include?, :sample]

Next you could just use:

{{ some_liquid_array.sample }}   


来源:https://stackoverflow.com/questions/9191764/liquid-can-i-get-a-random-element-from-an-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!