Mustach Rails3
If your UI needs to hold state and do a little more then just returning a html string and
updating a dom element(not that cool); then json is the way to go.
Javascript is powerful so it can just receive the json and it can handle the state of the UI.
We wanted templates that worked in both worlds; server side and client side; Based on these
requirements we went for Mustache and Mustache.js We also created a template resolver (super easy in rails3) to get these templates from the db so clients could have there own personal view; but that is another story;
However its not the easiest tool to get going in Rails; So based on that fact
we needed a quick implementation to get us going with Mustache.
Note: This is not the best approach to solve this problem; Mustache probably
needs a custom context that can be compliant in the rails3 in a action_view sense;
This seems to be actually quite hard; from my perspective action_view seems
to be one the most complexing parts in rails and rails3. I personally think that
the context should start from a simple hash and layer the extra functionality on
top in module fashion. But then you are in view helper hell; so in a nut shell I am
not that sure on that one.
What we did
-
Create a template handler to get a handle on the mustache
-
Create custom mustache to handle a context that can take a ActionView::Context;
-
Register the template handler
monicauk — March 15, 2010
Thanks for sharing this interesting and informative article.
Michael Aufreiter — April 22, 2010
Thanks for providing your approach!
I had to change:
mustache_class_name = “#{template.details[:virtual_path]}_view”.classify
to
mustache_class_name = “#{template.virtual_path}_view”.classify
due to this error in rails3.0.0-beta3:
undefined method `details’ for app/views/collections/index.html.mustache:ActionView::Template
Thanks,
Michael
Richard Hooker — April 28, 2010
yeah there has been alot of changes around action_view recently beta3 went from the details hash to a few specific methods; thanks for the heads up though;