Search code examples
javascripttemplateshandlebars.jsmustachehogan.js

How to override the default output interpolation of Mustache.js, Hogan.js or Handlebars.js?


Is there a way change the default output of libraries like Mustache, Hogan and Handlebars?

Here is my context for example:

var context = {
  primitive: 1,
  array: [1, 2, 3],
  object: new Object()
}

And thats how I would like the result to be when compiling:

<div>
  Primitive: {{primitive}}
  Array: {{array}} should output <Array#id> instead of "1,2,3"
  Object: {{object}} should output <Object#id> instead of "[Object object]"
</div>

I know that I could override the toString method of those properties, but is there any other way like a callback before the interpolation is happening?


Solution

  • Just found out that you can override Handlebars.Utils.escapeExpression to achieve this inside templates.