Rails 3.1 模板处理程序
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我有一个解决方案,我不确定它是最好的,对我来说感觉有点老套!
在我的视图类中,我设法通过执行以下操作来访问本地变量:
这感觉有点混乱,因为 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:
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!
我花了大约 4 个小时研究源代码来找到解决方案,现在看起来非常简单:
只需在您想要评估并使用它的地方添加“local_assigns”即可。
例如:
"#{view_class}.new(self, '#{template.source.gsub(/'/, "\\\\'")}', local_assigns).render.html_safe"
该字符串将在模块上下文中进行评估 -
ActionView::CompiledTemplates
和local_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
andlocal_assigns
will be accessible there.