Skip to content
This repository has been archived by the owner on May 3, 2022. It is now read-only.

Fallbacks

Lukas Jans edited this page Aug 28, 2019 · 2 revisions

Sections are great to define fallbacks in case there is no data.

You have already learned that regular sections with an empty context and inverted sections with a non-empty context will be removed. This behavior can be used to implement some sort of if...else-conditions in your template:

{{#login}} Welcome user! {{/login}}
{{^login}} Please login first. {{/login}}

After rendering, only one message will remain depending on the value of login. Remember that the content of regular sections assigned to a non-empty context are rendered recursively using that context.

You can also check for the length of arrays. Compare the following inverted sections:

{{^array}} ... {{/array}}
{{^array.length}} ... {{/array.length}}

They are equivalent because array.length is considered empty exactly when array is considered empty. Now compare the related regular sections:

{{#array}} ... {{/array}}
{{#array.length}} ... {{/array.length}}

They are completely different, because the second one is rendered using a scalar, not an array.

Clone this wiki locally