[英]What's the simplest way to use mustache view templates in rails?
I could do: 我可以做:
render :text => Mustache.render(view_template_in_a_string, object_hash)
in my controller, but it seems more conventional to put view_template_in_a_string in it's own viewname.mustache.html file under views/controllername/action.mustache.html like I would with action.html.erb 在我的控制器中,但按照惯例,将view_template_in_a_string放在它自己的viewname.mustache.html文件中views / controllername / action.mustache.html下,就像使用action.html.erb一样
currently I use 目前我使用
gem 'mustache'
for my mustache needs 满足我的胡子需求
How can I use mustache views like I would with erb 如何像使用erb一样使用小胡子视图
I understand that mustache is logic-less, I don't need logic in my views 我知道小胡子没逻辑,我不需要逻辑
my current hack: 我目前的骇客:
# controllers/thing_controller.rb
def some_action
hash = {:name => 'a name!!'}
vw = File.read('./app/views/'+params[:controller]+'/'+params[:action]+'.html.mustache') || ""
render :text => Mustache.render(vw, hash), :layout => true
end
An updated answer since the original solution, mustache-rails, ceases to exist. 由于原始解决方案“胡须栏杆”不复存在,因此有了更新的答案。
The gem is: 宝石是:
https://github.com/agoragames/stache https://github.com/agoragames/stache
And a redundant but simple example to address how to use stache in our rails project, we'll make the beginning of a Noises Demo App. 还有一个多余但简单的示例来解决如何在Rails项目中使用stache,我们将开始创建Noises Demo App。
To start we'll add both the mustache
and the stache
gem into our Gemfile and run bundle install
. 首先,我们将
mustache
和stache
宝石都添加到我们的Gemfile中,然后运行bundle install
。
Then in our config/application.rb
we need to tell stache
to use mustache
(the other available option is handlebars
; also, this and other configuration options can be found on their github page ). 然后,在我们的
config/application.rb
我们需要告诉stache
使用mustache
(另一个可用的选项是handlebars
;此外,此配置选项和其他配置选项可以在其github页面上找到)。
Stache.configure do |config|
config.use :mustache
end
Here's the sample directory structure with a few example files for our app: 这是示例目录结构,其中包含我们应用程序的一些示例文件:
app/
controllers/
noises_controller.rb
models/
noise.rb
templates/
noises/
animal.mustache
views/
noises/
animal.rb
In controllers/noises_controller.rb
在
controllers/noises_controller.rb
class NoisesController < ApplicationController
def animal
end
end
In models/noise.rb
在
models/noise.rb
class Noise < ActiveRecord::Base
def self.dog
"ar roof"
end
def self.sheep
"baa"
end
def self.bullfrog
"borborygmus"
end
end
In templates/noises/animal.mustache
在
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>
In views/noises/animal.rb
在
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
Hopefully this clarifies an example of how someone can use stache
and mustache
in a rails application to serve the correct view templates. 希望这可以阐明一个示例,说明有人如何在Rails应用程序中使用
stache
和mustache
提供正确的视图模板。
Just use this gem: 只需使用以下宝石:
https://github.com/josh/mustache-rails https://github.com/josh/mustache-rails
That way you can easily configure your rails application to serve the correct view templates, so that you no longer need your hack. 这样,您可以轻松地配置Rails应用程序以提供正确的视图模板,从而不再需要黑客。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.