Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jekyll, Liquid, Random numbers

I would like to have a set of links

<li>
  <h2>Random Articles</h2>  
  <ul>
    <li><a href="#">Old article 1</a></li>
    <li><a href="#">Old article 1</a></li>
    <li><a href="#">Old article 1</a></li>
  </ul>
</li>

But I want to generate the links from a random selection of my posts. I'm using jekyll and liquid to generate the site. I must use built in parts of jekyll as I'm hosting on github. I'm not really sure where to start on this. Google searches on the topic are fruitless.

like image 993
randomlogic78 Avatar asked Sep 20 '11 16:09

randomlogic78


2 Answers

This is choosing a random quote from a JSON file in _data but the principle should work with your posts as well:

{% assign random = site.time | date: "%s%N" | modulo: site.data.inspirational-quotes.size %}

<blockquote>&ldquo;{{ site.data.inspirational-quotes[random].quote }}&rdquo; <cite>{{ site.data.inspirational-quotes[random].person }}</cite></blockquote>
like image 197
knorthfield Avatar answered Sep 19 '22 04:09

knorthfield


I found this article to be useful to generating numbers using Liquid, it is not straight forward, nonetheless, it is the most elegant way to generate a random number with min/max.

The following example is for a number between 65 & 80.

{% assign min = 65 %}
{% assign max = 80 %}
{% assign diff = max | minus: min %}
{% assign randomNumber = "now" | date: "%N" | modulo: diff | plus: min %}
like image 28
user2517028 Avatar answered Sep 19 '22 04:09

user2517028