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

Sections

Lukas Jans edited this page Feb 21, 2019 · 1 revision

A section is formed by an opening and closing tag of the same name.

The delimiting tags of a section enclose its content. Sections have the same name and context as their tags. There are two different types of sections:

  • Regular sections using the regular opening tag:

    {{#test}}content{{/test}}

  • Inverted sections using the inverted opening tag:

    {{^test}}content{{/test}}

Besides their type, sections have different abilities.

Conditional sections

When a section gets rendered, a check is performed whether the assigned context is empty. The following values are considered empty:

  • Empty objects {}
  • Empty arrays []
  • Empty strings ''
  • Zero 0
  • false
  • null
  • undefined

If you assign a regular section to an empty context, the section will be removed without replacement. The other way around, inverted sections will be removed when assigned to a non-empty context.

Recursive sections

Sections can also be used for encapsulating parts of your template that should be rendered for themselves. If you assign a regular section to an object, its content will be rendered recursively using the assigned object. The corresponding result will replace the section.

This process can be done repeatedly by nesting sections. Just like objects can contain other objects, sections can contain other sections. See the below template for an example:

{{#alice}}
	{{#interests}}
		Content rendered with the interests of Alice
	{{/interests}}
{{/alice}}

This notation can be simplified using a dot (.) to cascade through objects directly:

{{#alice.interests}}
	Content rendered with the interests of Alice
{{/alice.interests}}
Clone this wiki locally