Rails源码研究之ActionView:二,partials

partials是Rails模板重用的一项重要技术,让我们来读读partials.rb源码文件:
module ActionView
  module Partials

    def render_partial(partial_path, local_assigns = nil, deprecated_local_assigns = nil)
      path, partial_name = partial_pieces(partial_path)
      object = extracting_object(partial_name, local_assigns, deprecated_local_assigns)
      local_assigns = extract_local_assigns(local_assigns, deprecated_local_assigns)
      local_assigns = local_assigns ? local_assigns.clone : {}
      add_counter_to_local_assigns!(partial_name, local_assigns)
      add_object_to_local_assigns!(partial_name, local_assigns, object)
      if logger
        ActionController::Base.benchmark("Rendered #{path}/_#{partial_name}", Logger::DEBUG, false) do
          render("#{path}/_#{partial_name}", local_assigns)
        end
      else
        render("#{path}/_#{partial_name}", local_assigns)
      end
    end

    def render_partial_collection(partial_name, collection, partial_spacer_template = nil, local_assigns = nil)
      collection_of_partials = Array.new
      counter_name = partial_counter_name(partial_name)
      local_assigns = local_assigns ? local_assigns.clone : {}
      collection.each_with_index do |element, counter|
        local_assigns[counter_name] = counter
        collection_of_partials.push(render_partial(partial_name, element, local_assigns))
      end
      return " " if collection_of_partials.empty?
      if partial_spacer_template
        spacer_path, spacer_name = partial_pieces(partial_spacer_template)
        collection_of_partials.join(render("#{spacer_path}/_#{spacer_name}"))
      else
        collection_of_partials.join
      end
    end

    private

      def extract_local_assigns(local_assigns, deprecated_local_assigns)
        local_assigns.is_a?(Hash) ? local_assigns : deprecated_local_assigns
      end

      def add_counter_to_local_assigns!(partial_name, local_assigns)
        counter_name = partial_counter_name(partial_name)
        local_assigns[counter_name] = 1 unless local_assigns.has_key?(counter_name)
      end

      def add_object_to_local_assigns!(partial_name, local_assigns, object)
        local_assigns[partial_name.intern] ||=
          if object.is_a?(ActionView::Base::ObjectWrapper)
            object.value
          else
            object
          end || controller.instance_variable_get("@#{partial_name}")
      end
  end
end

主要使用的方法就是public的render_partial和render_partial_collection,代码非常清楚
比较吸引眼球的地方是add_object_to_local_assigns!的方法实现, ||= ||这种连续||的方式还是第一次见到
下面是使用例子:
1, common render partials
在Advertiser下面的某个rhtml模板里面:
<%= render :partial => "account", :locals => { :account => @buyer } %>

<% for ad in @advertisements %>
  <%= render :partial => "ad", :locals => { :ad => ad } %>
<% end %>

上面的代码将渲染advertiser/_account.rhtml和advertiser/_ad.rhtml,:locals为变量Hash

2,render partials collection
上面我们用for语句来render多个相同的partials,其实有更简洁的方法:
<%= render :partial => "ad", :collection => @advertisements %>


3,render shared partials
给:partial参数的模板文件加上包名路径,这样可以在任何controller和模板里使用
<%= render :partial => "advertisement/ad", :locals => { :ad => @advertisement } %>

这样我们可以在除了AdvertisementController及相应模板之外的地方也使用advertisement/_ad.rhtml

你可能感兴趣的:(Rails)