diff --git a/lib/jekyll/filters.rb b/lib/jekyll/filters.rb index f3193f21..70e93b90 100644 --- a/lib/jekyll/filters.rb +++ b/lib/jekyll/filters.rb @@ -281,9 +281,9 @@ module Jekyll new_ary end - def sample(input) + def sample(input, num = 1) return input unless input.respond_to?(:sample) - input.sample(1) + input.sample(num) end # Convert an object into its String representation for debugging diff --git a/site/_docs/templates.md b/site/_docs/templates.md index 20a3a3f2..fd42b6b1 100644 --- a/site/_docs/templates.md +++ b/site/_docs/templates.md @@ -249,12 +249,15 @@ common tasks easier.

Sample

-

Pick a random value from an array.

+

Pick a random value from an array. Optional: pick multiple values.

{% raw %}{{ site.pages | sample }}{% endraw %}

+

+ {% raw %}{{ site.pages | sample:2 }}{% endraw %} +

diff --git a/test/test_filters.rb b/test/test_filters.rb index 59b882f1..5fd9facb 100644 --- a/test/test_filters.rb +++ b/test/test_filters.rb @@ -393,6 +393,13 @@ class TestFilters < JekyllUnitTest input = %w(hey there bernie) assert_includes input, @filter.sample(input) end + + should "allow sampling of multiple values (n > 1)" do + input = %w(hey there bernie) + @filter.sample(input, 2).each do |val| + assert_includes val, input + end + end end end