{{mustache}}
, pass them to a Handlebars helper, or use them as values in hash arguments. <h1>{{title}}</h1>
title
property in the current context". Block helpers may manipulate the current context, but they do not change the basic meaning of an expression. title
, then do the above", but we'll get to that soon enough. <h1>{{article.title}}</h1>
article
property in the current context. Then look up the title
property in the result". /
syntax, so you could write the above template as: <h1>{{article/title}}</h1>
!
"
#
%
&
'
(
)
*
+
,
.
/
;
<
=
>
@
[
\
]
^
`
{
|
}
~
[
: {{#each articles.[10].[#comments]}} <h1>{{subject}}</h1> <div> {{body}} </div> {{/each}}
each
parameter roughly equivalent to this javascript: articles[10]['#comments']
]
in a path-literal, but all other characters are fair game. "
and '
, may also be used vs. [
pairs. {{expression}}
. If you don't want Handlebars to escape a value, use the "triple-stash", {{{
{{{foo}}}
{{{link story}}}
link
is the name of a Handlebars helper, and story is a parameter to the helper. Handlebars evaluates parameters in exactly the same way described above in "Basic Usage". Handlebars.registerHelper('link', function(object) { var url = Handlebars.escapeExpression(object.url), text = Handlebars.escapeExpression(object.text); return new Handlebars.SafeString( "<a href='" + url + "'>" + text + "</a>" ); });
escapeExpression
method. {{{link "See more..." story.url}}}
"See more..."
and the result of evaluating story.url
in the current context. Handlebars.registerHelper('link', function(text, url) { url = Handlebars.escapeExpression(url); text = Handlebars.escapeExpression(text); return new Handlebars.SafeString( "<a href='" + url + "'>" + text + "</a>" ); });
story.text
: {{{link story.text story.url}}}
{{{link "See more..." href=story.url class="story"}}}
Handlebars.registerHelper('link', function(text, options) { var attrs = []; for (var prop in options.hash) { attrs.push( Handlebars.escapeExpression(prop) + '="' + Handlebars.escapeExpression(options.hash[prop]) + '"'); } return new Handlebars.SafeString( "<a " + attrs.join(" ") + ">" + Handlebars.escapeExpression(text) + "</a>" ); });
{{outer-helper (inner-helper 'abc') 'def'}}
inner-helper
will get invoked with the string argument 'abc'
, and whatever the inner-helper
function returns will get passed in as the first argument to outer-helper
(and 'def'
will get passed in as the second argument to outer-helper
). ~
character by the braces. When applied all whitespace on that side will be removed up to the first handlebars expression or non-whitespace character on that side. {{#each nav ~}} <a href="{{url}}"> {{~#if test}} {{~title}} {{~^~}} Empty {{~/if~}} </a> {{~/each}}
{ nav: [ {url: 'foo', test: true, title: 'bar'}, {url: 'bar'} ] }
<a href="foo">bar</a><a href="bar">Empty</a>
{{#each nav}} <a href="{{url}}"> {{#if test}} {{title}} {{^}} Empty {{/if}} </a> {{~/each}}
<a href="foo"> bar </a> <a href="bar"> Empty </a>
\
. Raw blocks are created using {{{{
mustache braces. \{{escaped}} {{{{raw}}}} {{escaped}} {{{{/raw}}}}
© 2011–2017 by Yehuda Katz
Licensed under the MIT License.
https://handlebarsjs.com/expressions.html