I’d like some help with Tera language if and elseif statements

I’m not good at all with writing out Tera. What I’m trying to do is say:

If term.name = widgets

output this description

So far I have got this:

  {% if term.name | widgets %}
    <h2>Description</h2>
  {% endif %}
```

This doesn’t work, as in the description isn’t output for that particular term.

It would also be useful to know how to use an`elseif`.Any advice appreciated.

{% if price < 10 or always_show %}
   Price is {{ price }}.
{% elif price > 1000 and not rich %}
   That's expensive!
{% else %}
    N/A
{% endif %}

Thanks for this, but I am looking for something to do with the taxonomy and terms. Probably didn’t explain myself very well because I don’t know what I’m trying to explain. If a term name on a given page is ‘Widgets’ I need it to return a widgets’ description. If the term name happens to be ‘Wantons’ I need it to return a wantons’ description.

Does that make sense?

Maybe you want:

{% if page.taxonomies.categories %}
  {% if "Widgets" in page.taxonomies.categories %}
    <p>Description for Widgets goes here...</p>
  {% elif "Wantons" in page.taxonomies.categories %}
    <p>Description for Wantons goes here...</p>
  {% endif %}
{% endif %}

If you end up having many terms, writing dozens of elif statements in your template can get messy. You can instead store your term descriptions in config.toml under [extra.term_descriptions]:

[extra.term_descriptions]
widgets = "This is the description for widgets."
wantons = "This is the description for wantons."

Then in your template, retrieve it dynamically without needing any if / elif blocks:

{% if config.extra.term_descriptions[term.slug] %}
  <p>{{ config.extra.term_descriptions[term.slug] }}</p>
{% endif %}
1 Like

Thanks, Colas. The dynamic method worked for me. The first method didn’t, despite me changing categories to tags.

Being a complete numpty when it comes to this type of thing, I really appreciate the effort it takes for someone in the know trying to explain things.

1 Like

Thanks.

Ah Yes, it was “tags”, and it worked for me… but anyways the dynamic version is nicer :slight_smile:

% if page.taxonomies.tags %}
  {% if "Widgets" in page.taxonomies.tags %}
    <p>Description for Widgets goes here...</p>
  {% elif "Wantons" in page.taxonomies.tags %}
    <p>Description for Wantons goes here...</p>
  {% endif %}
{% endif %}
    

I would advise you to use AI, it is an invaluable tool to learn (or just cope with) new systems. Especially for these “How to?” questions.

1 Like