自原始解决方案 mustache-rails 不复存在以来的更新答案。
宝石是:
https://github.com/agoragames/stache
还有一个多余但简单的示例来说明如何在我们的 rails 项目中使用 stache,我们将开始一个 Noises 演示应用程序。
首先,我们将把 themustache
和stache
gem 添加到我们的 Gemfile 中并运行bundle install
。
然后在config/application.rb
我们需要告诉stache
我们使用mustache
(另一个可用的选项是handlebars
;另外,这个和其他配置选项可以在他们的github 页面上找到)。
Stache.configure do |config|
config.use :mustache
end
这是示例目录结构,其中包含我们应用程序的一些示例文件:
app/
controllers/
noises_controller.rb
models/
noise.rb
templates/
noises/
animal.mustache
views/
noises/
animal.rb
在controllers/noises_controller.rb
class NoisesController < ApplicationController
def animal
end
end
在models/noise.rb
class Noise < ActiveRecord::Base
def self.dog
"ar roof"
end
def self.sheep
"baa"
end
def self.bullfrog
"borborygmus"
end
end
在templates/noises/animal.mustache
<p>This is what a dog sounds like: {{ dog }}</p>
<p>This is what a sheep sounds like: {{ sheep }}</p>
<p>And bullfrogs can sound like: {{ bullfrog }}</p>
在views/noises/animal.rb
module Noises
class Animal < Stache::Mustache::View
def dog
Noise.dog
end
def sheep
Noise.sheep
end
def bullfrog
Noise.bullfrog
end
end
end
希望这能阐明一个示例,说明人们如何使用stache
并mustache
在 Rails 应用程序中提供正确的视图模板。