#295 Sharing Mustache Templates pro
Mustache is a simple, logic-less templating language which allows you to use the same template for both Ruby and JavaScript. See how to set it up with a custom template handler in this episode.
- Download:
- source code
- mp4
- m4v
- webm
- ogv
Really interesting. I sometimes used Poirot for this purpose(https://github.com/olivernn/poirot).
Just a notice: isn't "try" a perf killer? You use it in your product helper. (Well just did some benchmark, seems not to be that a problem)
Thanks Ryan.
For those who wants to try out Handlebars, you might want to take a look at
https://github.com/leshill/handlebars_assets
It compiles your handlebars templates with the rest of your assets. Pretty neat
BTW, you can use jQuery waypoints to deal with scrolling.
Another thing is that I would probably use Spine.js or similar for infinite scrolling: http://vimeo.com/30796067
I also wonder how you would reuse the template with Rails 3.1 assets pipeline and JST?
+1 - Handlebars + handlebars_assets gem does this but the templates need to be required into application.js. Would be good if they were automatically compiled from your view directory
Twitter has a fork of mustache.js that at one time, at least, was much more robust than the original. I'm no sure if that's still the case. It's worth knowing about, though.
Thanks. Interesting cast.
Personally, I'd go one step further and completely ditch the initial rendering of the page. Then you can do all the rendering on the client side. The nice thing about that is you don't need to do the dual-nature templates. You drop support for clients without Javascript, but depending on your site, this may be OK.
I really like this screencast but am having problems implementing this because I use haml, besides telling me to switch to erb, is there any advice you can provide about implementation of this with haml. I have googled and there seems to be some suggestions that there are problems with haml, specifically with respect to this issue here https://github.com/nex3/haml/issues/450.
Found a way to get this working using the stache gem.
I have also tried the
stache
gem but couldn't make it work with haml. I'm now using an answer on StackOverflow to include haml in the template handler but it doesn't work with chained template extensions (e.g._foo.html.haml.mustache
).Any ideas? Thanks!
Currently
handlebars_assets
supports haml (as well as slim). See readme for details.Thanks Ryan for wonderful episode!
But I'm not sure that you did correctly by moving
price
andreleased_at
to product.js. It would be better if you made this methods in price.rb model and when you render product to json you would set methodsprice
andreleased_at
to render json format.Thanks Ryan!
3.2 Tip. You should replace
if template.locals.include? :mustache
with
if template.locals.include? 'mustache'
+1
Also +1. I had trouble for a while trying to figure out what I might be doing wrong.
(Thanks!)
Thanks a lot! It was driving me mad
thanks for this
legendary! Wasted 5 hours+ on that!!!
+1
This fixed problems I was having with the template not rendering with the object.
Hi,
I implement the same solution but the mustache handler doesn't appear in the handlers list .
config/initializers/mustache_template_handler.rb
Does anybody had this error before ?
Upgrade to rails 3.2.2.rc1 will fix this problem.
see https://github.com/rails/rails/pull/5101.
Has anyone done this with eco?
<%= raw render "eco_template" %>
Is it right way to use view_context in controllers? It's not caching and recreate every call lib/abstract_controller/rendering.rb#L74-76
Superb cast.
For older Rails version:
---- View
Here is the solution, which allows avoid rendering partial into markup. templates become available directly in javascript SMT (shared mustache template) global object.
https://github.com/railsware/smt_rails
I've done a mashup of the idea here of sharing templates with railscast 325 but changed to use Handlebars instead of Mustache.
Check it out:
https://github.com/etewiah/sharing_handlebars_templates_in_backbone
I am having a problem though with using exactly the same template on both the client and the server. I explain better in the README. Any comments will be appreciated.
Thanks
I'm confused on what the benefit of using mustache over an js.erb. Is it better because the view is render almost completely on the client side?
I didn't see this episode, but i'll bet that https://github.com/railsware/sht_rails and https://github.com/railsware/smt_rails much interesting solutions.
For people following along with the code in this Railscast, the curl command has changed. Github redirects the 'raw.github.com' to 'raw.githubusercontent.com' now but the curl command doesn't follow the redirect.
Or if you don't trust me (you shouldn't), then tell curl to follow redirects with the -L option and use Ryan's curl command instead:
hello all, with rails 4, I could not pass the locals variable to the template handler : only the hask key was present in the strings array named template.locals
I created a rails github issue for that :
https://github.com/rails/rails/issues/19204