can
Version:
MIT-licensed, client-side, JavaScript framework that makes building rich web applications easy.
137 lines (91 loc) • 4.09 kB
Markdown
1
Calls a stache helper function with a block, and optional inverse
block.
`{{#helper [args...] [hashName=hashValue...]}}BLOCK{{/helper}}`
Calls a stache helper function or a function with a block to
render.
The template:
<p>{{#countTo number}}{{num}}{{/countTo}}</p>
Rendered with:
{number: 5}
Will call the `countTo` helper:
can.stache.registerHelper('countTo',
function(number, options){
var out = [];
for(var i =0; i < number; i++){
var docFrag = options.fn({num: i+1});
out.push( docFrag.textContent );
}
return out.join(" ");
});
Results in:
<p>1 2 3 4 5</p>
{can.stache.key} helper A key that finds a [can.stache.helper helper function]
that is either [can.stache.registerHelper registered] or found within the
current or parent [can.stache.context context].
{...can.stache.key|String|Number} [args] Space 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.
{String} hashProperty
A property name that gets added to a [can.stache.helperOptions helper options]'s
hash object.
{...can.stache.key|String|Number} hashValue A value that gets
set as a property value of the [can.stache.helperOptions helper option argument]'s
hash object.
{stache} BLOCK A stache template that gets compiled and
passed to the helper function as the [can.stache.helperOptions options argument's] `fn`
property.
`{{#helper [args...] [hashName=hashValue...]}}BLOCK{{else}}INVERSE{{/helper}}`
Calls a stache helper function or a function with a `fn` and `inverse` block to
render.
The template:
<p>The bed is
{{#isJustRight firmness}}
pefect!
{{else}}
uncomfortable.
{{/justRight}}</p>
Rendered with:
{firmness: 45}
Will call the `isJustRight` helper:
can.stache.registerHelper('isJustRight',
function(number, options){
if(number > 50){
return options.fn(this);
} else {
return options.inverse(this);
}
});
Results in:
<p>The bed is uncomfortable.</p>
{can.stache.key} helper A key that finds a [can.stache.helper helper function]
that is either [can.stache.registerHelper registered] or found within the
current or parent [can.stache.context context].
{...can.stache.key|String|Number} [args] Space 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.
{String} hashProperty
A property name that gets added to a [can.stache.helperOptions helper options]'s
hash object.
{...can.stache.key|String|Number} hashValue A value that gets
set as a property value of the [can.stache.helperOptions helper option argument]'s
hash object.
{stache} BLOCK A stache template that gets compiled and
passed to the helper function as the [can.stache.helperOptions options argument's] `fn`
property.
{stache} INVERSE A stache template that gets compiled and
passed to the helper function as the [can.stache.helperOptions options argument's] `inverse`
property.
## Use
Read the [use section of {{helper}}](can.stache.helpers.helper.html#section_Use) to better understand how:
- [Helper functions are found](can.stache.helpers.helper.html#section_Arguments)
- [Arguments are passed to the helper](can.stache.helpers.helper.html#section_Arguments)
- [Hash values are passed to the helper](can.stache.helpers.helper.html#section_Hash)
Read how [helpers that return functions](can.stache.helper.html#section_Returninganelementcallbackfunction) can
be used for rich behavior like 2-way binding.
can.stache.helpers.sectionHelper {{#helper args hashes}}
can.stache.htags