这里有两个项目,一个是mustache,一个是mustache_rails3,项目地址分别如下:
http://github.com/defunkt/mustache http://github.com/goodmike/mustache_rails3
首先在Gemfile中添加
gem 'mustache' gem 'mustache_rails3'
然后执行
rails g mustache:install
详细用法请看这里:
http://github.com/defunkt/mustache
http://mustache.github.com/mustache.5.html
最后你也可以修改你的模板加载路径,还有注册模板文件类型
# 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')
Rails.root现在返回的是Rails根目录,以数组方式分割文件,所以这里join一下
通过源码(mustache_rails3/lib/generators/mustache/install/templates/lib/mustache_rails.rb)
可以了解我们能够配置什么东西。
# 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