Expressions
can.stache.expressions
In addition to different magic tag types, stache supports different expression types. These can be used in various combinations to call helper methods or viewModel methods. The following is an example of all the expressions combined:
There are 5 expression types stache supports:
{{"string"}}
{{key}}
{{prop=key}}
{{method(arg)}}
{{helper arg}}
Literal expressions
Literal expressions specify JS primitive values like:
"strings"
5
true
orfalse
null
orundefined
They are usually passed as arguments to Call or Helper expressions like:
KeyLookup expressions
A KeyLookup expression specifies a value in the scope or HelperOptions scope that will be looked up. KeyLookup expressions can be the entire stache expression like:
Or they can makeup the method, helper, arguments and hash value parts of Call, Helper, and Hash expressions:
The value looked up by a KeyLookup depends on what the key looks like, and what expression type the KeyLookup is within.
For example,
{{method(~./key)}}
will callmethod
with the a compute that looks up the value ofkey
only in the top of the scope.In general the rules are as follows:
{{method(key)}}
- values are passed.{{helper key}}
- computes are passed.{{method(hash=key)}}
- values are set as property values.{{method hash=key}}
- computes are set as property values.{{%index}}
- lookup values in a special context provided by some helpers.{{method(~key)}}
- pass a compute instead of a value.{{method(@key}}
- pass a function instead of trying to read the value of the function.{{./key}}
- only lookup key at the top of the scope.{{../key}}
- lookup the value in the parent context.{{.}}
- return the current context/top of the scope.Hash expression
A hash expression sepecifies a property value on an options object in a call expression and property value on the the hash object in a helper expression.
For example, in a call expression:
method
will be called with{prop: "value"}
asarg
.In a helper expression:
method
will be called with{prop: compute<"value">}
asoptions.hash
.Call expression
A call expression calls a function looked up in the scope followed by the helpers scope. It looks like:
Call expression arguments are commma (,) seperated. If a Hash expression is an argument, an object with the hash properties and values will be passed. For example:
Helper expression
A helpers expression calls a function looked up in the helpers scope followed by the scope. It looks like:
Helper expression arguments that are observable are passed a compute. This is in contrast to Call expressions that get passed the value.
Helper expression arguments are space seperated. If a Hash expression is an argument, the hash properties and values will be added to the helper options object. For example: