Rails 3.1模板处理程序

Oli*_*ale 5 ruby ruby-on-rails actionview mustache

我有一个ruby gem,poirot,可以在Rails中使用小胡子模板.我的模板处理程序是从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
Run Code Online (Sandbox Code Playgroud)

在我上面的处理程序代码中,我传递了模板,它是ActionView :: Template的一个实例.但我不知道如何获取视图上下文,其中应包括本地等

有人能指出我正确的方向吗?

Oli*_*ale 0

好吧,我有一个解决方案,我不确定它是最好的,对我来说感觉有点老套!

在我的视图类中,我通过执行以下操作成功地访问了当地人:

locals = view_context.send(:view_renderer).send(:_partial_renderer).instance_variable_get("@locals") || {}
Run Code Online (Sandbox Code Playgroud)

这感觉有点混乱,因为 view_renderer 和 _partial_renderer 都是私有的,并且没有适当的本地 ivar 访问器。

我仍然希望有更好的方法来做到这一点!