Skip to content

Search tags, filters, functions and guides.

Tags / if

Tags

if

Render content only when a condition holds.

if renders its content only when a condition is true. Add elseif and else for other cases.

page.kiln
{% if page.hero %}
	<div class="hero">{{ page.hero }}</div>
{% elseif page.image %}
	<img src="{{ page.image }}" alt="">
{% else %}
	<hr>
{% endif %}
Output
<img src="/img/cover.jpg" alt="">

Conditions

Conditions follow Kiln's rules for true and false. Empty arrays and hashes are false, and "0" is true.

Combine conditions with and, or and not, and use tests with is:

{% if user and not user.banned %}
{% if posts is not empty %}
{% if page.subtitle is defined %}

Inline conditions

For a value rather than a block of markup, use ? : inside an expression:

page.kiln
<a class="{{ active ? "link link--active" : "link" }}">Home</a>
Output
<a class="link link--active">Home</a>

if doesn't create a scope, so variables set inside it are still there afterwards.