Friday, December 23, 2011

Using hogan.js with express on node.js

Thought i'd try getting twitter's hogan.js up and running with express... not as straight forward as i expected. Drawing upon this article on using mustache.js with express.js i created a simple adapter to help bridge functionality.

I would only use this while connect 3.x is in dev. After that i'd use TJ's consolidate.js cause i'm no TJ.

If you're looking for a full, working example, be sure to checkout a Nodestrap-- a repo i use as a project "prototype." It comes out of the box with better than average architecture, hogan-express templating, bootstrap 1.x. Its a good jumping off point.

Here's the adapter:

Here's an example of it in use:

In the views directory i have a index.hogan.js file-- that's the template.

It's worked for simple uses so far. I'll update if needs be.

6 comments:

  1. you could do this much simpler even for 2x, but with 3x coming out soon I wont go into details

    ReplyDelete
  2. @TJ updated it per your suggestion

    ReplyDelete
  3. Is there a specific syntax needed in the layout file to get layout:true to work?

    ReplyDelete
  4. Does the adapter need to be used somewhere, I'm not sure if this works as-is?

    What version of express is this for ?

    ReplyDelete
  5. It was express 2.x. Given that this code is > 6 months old, perhaps it is dead at this point!

    You may want to check out consolidate.js https://github.com/visionmedia/consolidate.js

    ReplyDelete
  6. I'd check out mmm.

    https://github.com/techhead/mmm

    It has support for layout, partials, and caching. It was made specifically for Express 3.

    ReplyDelete