Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handlebars, whitespace control

i want fine control of whitespace but still have readable templates.

Just wanted to see if other's solution to by simple use case.

{{name}} {{#if age}}   , {{age}} {{/if}}  # outputs {{name}} , {{age}} # desire: {{name}}, {{age}} 

https://github.com/wycats/handlebars.js/issues/479 - submitted a ticket which was closed.

like image 508
Quang Van Avatar asked Apr 11 '13 15:04

Quang Van


People also ask

How do you escape Handlebars?

Escaping Handlebars expressions Handlebars content may be escaped in one of two ways, inline escapes or raw block helpers. Inline escapes created by prefixing a mustache block with \ .

What does variable triple brace meaning in Handlebars?

Because it was originally designed to generate HTML, Handlebars escapes values returned by a {{expression}} . If you don't want Handlebars to escape a value, use the "triple-stash", {{{ . Source: https://handlebarsjs.com/guide/#html-escaping.

How do you pass variables to Handlebars?

To have access to the passed arguments, you need to look for them into the 'hash' object: {{hash. foo}}. (I'm new with handlebars and this took me a while to figure out) - Thanks, great helper! Note, this requires you to have your partials pre-compiled before using the helper.


1 Answers

Following the history from the pull request to add this feature it looks like this is the correct syntax:

<h4> {{~#object~}}  Surrounding whitespace would be removed.  {{/object}} </h4> 

Result:

<h4>Surrounding whitespace would be removed.</h4> 

There is also this syntax which trims only leading whitespace:

<h4> {{~#object}}  Only leading whitespace would be removed.  {{/object}} </h4> 

Result:

<h4>Only leading whitespace would be removed. </h4> 
like image 52
Brian Avatar answered Sep 20 '22 07:09

Brian