Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing javascript inside Handlebars template

I'm totally new to js template engines. Handlebars seems to be the popular choice. I don't dislike the syntax for doing conditions, loops and so on, but since I'm perfectly capable of and feel more comfortable using plain old js and I'm not planning to let anyone who doesn't know js touch my templates, I'm asking if Handlebars supports this.

Of course the most popular choice isn't always the best. I'm more of a Mootools guy and jQuery drives me crazy(great library, just not for me). So if Handlebars was jQuery of template engines, what would be Mootools?

like image 805
jpeltoniemi Avatar asked Nov 30 '12 22:11

jpeltoniemi


2 Answers

One of the central ideas behind handlebars and mustache is they are LOGICLESS by design and intention. They can not now, nor will they ever allow or recommend you embed raw JS in your templates. Some say this is a better design. If you want embedded javascript, almost every other traditional templating engine, such as _.template or jade or EJS is based on (or at least supports) the idea of embedded JS code snippets.

like image 161
Peter Lyons Avatar answered Oct 09 '22 08:10

Peter Lyons


Sorry to resurrect a old issue here. If you have to use handlebars or it's not feasible to change your templating library you can use Helpers from the Handlebars library.

https://handlebarsjs.com/guide/expressions.html#helpers

like image 41
Doctor Parameter Avatar answered Oct 09 '22 07:10

Doctor Parameter