Handlebars, whitespace control

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: … Read more

Example of using Handlebars lookup helper

Sure, past me! Here’s an example from your future. Suppose you have an object or array obj and a variable field and you want to output the value of obj[field], you would use the lookup helper {{lookup obj field}}. The code defining the helper is simply: function(obj, field) { return obj && obj[field]; }

Is it possible to assign a parameter value within handlebars templates without using helpers?

I needed this solution solved in the same way the original poster expected. Therefore I created a helper function to do this for me. function setVariable(varName, varValue, options){ options.data.root[varName] = varValue; }; Contained within the options in the helper, is the data block, with the root of the helper variables. I placed my variable within … Read more

Does handlebars.js replace newline characters with ?

It doesn’t do so automatically, but using the helpers feature this can be achieved: JS: Handlebars.registerHelper(‘breaklines’, function(text) { text = Handlebars.Utils.escapeExpression(text); text = text.replace(/(\r\n|\n|\r)/gm, ‘<br>’); return new Handlebars.SafeString(text); }); HTML template: <div> {{breaklines description}} </div>

Rendering a string array with handlebars

Yes, you should use an each loop: <ul> {{#each selectedUsers}} <li>{{ this }}</li> {{/each}} </ul> From the docs: You can iterate over a list using the built-in each helper. Inside the block, you can use this to reference the element being iterated over. <ul class=”people_list”> {{#each people}} <li>{{this}}</li> {{/each}} </ul> when used with this context: … Read more

Adding offset to {{@index}} when looping through items in Handlebars

Handlebars gives you the possibility to write a custom helper that handles this situation, e.g. a helper function that lets you perform calculations on expressions like addition and subtraction etc. Below function registers a new helper, which simply increments a value by 1: var Handlebars = require(‘handlebars’); Handlebars.registerHelper(“inc”, function(value, options) { return parseInt(value) + 1; … Read more

Iterating over basic “for” loop using Handlebars.js

There’s nothing in Handlebars for this but you can add your own helpers easily enough. If you just wanted to do something n times then: Handlebars.registerHelper(‘times’, function(n, block) { var accum = ”; for(var i = 0; i < n; ++i) accum += block.fn(i); return accum; }); and {{#times 10}} <span>{{this}}</span> {{/times}} If you wanted … Read more

Handlebars.js Else If

Handlebars supports {{else if}} blocks as of 3.0.0. Handlebars v3.0.0 or greater: {{#if FriendStatus.IsFriend}} <div class=”ui-state-default ui-corner-all” title=”.ui-icon-mail-closed”><span class=”ui-icon ui-icon-mail-closed”></span></div> {{else if FriendStatus.FriendRequested}} <div class=”ui-state-default ui-corner-all” title=”.ui-icon-check”><span class=”ui-icon ui-icon-check”></span></div> {{else}} <div class=”ui-state-default ui-corner-all” title=”.ui-icon-plusthick”><span class=”ui-icon ui-icon-plusthick”></span></div> {{/if}} Prior to Handlebars v3.0.0, however, you will have to either define a helper that handles the branching logic … Read more

What are the differences between Mustache.js and Handlebars.js?

You’ve pretty much nailed it, however Mustache templates can also be compiled. Mustache is missing helpers and the more advanced blocks because it strives to be logicless. Handlebars’ custom helpers can be very useful, but often end up introducing logic into your templates. Mustache has many different compilers (JavaScript, Ruby, Python, C, etc.). Handlebars began … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)