can.stache
can/view/stache 2.1Live binding Mustache and Handlebars-comptable templates.
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
Stache templates are a mustache and handlebars compatable syntax. They are used to:
The following creates a stache template, renders it with data, and inserts the result into the page:
Render a template with observable data like can.Maps or can.Lists and the HTML will update when the observable data changes.
There's a whole lot of behavior that
can.stacheprovides. The following walks through the most important stuff:{{key}}and{{#key}}...{{/key}}{{helper arg}}and{{method(arg)}}Differences from can.mustache
can.stacheis 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
greeting.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.fnsection 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.mustachesupported element callbacks like{{(el) -> CODE}}. These are not supported incan.stache. Instead, create a helper that returns a function or register a custom attribute.Working with Promises
Promises passed into a template have the following attributes that are observable:
isResolvedis trueisRejectedis trueStache Template
JavaScript
Tags