Index âș 7. Conditionals
Learn how to implement conditional statements in Kitten templates.
Topics covered
- The different ways to implement conditional logic in Kitten templates.
- Kittenâs
<if ${condition}><then>âŠ</then><else>âŠ</else></if>
tags.
How many kittens are too many kittens?
So kittens are great but maybe after a certain number we should truncate the kitten emojis and display the exact count to save the person from having to count them, as fun as that might be.
Enter, conditionals.
Letâs add a conditional so once there are more than twenty kittens, we show a message that states the additional number instead of showing yet more kitten emojis:
if (kitten.db.kittens === undefined) kitten.db.kittens = { count: 1 }
export default () => {
kitten.db.kittens.count++
return kitten.html`
<h1>Kitten count</h1>
<p>${'đ±ïž'.repeat(kitten.db.kittens.count > 20 ? 20 : kitten.db.kittens.count)}</p>
<if ${kitten.db.kittens.count > 20}>
<p>(and ${kitten.db.kittens.count - 20} more.)</p>
</if>
`
}
Ooh, whatâs that? An <if>
tag? Thatâs not HTML.
No, itâs not. Itâs a little extension to HTML thatâs unique to Kitten.
What you see above is actually a convenient shorthand form of the <if>
tag that comes with an important limitation: it can only be used to display one tag.
In the above example, that means that we can only include that one <p>
tag after the <if>
. The <p>
tag itself can have any number of children but you cannot add any sibling tags.
To understand why, letâs look at the full syntax if the <if>
tag:
kitten.html`
<if ${condition}>
<then>
<h1>The condition is true :)</h1>
<img src='woot.gif' alt='Geeks partying'>
<p>And there was much rejoicing!</p>
</then>
<else>
<h1>The condition is false :(</h1>
<p>Sad trombone.</p>
</else>
</if>
`
The <then>
and <else>
tags, apart from being optional (as you saw in the shorthand version, where theyâre omitted), are also optionally-closed.
So the above example can also be written as shown below without losing the ability to include any number of siblings:
<if ${condition}>
<then>
<h1>The condition is true :)</h1>
<img src='woot.gif' alt='Geeks partying'>
<p>And there was much rejoicing!</p>
<else>
<h1>The condition is false :(</h1>
<p>Sad trombone.</p>
</if>
In fact, most times, this last form is the one you should favour unless you only have a tiny snippet of code (with a single root node) that you want to add conditionally to your page, in which case you can use the first form you saw in the initial example.
The one downside of this canonical form is that youâre left with a dangling indent right before the closing tag (</if>
). If that gets on your nerves or makes it more difficult to ensure that your markup is valid at a glance, please feel free to use the full form. It really is up to you.
đĄ Since the
<then>
and<else>
tags are optional, the way the parser works is that it takes the first child of the<if>
tag as thetrue
condition and the second child as thefalse
condition.
đȘ€ The
<if>
conditional does not short circuit so if you have values in your branches that might be null or undefined, use conditional chaining to ensure you donât encounter runtime errors. Either that, or use JavaScript conditionals in your templates instead.
Now that you know how to implement conditional logic in your Kitten templates, letâs learn about how we can persist a different Kitten count for different browser sessions.
Next tutorial: Sessions