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).
How it works?
First of all you have to add this gem to your Gemfile and start ‘bundle install':
gem 'smt_rails', git: 'git://github.com/railsware/smt_rails.git'
Next launch generator:
rails g smt_rails:install
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:
You can render this partial in ActiveView:
<%= render "products/product", :mustache => product.as_json %>
var product = … ; come from ajax call as json
var content = SMT['products/product'](product);
Demo application with scroll pagination you can find here
Source code: https://github.com/le0pard/st_rails_example
That’s all folks!
SMT_rails (Shared Mustache Templates for Rails)
Source code: https://github.com/railsware/smt_rails