Nested "for" loop inside "if" condition in Liquid/Jekyll - jekyll

I'm getting syntax error while trying to generate site. Do you have any ideas what can be wrong here?
Error: Liquid syntax error: Unknown tag 'elsif'
{% if page.title == "Tags" %}
{% for tag in site.tags %}
{% elsif page.title == "Categories" %}
{% for tag in site.categories %}
{% endif %}
{{ tag[0] }}
{% endfor %}

You can't start a loop conditionally like that, control blocks must be properly nested. To accomplish what you're trying to do you could do:
{% if page.title == "Tags" %}
{% assign data = site.tags %}
{% elsif page.title == "Categories" %}
{% assign data = site.categories %}
{% endif %}
{% for tag in data %}
{{ tag[0] }}
{% endfor %}

You have got it all wrong. The nested loops does not work this way.
It should start and end inside the same conditional.
if conditional
for loop
endfor
endif
something like this.
So, the correct way to do it should be this
{% if page.title == "Tags" %}
{% for tag in site.tags %}
{{ tag[0] }}
{% endfor %}
{% elsif page.title == "Categories" %}
{% for tag in site.categories %}
{{ tag[0] }}
{% endfor %}
{% endif %}
This code should do exactly what you want correctly but there is a better approach as answered by JJJ

Related

Liquid/Jekyll: How to check for no posts when one has two or more conditions?

I am having trouble understanding how I might show a "no posts exist" message for a particular conditional statement with two variables.
In this example, let's say I have a collection of "animals" - on a particular page, I'd like a section that displays "primates that are herbivores":
{% for animal in site.animal %}
{% if animal.species == "primate" and animal.type == "herbivore" %}
{{ animal.content }}
{% endif %}
{% endfor %}
What I'd like to do is something like this (pseudocode):
{% if POSTS_EXIST_FOR_THIS_COMBO: (animal.species == "primate" and animal.type == "herbivore") %}
{% for animal in site.animal %}
{% if animal.species == "primate" and animal.type == "herbivore" %}
{{ animal.content }}
{% endif %}
{% endfor %}
{% else %}
There are no posts for this category.
{% endif %}
Note: This differs from examples like this, because I have two parameters to check. Can someone offer a suggestion about the syntax?
I think you can do the following where you at first filter all by species=primate from site.animal and then filter by type=herbivore from that pool and then check if the result exists.
{% assign animals = site.animal | where:"species","primate" | where:"type","herbivore" %}
{% if animals %}
{% for animal in animals %}
{{ animal.content }}
{% endfor %}
{% endif %}
Hope this helps.

Django Multi-Conditional Statement

I have the following code working in a Django HTML template. However, it’s quite repetitive. How can this code be simplified?
It translates to, "If you're not staff, you get to see nav.html. If you are staff, you only get to see nav.html if you're on these 4 pages."
{% if not request.user.is_staff %}
{% include ‘nav.html’ %}
{% else %}
{% if request.get_full_path == ‘/one/’ %}
{% include ‘nav.html’ %}
{% if request.get_full_path == ‘/two/’ %}
{% include ‘nav.html’ %}
{% if request.get_full_path == ‘/three/’ %}
{% include ‘nav.html’ %}
{% if request.get_full_path == ‘/four/’ %}
{% include ‘nav.html’ %}
{% endif %}
{% endif %}
From your view, you can send a list containing the paths for which to show nav.html. Something like,
allowed_paths = ['/one/', '/two/', '/three/', '/fourth/']
In your template, you can simply do,
{% if not request.user.is_staff or request.get_full_path in allowed_paths %}
{% include 'nav.html' %}
{% endif %}

Liquid markup sorting the output

I am trying to display a list of all articles using liquid markup. I've got this code which displays them properly, however I want to be able to sort by the modified date descending (most recent article on top). How can this be accomplished?
I was thinking that perhaps I need to create a new array with all articles in it and then sort that, but I am not sure how to do that. Also note that I want to sort ALL of my articles by date, not just within each folder.
{% for category in portal.solution_categories %}
{% if category.folders_count > 0 %}
{% for folder in category.folders %}
{% for article in folder.articles %}
{{ article.title }} - {{ article.modified_on | short_day_with_time }} <br>
{% endfor %}
{% endfor %}
{% endif %}
{% endfor %}
Thanks!
You can use a variable to sort the list of articles and then iterate that variable.
{% for category in portal.solution_categories %}
{% if category.folders_count > 0 %}
{% for folder in category.folders %}
{% assign sorted = (folder.articles | sort:date) %}
{% for article in sorted %}
{{ article.title }} - {{ article.modified_on | short_day_with_time }} <br>
{% endfor %}
{% endfor %}
{% endif %}
{% endfor %}

Jekyll - Liquid Templating - Exclude a Category from Widget

I am attempting to rebuild a sidebar widget which shows my categories used in Jekyll. It works fine as it is now. I want to change the liquid templating to exclude one specific category link from being shown in this widget.
{% assign cat_list = site.categories %}
{% if cat_list.first[0] == null %}
{% for category in cat_list %}
<li>{{ category }} <span class="cat-count">{{ cat_list[category].size }}</span></li>
{% endfor %}
{% else %}
{% for category in cat_list %}
<li>{{ category[0] }} <span class="cat-count">{{ category[1].size }}</span></li>
{% endfor %}
{% endif %}
{% assign cat_list = nil %}
I think what I want is something like
{% for category in cat_list **UNLESS category = 'CATEGORY'** %}
But that did not work. I'm kinda stuck, is this possible?
Thank You.
Not displayed categories array :
{% assign noDisplay = "one,two,three" | split: "," %} => ["one", "two", "three"]
The test :
{% unless noDisplay contains category[0] %}
{{ category[0] }}...
{% endunless %}
Thank you, #David Jacquel
{% assign noDisplay = "CATEGORY" | split: "," %}
{% assign cat_list = site.categories %}
{% if cat_list.first[0] == null %}
{% for category in cat_list %}
<li>{{ category }} <span class="cat-count">{{ cat_list[category].size }}</span></li>
{% endfor %}
{% else %}
{% for category in cat_list %}
{% unless noDisplay contains category[0] %}
<li>{{ category[0] }} <span class="cat-count">{{ category[1].size }}</span></li>
{% endunless %}
{% endfor %}
{% endif %}
{% assign cat_list = nil %}

Any way to access directory nodes?

In Jekyll 2, suppose I have a collection named mycol and that it has a structure like this in the filesystem:
_mycol/
00-intro.md
01-preface/
00-intro.md
01-something.md
02-subsection/
00-intro.md
03-hello.md
02-section/
00-intro.md
Is there any way to do have inner loops and detect if the current item is a directory or actual data?
Here's an example of what I would like :
{% for d in site.mycol %}
{% if d.content %}
{{ d.content }}
{% else %}
{% for v in d %}
{% if v.content %}
{{ v.content }}
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
Is there any know way or hack to achieve this?