如何使 .liquid 文件在 Rails 3 中工作

发布于 2024-12-11 13:20:06 字数 2913 浏览 0 评论 0原文

我正在使用 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 技术交流群。

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

发布评论

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

评论(1

我要还你自由 2024-12-18 13:20:06

我认为这应该对您有帮助,只需使用下面的代码更改初始值设定项

class LiquidView
  def self.call(template)
    "LiquidView.new(self).render(#{template.source.inspect}, local_assigns)"
  end

  def initialize(view)
    @view = view
  end

  def render(template, local_assigns = {})
    @view.controller.headers["Content-Type"] ||= 'text/html; charset=utf-8'

    assigns = @view.assigns

    if @view.content_for?(:layout)
      assigns["content_for_layout"] = @view.content_for(:layout)
    end
    assigns.merge!(local_assigns.stringify_keys)

    controller = @view.controller
    filters = if controller.respond_to?(:liquid_filters, true)
                controller.send(:liquid_filters)
              elsif controller.respond_to?(:master_helper_module)
                [controller.master_helper_module]
              else
                [controller._helpers]
              end

    liquid = Liquid::Template.parse(template)
    liquid.render(assigns, :filters => filters, :registers => {:action_view => @view, :controller => @view.controller})
  end

  def compilable?
    false
  end
end

ActionView::Template.register_template_handler :liquid, LiquidView

,只是不要忘记将 Liquid_methods [,:column_name] 添加到您的模型中

I think this should help you, just change initializer with code below

class LiquidView
  def self.call(template)
    "LiquidView.new(self).render(#{template.source.inspect}, local_assigns)"
  end

  def initialize(view)
    @view = view
  end

  def render(template, local_assigns = {})
    @view.controller.headers["Content-Type"] ||= 'text/html; charset=utf-8'

    assigns = @view.assigns

    if @view.content_for?(:layout)
      assigns["content_for_layout"] = @view.content_for(:layout)
    end
    assigns.merge!(local_assigns.stringify_keys)

    controller = @view.controller
    filters = if controller.respond_to?(:liquid_filters, true)
                controller.send(:liquid_filters)
              elsif controller.respond_to?(:master_helper_module)
                [controller.master_helper_module]
              else
                [controller._helpers]
              end

    liquid = Liquid::Template.parse(template)
    liquid.render(assigns, :filters => filters, :registers => {:action_view => @view, :controller => @view.controller})
  end

  def compilable?
    false
  end
end

ActionView::Template.register_template_handler :liquid, LiquidView

just don't forget to put liquid_methods [,:column_name] to your model

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