Shared Mustache Templates for Rails 3

Hello my dear friends. Today we will talk about how we share mustache templates in Rails 3.Let’s imagine that we have a task, where on first load of the page we show only 10 products. But when user scroll, we should automatically load more products on page (aka continuous pagination). Of course, this task can be solved in several ways, but I want solve this task by sharing one template between Rails and JavaScript.There are many JavaScripts templates exists today – Hogan.js, Handlebars, Mustache etc. I choose Mustache, because it’s simple and has template engine implemented on many languages (we need JavaScript and Ruby). We created a gem for such type of tasks – smt_rails. How it works?First of all you have to add this gem to your Gemfile and start ‘bundle install’:Next launch generator:That’s all.Now you can create a directory in ‘app/templates’ mustache templates (these templates ends with ‘.mustache’ by default). Let’s create partial for the product:File: ‘app/templates/products/_product.mustacheContent:You can render this partial in ActiveView:And in JavaScript:So in the end you’ll have only one template, which is shared between Rails and JavaScript.Demo application with scroll pagination you can find hereSource code: https://github.com/le0pard/st_rails_exampleThat’s all folks!SMT_rails (Shared Mustache Templates for Rails)Rubygems: https://rubygems.org/gems/smt_railsSource code: https://github.com/railsware/smt_rails