N5ken's technical blog
Just another WordPress.com site
Tag Archives: template
Using Mustache in Rails 3
June 22, 2011
Posted by on Mustache is …
So, for binding mustache in your Rails3 project, add the following gems to you Gemfile
gem 'mustache' gem 'mustache_rails3'
You can enable the mustache template handler by running
rails g mustache:install
Then in RAILS_ROOT/config/initializers/mustache.rb
rails g mustache:install # Be sure to install mustache gem and include mustache gem in project Gemfile. # Template Handler require 'mustache_rails' # Generator Rails.application.config.generators.template_engine :mustache # Need to change dynamically when the theme changed Mustache::Rails::Config.template_base_path = Rails.root.join('themes', 'n5ken', 'templates') Mustache::Rails::Config.shared_path = Rails.root.join('themes', 'n5ken', 'templates', 'shared') # You can change these defaults in, say, a Rails initializer or # environment.rb, e.g.: # # Mustache::Rails::Config.template_base_path = Rails.root.join('app', 'templates') module Config def self.template_base_path @template_base_path ||= ::Rails.root.join('app', 'templates') end def self.template_base_path=(value) @template_base_path = value end def self.template_extension @template_extension ||= 'html.mustache' end def self.template_extension=(value) @template_extension = value end def self.shared_path @shared_path ||= ::Rails.root.join('app', 'templates', 'shared') end def self.shared_path=(value) @shared_path = value end end
If we run scaffold to create a product model, we will get the response messages as follow:
$:/var/projects/template# rails g scaffold Product title:string description:text invoke active_record create db/migrate/20110703090715_create_products.rb create app/models/product.rb invoke test_unit create test/unit/product_test.rb create test/fixtures/products.yml route resources :products invoke scaffold_controller create app/controllers/products_controller.rb invoke mustache create app/views/products create app/views/products/index.rb create app/templates/products/index.html.mustache create app/views/products/edit.rb create app/templates/products/edit.html.mustache create app/views/products/show.rb create app/templates/products/show.html.mustache create app/views/products/new.rb create app/templates/products/new.html.mustache create app/templates/products/_form.html.mustache invoke test_unit create test/functional/products_controller_test.rb invoke helper create app/helpers/products_helper.rb invoke test_unit create test/unit/helpers/products_helper_test.rb invoke stylesheets create public/stylesheets/scaffold.css
From the message, it will generate mainly three folders: controller, view, templates.