本文介绍了如何在Rails 3中使.liquid文件工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Rails 3.0.9.我正在尝试使用液体,但出现错误.

I am using Rails 3.0.9 . I am trying to use liquid and I am getting an error.

这是我的宝石文件

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

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>

这是控制器

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 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

别忘了将liquid_methods [,:column_name]放入模型中

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

这篇关于如何在Rails 3中使.liquid文件工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 21:32