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?

Answers


with Hogan there migt be, depending what you really want to do with it; https://github.com/twitter/hogan.js#features

var text = "{{^check}}{{#i18n}}No{{/i18n}}{{/check}}";
text +=  "{{#check}}{{#i18n}}Yes{{/i18n}}{{/check}}";
var tree = Hogan.parse(Hogan.scan(text));

// outputs "# check"
console.log(tree[0].tag + " " + tree[0].name);

// outputs "Yes"
console.log(tree[1].nodes[0].nodes[0]);

the tags are parsed out, you can manipulate the tree as you see fit, and then compile it when done


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


Need Your Help

Alternatives to MS OpenXML SDK

.net openxml docx

I did some investigations into OpenXML 2.0 a few years ago, as we have a server application which builds Word files, which was using Word interop and therefore required Word installed on the server...