【问题标题】:Using mustache templates as partial views in Rails 4?在 Rails 4 中使用 mustache 模板作为局部视图?
【发布时间】: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.erbrender partial: 'template' 渲染得很好

我应该将Template.rb 文件放在哪里才能让它与小胡子一起使用?

class Template < Mustache

  def something
    # return something
    "WOW"
  end

end

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-14
      • 2013-06-18
      • 1970-01-01
      相关资源
      最近更新 更多