Because of subtle issues with the HTML spec, there are unfixable bugs with this template engine. For example, it's not possible to dynamically create table rows:
<table>
{{#each items}}
<tr>
<td>{{name}}</td>
<td>{{color}}</td>
</tr>
{{/each}}
</table>
Given the template above, the spec says that the text content outside of <td>
should be lifted up out of its original position. This obviously breaks the template.
Stubble is a templating language for the browser, based loosely on Mustache's syntax.
- Uses DOM-based templates, not strings
- No scope traversal via
../
, but this may be added later - Helpers must be explicitly named, e.g.
{{#each foo}}{{/each}}
instead of{{#foo}}{{/foo}}
- No triple-curly, e.g.
{{{foo}}}
, for raw HTML
Stubble recognizes the following types of data:
- Array-like objects
Node
and its descendantsJQuery
objects- Everything else
Array-like objects (that is, objects with a length
property) can be iterated
over using a built-in helper, each
.
Instances of Node
and JQuery
are inserted into the rendered result directly. This means
that you can attach event-handlers, JQuery data, etc., and still make use of
it after wrapping the object in a template. This is the primary motivation for Stubble.
All other data types are stringified via their .toString()
method, and then
inserted into the rendered result as a text node. This means that all data is HTML-encoded
automatically.
There are three helpers built into Stubble. These helpers cover the basic logic
needed to render templates. Each helper accepts an optional {{else}}
block which
behaves differently according to its purpose.
The each
helper iterates over each item in the value passed to it, rendering
the block contents with the context set to the current item. If the value passed
is not an array-like object, or its length
property is 0
, then the else
block is rendered instead.
The if
helper tests the value passed to it. If it is truthy, the block is rendered
as usual. Otherwise, the {{else}}
block is rendered.
The with
helper is much like the if
helper, except that it temporarily changes the
context of the template to the value it receives.