integrity-glyph iyq924

March 15, 2020

Switch Statements Exist in Nunjucks

Learn how to use switch statements in Nunjucks, even though they are missing from the official documentation.

h

It's not a bug, it's a feature

Despite being nowhere in the current documentation at the time of writing this, the switch statement, which can be very useful, is fully supported in Nunjucks. Here is an example below of how to use it.

<!-- Conditional -->
{% set test = false %}

<!-- Switch -->
{% switch test %}
{% case true %}
<p>This is true</p>
{% default %}
<p>This is false</p>
{% endswitch %}

<!-- Output -->
<p>This is false</p>

Enjoy dryer conditional statements!

let isSiteAwesome = false

function contactAlec(message) {
return alert(message)
}

if (!isSiteAwesome) {
return contactAlec("Let's work together!")
}

Do you want something new and fresh?

Don't get Bored with a project you don't love. I can refresh, or just rebuild you a new website that will knock your socks off.

Contact