如何使 .liquid 文件在 Rails 3 中工作
我正在使用 Rails 3.0.9 。我正在尝试使用液体,但出现错误。
这是我的 Gemfile
source 'http://rubygems.org'
gem 'rails', '3.0.9'
gem 'sqlite3'
gem 'liquid'
这是一个初始化程序
class LiquidView
PROTECTED_ASSIGNS = %w( template_root response _session template_class action_name request_origin session template
_response url _request _cookies variables_added _flash params _headers request cookies
ignore_missing_templates flash _params logger before_filter_chain_aborted headers )
PROTECTED_INSTANCE_VARIABLES = %w( @_request @controller @_first_render @_memoized__pick_template @view_paths
@helpers @assigns_added @template @_render_stack @template_format @assigns )
def self.call(template)
"LiquidView.new(self).render(template, local_assigns)"
end
def initialize(view)
@view = view
end
def render(template, local_assigns = nil)
@view.controller.headers["Content-Type"] ||= 'text/html; charset=utf-8'
# Rails 2.2 Template has source, but not locals
if template.respond_to?(:source) && !template.respond_to?(:locals)
assigns = (@view.instance_variables - PROTECTED_INSTANCE_VARIABLES).inject({}) do |hash, ivar|
hash[ivar[1..-1]] = @view.instance_variable_get(ivar)
hash
end
else
assigns = @view.assigns.reject{ |k,v| PROTECTED_ASSIGNS.include?(k) }
end
source = template.respond_to?(:source) ? template.source : template
local_assigns = (template.respond_to?(:locals) ? template.locals : local_assigns) || {}
if content_for_layout = @view.instance_variable_get("@content_for_layout")
assigns['content_for_layout'] = content_for_layout
end
assigns.merge!(local_assigns.stringify_keys)
liquid = Liquid::Template.parse(source)
liquid.render(assigns, :filters => [@view.controller.master_helper_module], :registers => {:action_view => @view, :controller => @view.controller})
end
def compilable?
false
end
end
::ActionView::Template.register_template_handler(:liquid, LiquidView)
这是 app/views/users/index.html.liquid
<h1>Listing users</h1>
<table>
<tr>
<th>Name</th>
</tr>
{% for user in @users %}
<tr> <td> {{ user.name }} </td> </tr>
{% endfor %}
</table>
这是控制器
class UsersController < ApplicationController
def index
@users = User.all
respond_to do |format|
format.html # index.html.erb
end
end
end
我收到此错误。
undefined method `template' for #<UsersController:0x00000100979b80>
Extracted source (around line #1):
1: <h1>Listing users</h1>
2:
3: <table>
4: <tr>
I am using Rails 3.0.9 . I am trying to use liquid and I am getting an error.
Here is my Gemfile
source 'http://rubygems.org'
gem 'rails', '3.0.9'
gem 'sqlite3'
gem 'liquid'
Here is an initializer
class LiquidView
PROTECTED_ASSIGNS = %w( template_root response _session template_class action_name request_origin session template
_response url _request _cookies variables_added _flash params _headers request cookies
ignore_missing_templates flash _params logger before_filter_chain_aborted headers )
PROTECTED_INSTANCE_VARIABLES = %w( @_request @controller @_first_render @_memoized__pick_template @view_paths
@helpers @assigns_added @template @_render_stack @template_format @assigns )
def self.call(template)
"LiquidView.new(self).render(template, local_assigns)"
end
def initialize(view)
@view = view
end
def render(template, local_assigns = nil)
@view.controller.headers["Content-Type"] ||= 'text/html; charset=utf-8'
# Rails 2.2 Template has source, but not locals
if template.respond_to?(:source) && !template.respond_to?(:locals)
assigns = (@view.instance_variables - PROTECTED_INSTANCE_VARIABLES).inject({}) do |hash, ivar|
hash[ivar[1..-1]] = @view.instance_variable_get(ivar)
hash
end
else
assigns = @view.assigns.reject{ |k,v| PROTECTED_ASSIGNS.include?(k) }
end
source = template.respond_to?(:source) ? template.source : template
local_assigns = (template.respond_to?(:locals) ? template.locals : local_assigns) || {}
if content_for_layout = @view.instance_variable_get("@content_for_layout")
assigns['content_for_layout'] = content_for_layout
end
assigns.merge!(local_assigns.stringify_keys)
liquid = Liquid::Template.parse(source)
liquid.render(assigns, :filters => [@view.controller.master_helper_module], :registers => {:action_view => @view, :controller => @view.controller})
end
def compilable?
false
end
end
::ActionView::Template.register_template_handler(:liquid, LiquidView)
Here is app/views/users/index.html.liquid
<h1>Listing users</h1>
<table>
<tr>
<th>Name</th>
</tr>
{% for user in @users %}
<tr> <td> {{ user.name }} </td> </tr>
{% endfor %}
</table>
And here is controller
class UsersController < ApplicationController
def index
@users = User.all
respond_to do |format|
format.html # index.html.erb
end
end
end
I am getting this error.
undefined method `template' for #<UsersController:0x00000100979b80>
Extracted source (around line #1):
1: <h1>Listing users</h1>
2:
3: <table>
4: <tr>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这应该对您有帮助,只需使用下面的代码更改初始值设定项
,只是不要忘记将 Liquid_methods [,:column_name] 添加到您的模型中
I think this should help you, just change initializer with code below
just don't forget to put liquid_methods [,:column_name] to your model