Rails 3.1 模板处理程序

发布于 2024-11-28 22:57:08 字数 952 浏览 1 评论 0原文

我有一个 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

在上面的处理程序代码中,我传递了模板,它是 ActionView::Template 的实例。但我不确定如何获取视图上下文,其中应该包括当地人等

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

I have a ruby gem, poirot, which enables the use of mustache templates in Rails. The template handler I have was extending from ActionView::Template::Handler, however this appears to be deprecated in Rails 3.1.

I have re-factored the handler to comply with the deprecation warnings. In doing this I am now unable to pass locals, or the view context, to the template. I can't seem to find out how to get this working with 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

In my code above for the handler I get passed the template, which is an instance of ActionView::Template. But I'm not sure how to get the view context, which should include the locals etc

Can someone point me in the right direction?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

所谓喜欢 2024-12-05 22:57:08

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

在我的视图类中,我设法通过执行以下操作来访问本地变量:

locals = view_context.send(:view_renderer).send(:_partial_renderer).instance_variable_get("@locals") || {}

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

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

Okay I have a solution, I'm not sure it is the best, it feels a little hacky to me!

In my view class I have managed to get access to the locals by doing the following:

locals = view_context.send(:view_renderer).send(:_partial_renderer).instance_variable_get("@locals") || {}

This feels a little messy as both view_renderer and _partial_renderer are private, and there is no proper accessor to the locals ivar.

I'm still hoping there is a better way to do this!

雨巷深深 2024-12-05 22:57:08

我花了大约 4 个小时研究源代码来找到解决方案,现在看起来非常简单:

只需在您想要评估并使用它的地方添加“local_assigns”即可。

例如:

"#{view_class}.new(self, '#{template.source.gsub(/'/, "\\\\'")}', local_assigns).render.html_safe"

该字符串将在模块上下文中进行评估 - ActionView::CompiledTemplateslocal_assigns 将在那里进行访问。

I spent about 4 hours investigating source code to find a solution, and now it's seems very simple:

just add "local_assigns" where you are want to eval it and use.

For example:

"#{view_class}.new(self, '#{template.source.gsub(/'/, "\\\\'")}', local_assigns).render.html_safe"

this string will be evaluted inside the module context - ActionView::CompiledTemplates and local_assigns will be accessible there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文