【发布时间】:2017-07-04 11:55:44
【问题描述】:
我正在尝试在 Rails 4 中使用 mustache 视图,而不是一些旧的部分视图,这些视图托管了我与 javascript 应用程序共享的一些 HTML 模板。
添加了 config/initializers/mustache.rb
# Tell Rails how to render mustache templates
module MustacheTemplateHandler
def self.call(template)
#assigns contains all the instance_variables defined on the controller's view method
"Mustache.render(#{template.source.inspect}, assigns).html_safe"
end
end
# Register a mustache handler in the Rails template engine
ActionView::Template.register_template_handler(:mustache, MustacheTemplateHandler)
我将我的模板命名为_template.mustache 并将其放在views/sales/_template.mustache 中,我可以将其从普通.html.erb 与render partial: 'template' 渲染得很好
我应该将Template.rb 文件放在哪里才能让它与小胡子一起使用?
class Template < Mustache
def something
# return something
"WOW"
end
end
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 mustache