Skip to content

Instantly share code, notes, and snippets.

@pdokas
Last active December 19, 2015 15:48
Show Gist options
  • Save pdokas/5978746 to your computer and use it in GitHub Desktop.
Save pdokas/5978746 to your computer and use it in GitHub Desktop.
The simplest way I can find to find a sorted list of all tags across all posts in Siteleaf.
{% capture tags %}
{% for post in site.posts %}
{% for tag in post.taxonomy.tags %}
{{tag.value}}\{{tag.url}}{% unless forloop.last %},{% endunless %}
{% endfor %}
{% endfor %}
{% endcapture %}
{% assign tags = tags | strip_newlines | split:',' | sort %}
<ul>
{% for tag in tags %}
{% assign tag_parts = tag | split:'\' %}
{% unless last_tag == tag_parts.first %}
<li><a href="{{tag_parts.last}}">{{tag_parts.first}}</a></li>
{% endunless %}
{% assign last_tag = tag_parts.first %}
{% endfor %}
</ul>
@pdokas
Copy link
Author

pdokas commented Jul 12, 2013

Very cool, this works perfectly, thanks for the speedy work on this too!

While this is perfect, I have a few questions to better my understanding.

  1. In the line site.pages['blog'].taxonomy['tags'].values, what is the values member? It’s not mentioned in siteleaf-themes. Syntactically it’s a bit confusing to me because it looks like we’re not iterating over the tags array itself, but some child of it.
  2. Passing a key to sort is awesome. It’s a shame it’s not in the Liquid For Designers docs.
  3. I’ve found it’s perhaps impossible to iterate over a filtered array in a for loop. For instance, I often run into trouble with a line like {% for tag in site.pages.blog.taxonomy.tags.values | sort:'value' %}, even when wrapping the in clause in ( ). Is this syntactically invalid or is there some subtlety I’m overlooking?

@pdokas
Copy link
Author

pdokas commented Jul 12, 2013

Also,

diff

💖 💖 💖

@sskylar
Copy link

sskylar commented Jul 12, 2013

  1. In Siteleaf you can call your tag set anything you want (default is 'tags', but can be anything). values is the generic name for the array of 'tag' values. Here is what the data looks like behind the scenes:

https://github.com/siteleaf/siteleaf-api#get-v1taxonomyidjson

We'll update the theme docs to make this more clear.

  1. Agreed, they list it but don't explain it further.

  2. You can't sort on a for statement, but you can sort first and assign it to a new variable like this:

{% assign sorted_tags = site.pages['blog'].taxonomy['tags'].values | sort:'value' %}

Now you can use the sorted_tags variable:

{% for tag in sorted_tags %}
    <li><a href="{{tag.url}}">{{tag.value}}</a></li>
{% endfor %}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment