简体   繁体   English

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

[英]Using mustache templates as partial views in Rails 4?

I'm trying to use mustache views in Rails 4 instead of some old partial views that hosted some HTML templates I am sharing with a javascript application. 我正在尝试在Rails 4中使用小胡子视图,而不是一些旧的部分视图,这些视图承载了与JavaScript应用程序共享的一些HTML模板。

Added config/initializers/mustache.rb 添加了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)

I named my template _template.mustache and put it the template in views/sales/_template.mustache and I can render it just fine from normal .html.erb with render partial: 'template' 我将模板命名为_template.mustache并将其放在views/sales/_template.mustache并且可以使用普通的.html.erb渲染,并且可以使用render partial: 'template'

Where do I put the Template.rb file for it to work with the mustache? 我在哪里将Template.rb文件与胡子配合使用?

class Template < Mustache

  def something
    # return something
    "WOW"
  end

end

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM