在Rails 4中使用胡子模板作为部分视图?

时间:2017-07-04 11:55:45

标签: ruby-on-rails ruby-on-rails-4 mustache

我试图在Rails 4中使用小胡子视图,而不是托管我与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

1 个答案:

答案 0 :(得分:0)

因此,您可以将此变量直接写入您的控制器。