Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make list order ascending with Jekyll and Liquid?

Tags:

jekyll

liquid

I am starting with Liquid & Jekyll can anyone help me with making list order ascending (by {{category}} name)?

<div class="col-xs-6 col-sm-3 patickaborder">
                <h5>Rubriky</h5>
                    <ul>
                        {% for category in site.categories order:ascending %}
                            <li><a href="{{ site.url }}{{ category | first }}/index.html">{{ category | first }}</a></li>
                        {% endfor %}
                    </ul>
            </div>
like image 341
Kodejn Avatar asked Dec 16 '22 00:12

Kodejn


2 Answers

Just found this out. You can sort the list and assign it to a variable, then use that in your markup, like this:

{% assign sortedcats = site.categories | sort %}

<div class="col-xs-6 col-sm-3 patickaborder">
    <h5>Rubriky</h5>
        <ul>
            {% for category in sortedcats %}
                <li><a href="{{ site.url }}{{ category | first }}/index.html">{{ category | first }}</a></li>
            {% endfor %}
        </ul>
</div>
like image 105
katdee Avatar answered Jan 07 '23 16:01

katdee


As far as I know, there's no built-in way for ordering as of now, it's only possible with plugins.

The order:ascending syntax you're using in your question will maybe work in the future, when this is implemented in Liquid.
In the same question, there's another answer that shows how to sort with a plugin (which means that it won't work on GitHub Pages!).


If you can't use or don't want to use a plugin, it's possible to do it without plugins...with some really ugly looking Liquid-foo.

Check out this answer, where I'm ordering site.tags alphabetically to create a list of tags for my blog.

like image 41
Christian Specht Avatar answered Jan 07 '23 17:01

Christian Specht