4

I'd like to add handlebar.js as a view engine in my rails app. I'm trying to share the same templates for both the server and client.

I've found a few gem's already (handlebar_assets and handlebar-rails), but they seem to focus only on client side use. I'd like something that was usable from the server as well.

Is rendering handlebar templates in controllers easy? I'm betting this has been done before, but I'm having a hard time locating examples.

Charles
  • 50,943
  • 13
  • 104
  • 142
Dane O'Connor
  • 75,180
  • 37
  • 119
  • 173
  • 1
    hi, I'm no expert in handlebar but I had a look at the handlebar_assets gem and it seems like the main thing happens [here](https://github.com/leshill/handlebars_assets/blob/master/lib/handlebars_assets/handlebars.rb) by calling `ExecJS.compile(source)` so you just have to write an own rendering engine (google for it there are many examples). – sled Oct 12 '11 at 01:01
  • @sled, I've actually googled around a bit and can't find much. Can you possibly point me to a resource? I thought this would be common, but now I'm not so sure. I've been looking at the haml internals but it's certainly not obvious/clean to me how to do this just yet. – Dane O'Connor Oct 12 '11 at 03:21
  • maybe you could explain what you want to render exactly, just plain javascript? Give me an example. I could help you with this in maybe a few hours, I need some sleep first ;) – sled Oct 12 '11 at 03:36
  • @sled me too :). Idealy, I'd like to grab the template via the asset pipeline, pass it a json context object, render the html to the output buffer from my controller. Even more idealy, it'd to this automatically. Calling render in the controller would: locate the right template, serialize the controller's instance variables to json, and feed rails the rendered html. I figure this can be done with a custom template engine, but yeah, I'm lost on where to start. – Dane O'Connor Oct 12 '11 at 03:43
  • Looks like a 'template handler' might be the more idiomatic terminology for what I'm trying to create. – Dane O'Connor Oct 12 '11 at 04:01
  • It also appears the tilt gem might be useful here. – Dane O'Connor Oct 12 '11 at 04:12

1 Answers1

0

For mustache there are the poirot and stache gem which provide the html server side

grigio
  • 167
  • 11