【发布时间】:2011-10-25 09:36:51
【问题描述】:
我有一个 ruby gem,poirot,它可以在 Rails 中使用 mustache 模板。我拥有的模板处理程序是从 ActionView::Template::Handler 扩展而来的,但是这似乎在 Rails 3.1 中已被弃用。
我已重构处理程序以符合弃用警告。在这样做时,我现在无法将本地变量或视图上下文传递给模板。我似乎不知道如何在 Rails 3.1 中使用它。
module Poirot
class Handler
attr_reader :template
def initialize(template)
@template = template
end
def self.call(template, *args)
self.new(template).call
end
def call
view_path = "#{template.virtual_path}_view"
abs_view_path = Rails.root.join('app/views', view_path)
view_class = begin
view_path.classify.constantize
rescue NameError => e
Poirot::View
end
"#{view_class}.new(self, '#{template.source.gsub(/'/, "\\\\'")}').render.html_safe"
end
end
end
在我上面的处理程序代码中,我传递了模板,它是 ActionView::Template 的一个实例。但我不确定如何获取视图上下文,其中应该包括本地人等
有人能指出正确的方向吗?
【问题讨论】:
标签: ruby-on-rails ruby actionview mustache