can.stache
can/view/stache 2.1Logic-less Handlebar and Mustache templates with live binding.
can.stache(template)
{{{key}}}
Behaves just like {{key}} and {{helper}} but does not escape the result.
{{&key}}
The `{{&key}}` tag is an alias for {{{key}}}, behaving just like {{key}} and {{helper}} but does not escape the result.
{{#key}}BLOCK{{/key}}
Render blocks of text one or more times, depending on the value of the key in the current context.
{{^key}}BLOCK{{/key}}
Render blocks of text if the value of the key is falsey.
{{>key}}
Render another template within the current template.
{{!key}}
The comment tag operates similarly to a `` tag in HTML.
Use
Mustache and Handlebar templates are compatible with can.stache.
Stache templates looks similar to normal HTML except they contain keys for inserting data into the template and sections to enumerate and/or filter the enclosed template blocks.
For example, the following renders a welcome header for a user and displays the number of messages.
Stache Template
JavaScript
HTML Result
To update the html using live-binding, change an observable value:
This updates this paragraph in the HTML Result to:
can.stache provides significantly more functionality such as:
Differences from can.mustache
can.stache
is largely compatable with can.mustache. There are three main differences:{key}
.{{(el) -> CODE}}
are no longer supported.Passing values in the scope to can.Components
A can.mustache template passes values from the scope to a can.Component by specifying the key of the value in the attribute directly. For example:
With stache, you wrap the key with
{}
. For example:If the key was not wrapped, the template would render:
Because the attribute value would be passed as the value of
gretting
.Section renderers return documentFragments
A Mustache section renderer called like
options.fn()
oroptions.inverse()
would always return a String. For example, the following would wrap the.fn
section in an<h1>
tag:can.stache
s section renderers return documentFragments when sections are not contained within an html element. This means the result of the previous helper would be:Instead, helper functions should manipulate the document fragment into the desired response. With jQuery, this can be done like:
Element callbacks are no longer supported
can.mustache
supported element callbacks like{{(el) -> CODE}}
. These are not supported incan.stache
. Instead, create a helper that returns a function or register a custom attribute.Tags