Posted on

fs-hogan == mustache.java (almost)

So lately I have been working on creating a mustache renderer in NodeJS.
My biggest challenge was finding a NodeJS module that had mustache rendering implemented closely to its Java counterpart.

I was able to find Hogan.js, the mustache renderer done by the twitter guys.
As great and powerful as Hogan.js is, initially I was a little disappointed that it was designed to run in the browser.

That meant no Filesystem operations were allowed. This is kind of like the difference between mustache.js and mu2.js. mu2.js is the NodeJS version, where partials are loaded and parsed from the filesystem, versus mustache.js, where an in memory collection of partials is used to pull from.

Well, Robert Sayer told me that I should just search NPM, and that I would find more than one “fork” of their Hogan.js.
He was right, I quickly found fs-hogan.js which did everything I needed.

The only other difference I have to note between Mustache.Java and FS-Hogan is the use of the {{.}} implicit iterator.
In the Java version, having a data model like:

{title:”some title”}

will render this template

{{#title}} <h2> {{.}} </h2> {{/title}}

without any issues into

<h2> some title </h2>

I can totally see why Sam Pullara made it work like that. Its really easy syntax to follow, and looks very elegant. Unfortunately the same thing will not render in FS-Hogan, you will get:

<h2> </h2>

To get the same result in both Mustache.Java and FS-Hogan, you must use this syntax:

{{#title}}<h2>{{title}}</h2>{{/title}}

 

Big shout out to Robert Sayer and Sam Pullara, thanks for  all your hard work guys!