A key specifies a value in the scope or
options of a template being rendered. The
key is used to look up a value in the scope.
What the key looks like changes the behavior of how a value is looked up in
the scope. Keys can look like:
{{name}}
- a single property name.
{{name.first}}
- multiple property names.
{{foo\\.bar}}
- a single property name that includes a dot character.
{{./name}}
- looks up a single property in the current context.
{{../name}}
- looks up a single property in the parent context.
{{.}}
or {{this}}
- looks up the current context.
@index
- The index of a value in an array or can.List.
@key
- The property name of a value within an object or can.Map.
A named reference to a value in the scope or options of a template being rendered.
Use
A key references a value within the scope of a template being rendered. In the following example, the key is
name
:If this template is rendered with:
The template writes out:
A scope is a collection of multiple contexts. By default, a key walks up the scope to each context until it finds a value. For example, a template like:
Rendered with:
Writes out:
When
last
is looked up on the{first: "Justin"}
object and not found, it will then try to read the parent context'slast
property. This is why "Justin Meyer" is written out.Controlling context lookup
Sometimes, especially with recursive templates, you want to control which context is used to lookup. Adding
./
before the key name will only look up in the current context. If we change the previous template to:It will write out:
Adding
../
before a key will lookup the key starting in the parent context. By changing the previous template to:It will write out:
To write out the current context, write
{{.}}
or{{this}}
. For example, a template like:With data like:
Will write out:
@index and @key
When looping over an array or can.List, you an use
@index
to write out the index of each property:Indexes start at 0. If you want to start at 1, you can create a helper like:
And use it like: