-
Notifications
You must be signed in to change notification settings - Fork 0
Fallbacks
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.