How do I chain if statements in Jekyll?
Asked Answered
U

2

80

I am using a logic operator in Jekyll but it's not working.

Page one, two and three all use the same layout (part of a multilingual solution, works good but requires logical loops for some layout control to keep things DRY.)

Here's the code:

{% if page.type == "post" %}
{% include post.html %}
{% elseif page.class == "contact" %}
{% include contact.html %}
{% else %}
{{ content }}
{% endif %}

If I break it down to just an else and an if else setup, with any two of the tree, everything works. But as soon as I use a third condition it breaks. Am I limited to two conditionals with Jekyll? I can potentially restructure to make a case operator applicable, but I'd prefer to understand the fundamental problem here. Thanks all.

Uxorial answered 14/9, 2012 at 11:31 Comment(2)
Use elsif instead of elseifChimera
See github.com/Shopify/liquid/wiki/Liquid-for-DesignersBackwater
D
133

In Jekyll/Liquid else-if is spelt elsif, i.e.:

{% if page.type == "post" %}
{% include post.html %}
{% elsif page.class == "contact" %}
{% include contact.html %}
{% else %}
{{ content }}
{% endif %}
Drizzle answered 14/9, 2012 at 11:47 Comment(1)
Can you continue to use {% else %} for even more statements?Altheaalthee
S
-2

You can either use else if or elsif. elseif is wrong and will throw an error.

Scaffold answered 29/5, 2021 at 7:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.