Questions tagged [handlebars.js]

Handlebars is a templating library for JavaScript.

Handlebars is a template library that is, more or less, based on . Handlebars uses Mustache template syntax to render templates but it also adds these features compared to Mustache:

  • Handlebars templates are compiled
  • Handlebars adds #if, #unless, #with, and #each
  • Handlebars supports custom helpers
  • Handlebars supports paths
  • Allows use of {{this}} in blocks (which outputs the current item's string value)
  • Handlebars.SafeString() (and maybe some other methods)
  • Handlebars is 2 to 7 times faster

However Mustache supports inverted sections (i.e. if !x ...) that are not used in Handlebars

Handlebars is used in as a Templating engine, Ember.js add some additionnal builtin helpers to the vanilla Handlebars.js. can also use handlebars when installed with the hbs option : --hbs


Resources :

7223 questions
118
votes
6 answers

How to iterate over array of objects in Handlebars?

This might seem a silly question but I can't seem to find the answer anywhere. I'm hitting this Web API that returns an array of objects in JSON format: Handlebars docs shows the following example:
    {{#each people}} …
empz
  • 8,787
  • 9
  • 57
  • 91
90
votes
4 answers

How to find Array length inside the Handlebar templates?

I have a Handlebars template which is rendered using a json object. In this json I am sending an array. Like this: var json = { "array":["abc","def","ghi","jkl"] } Now in my template I want to find the length of this array.…
Abhidev
  • 6,413
  • 5
  • 18
  • 25
77
votes
5 answers

Iterating over basic “for” loop using Handlebars.js

I’m new to Handlebars.js and just started using it. Most of the examples are based on iterating over an object. I wanted to know how to use handlebars in basic for loop. Example. for(i=0 ; i<100 ; i++) { create li's with i as the value } How can…
user1184100
  • 6,322
  • 27
  • 74
  • 116
77
votes
6 answers

Need Handlebars.js to render object data instead of "[Object object]"

I'm using Handlebars templates and JSON data is already represented in [Object object], how do I parse this data outside of the Handlebars? For example, I'm trying to populate a JavaScript variable on the page through a handlebars tag, but this…
dzm
  • 20,376
  • 41
  • 127
  • 215
76
votes
6 answers

conditional on last item in array using handlebars.js template

I am leveraging handlebars.js for my templating engine and am looking to make a conditional segment display only if it is the last item in array contained in the templates configuration object. { columns:…
techie.brandon
  • 1,458
  • 2
  • 15
  • 26
67
votes
5 answers

Using pre-compiled templates with Handlebars.js (jQuery Mobile environment)

I am struggling somewhat with pre-compilation of templates in Handlebars. My jQuery Mobile project is getting pretty big template-wise and I wish to pre-compile the templates I use. However I can't seem to find a good explanation (like a step by…
Marco
  • 2,403
  • 3
  • 22
  • 19
67
votes
10 answers

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

I'm iterating over a list in Handlebars using the built-in each helper. Within the each block, I'm referencing the current loop index {{@index}} to print the consecutive number of an item: