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

后端 未结 6 927
甜味超标
甜味超标 2021-02-13 10:49

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 \

6条回答
  •  北海茫月
    2021-02-13 11:28

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

提交回复
热议问题