{{helper args hashes}}
can.stache.helpers.helper
Calls a stache helper function and inserts its return value into the rendered template.
{{helper [args...] [hashProperty=hashValue...]}}
Calls a stache helper function or a function. For example:
The template:
<p>{{madLib "Lebron James" verb 4 foo="bar"}}</p>
Rendered with:
{verb: "swept"}
Will call a madLib
helper with the following arguements:
can.stache.registerHelper('madLib',
function(subject, verb, number, options){
// subject -> "Lebron James"
// verb -> "swept"
// number -> 4
// options.hash.foo -> "bar"
});
Parameters
-
helper
{key}
A key that finds a helper function that is either registered or found within the current or parent [can.stache.context context].
-
args
{key | String | Number}
Optional VariableSpace seperated arguments that get passed to the helper function as arguments. If the key's value is a:
- can.Map - A getter/setter can.compute is passed.
- can.compute - The can.compute is passed.
function
- The function's return value is passed.
-
hashProperty
{String}
A property name that gets added to a helper options's hash object.
-
hashValue
{key | String | Number}
A value that gets set as a property value of the helper option argument's hash object.
Use
The
{{helper}}
syntax is used to call out to stache helper functions functions that may contain more complex functionality.helper
is a key that must match either:The following example shows both cases.
The Template:
Rendered with data:
And a with a registered helper like:
Results in:
Arguments
Arguments can be passed from the template to helper function by listing space seperated strings, numbers or other keys after the
helper
name. For example:The template:
Rendered with:
Will call a
madLib
helper with the following arguements:If an argument
key
value is a can.Map property, the Observe's property is converted to a getter/setter can.compute. For example:The template:
Rendered with:
Needs the helper to check if name is a function or not:
This behavior enables two way binding helpers and is explained in more detail on the helper functions docs.
Hash
If enumerated arguments isn't an appropriate way to configure the behavior of a helper, it's possible to pass a hash of key-value pairs to the helper option argument's hash object. Properties and values are specified as
hashProperty=hashValue
. For example:The template:
` And the helper:
Render with:
Results in:
Returning an element callback function
If a helper returns a function, that function is called back after the template has been rendered into DOM elements. This can be used to create stache tags that have rich behavior. Read about it on the helper function page.