Every part of a stache template is rendered with a
given scope. The scope is used to lookup
values. A scope can contain multiple places to lookup values.
Lets look at what happens with the scope the following example:
The template is rendered with Data as the only item in the scope. scope:[Data]
{{message}} is looked up withinData`.
{{#person}} adds the person object to the top of the scope. scope:[Data,Data.person]
{{first}} is looked up in the scope. It will be found on Data.person.
{{last}} is looked up in the scope.
last is looked in Data.person, it's not found.
last is looked up in Data and returned.
{{/person}} removes person from the scope. scope:[Data]
The scope used to lookup a value can be controlled by adding ../ or ./ before a
key. For instance, if we wanted to make sure last was only going to lookup on person,
we could change the template to:
When using [can.stache.Basics tags] in Stache, the key in {{key}}
references a property on the current context object. The default context always points to the data
object initially passed to the template.
Instead of simply referencing a key matching a property on the current context object, a full path can
be included instead. When a path is found, Stache will look for a matching property using the entire path:
Additionally, the current context can be changed by using sections. Anytime a section
is opened, any tags inside of it will use that object as the local context for any key lookups:
Scope and Contexts
Every part of a stache template is rendered with a given scope. The scope is used to lookup values. A scope can contain multiple places to lookup values.
Lets look at what happens with the scope the following example:
Data
as the only item in the scope.scope:[Data]
{{message}} is looked up within
Data`.{{#person}}
adds theperson
object to the top of the scope.scope:[Data,Data.person]
{{first}}
is looked up in the scope. It will be found onData.person
.{{last}}
is looked up in the scope.last
is looked inData.person
, it's not found.last
is looked up inData
and returned.{{/person}}
removesperson
from the scope.scope:[Data]
The scope used to lookup a value can be controlled by adding
../
or./
before a key. For instance, if we wanted to make surelast
was only going to lookup onperson
, we could change the template to:Sections, Helpers, and custom elements can modify the scope used to render a subsection.
Older
When using [can.stache.Basics tags] in Stache, the
key
in{{key}}
references a property on the current context object. The default context always points to the data object initially passed to the template.Instead of simply referencing a key matching a property on the current context object, a full path can be included instead. When a path is found, Stache will look for a matching property using the entire path:
Additionally, the current context can be changed by using sections. Anytime a section is opened, any tags inside of it will use that object as the local context for any key lookups:
If the key used within a section is not found on the local context, Stache will look up the stack of contexts until it finds a matching key: