Handlebars.js – How to escape or display special html characters

Handlebars by default escapes special characters. So, if you trying to print special characters like ™ or ® etc, they will be escaped and the corresponding html codes will be printed. For example , <!DOCTYPE html> <html> <head> <title>Handlebars js</title> <script type="text/javascript" src="handlebars-v4.0.10.js"></script> </head> <body>   <div id="displayArea"></div>   <script id="myTemplate" type="text/x-handlebars-template">     <h1>{{desc}}</h1> </script>   <script type="text/javascript">     var […]


Handlebars.js – Partials

In this article, we will discuss about Partials in Handlebars.js with examples. Handlebars allows code reuse using Partials. A Partial is a common template that can be included in another template. To create and register a partial, the method Handlebars.registerPartial() can be used as show below : Handlebars.registerPartial("messagePartial","Employee : {{name}}, Age : {{age}}" ); The […]


Handlebars.js – Unless helper

The unless helper in handlebars works as “if not”. So, the block under the unless helper will be rendered if the expression passed is false. For example, the following code will display the block of text “No name element present” only if name is not available in the context. {{#unless name}}   <h1>No name element present</h1> […]


Handlebars.js – Creating Custom Helpers

Handlebars Helpers Helpers in Handlebars.js are reusable functions that can be added to data to change its behavior in some way. Handlebars provides several built-in helpers such as if, each, unless etc. (For built-in helper, refer http://handlebarsjs.com/builtin_helpers.html)   Creating custom Handlebars Helpers We can create our own helper functions in Handlebars using the registerHelper() method. […]

Handlebars.js – Looping using the each helper

In Handlebars.js, the built-in each helper can be used to iterate over a list. Here is an example : <!DOCTYPE html> <html> <head> <title>Handlebars js looping</title> <script type="text/javascript" src="handlebars-v4.0.10.js"></script> </head> <body>   <div id="displayArea"></div>   <script id="myTemplate" type="text/x-handlebars-template">     {{#each employees}}     <h1>Employee Name : {{this.name}}, Age : {{this.age}}</h1>     {{/each}} </script>   <script type="text/javascript">     var tmpHtml = document.getElementById("myTemplate").innerHTML;     var template […]