Rails源代码分析(42):ActionView Base


贴代码:
  1.     module CompiledTemplates #:nodoc:
  2.       # holds compiled template code
  3.     end
  4.     include CompiledTemplates
  5.     # Maps inline templates to their method names
  6.     cattr_accessor :method_names
  7.     @@method_names = {}
  8.     # Map method names to the names passed in local assigns so far
  9.     @@template_args = {}
  10.     # Cache public asset paths
  11.     cattr_reader :computed_public_paths
  12.     @@computed_public_paths = {}
  13.     class ObjectWrapper < Struct.new(:value#:nodoc:
  14.     end
  15.     def self.helper_modules # 这个类方法可以获得所有的rails自带的helper module的Module名
  16.       helpers = []
  17.       Dir.entries(File.expand_path("#{File.dirname(__FILE__)}/helpers")).sort.each do |file|
  18.         next unless file =~ /^([a-z][a-z_]*_helper).rb$/
  19.         require "action_view/helpers/#{$1}"
  20.         helper_module_name = $1.camelize
  21.         if Helpers.const_defined?(helper_module_name)
  22.           helpers << Helpers.const_get(helper_module_name)
  23.         end
  24.       end
  25.       return helpers
  26.     end
  27.     def initialize(view_paths = [], assigns_for_first_render = {}, controller = nil)#:nodoc:
  28.       @assigns = assigns_for_first_render
  29.       @assigns_added = nil
  30.       @controller = controller
  31.       @finder = TemplateFinder.new(self, view_paths)
  32.     end
  33.     # Renders the template present at <tt>template_path</tt>. If <tt>use_full_path</tt> is set to true, 
  34.     # it's relative to the view_paths array, otherwise it's absolute. The hash in <tt>local_assigns</tt> 
  35.     # is made available as local variables.
  36.     def render_file(template_path, use_full_path = true, local_assigns = {}) #:nodoc:
  37.       if defined?(ActionMailer) && defined?(ActionMailer::Base) && controller.is_a?(ActionMailer::Base) && !template_path.include?("/")
  38.         raise ActionViewError, <<-END_ERROR
  39. Due to changes in ActionMailer, you need to provide the mailer_name along with the template name.
  40.   render "user_mailer/signup"
  41.   render :file => "user_mailer/signup"
  42. If you are rendering a subtemplate, you must now use controller-like partial syntax:
  43.   render :partial => 'signup' # no mailer_name necessary
  44.         END_ERROR
  45.       end
  46.       
  47.       Template.new(self, template_path, use_full_path, local_assigns).render_template
  48.     end
  49.     
  50.     # Renders the template present at <tt>template_path</tt> (relative to the view_paths array). 
  51.     # The hash in <tt>local_assigns</tt> is made available as local variables.
  52.     def render(options = {}, local_assigns = {}, &block) #:nodoc:
  53.       if options.is_a?(String)
  54.         render_file(options, true, local_assigns) # 如果是String,当作render_file处理
  55.       elsif options == :update
  56.         update_page(&block)
  57.       elsif options.is_a?(Hash)
  58.         use_full_path = options[:use_full_path]
  59.         options = options.reverse_merge(:locals => {}, :use_full_path => true)
  60.         if partial_layout = options.delete(:layout)
  61.           if block_given?
  62.             wrap_content_for_layout capture(&block) do 
  63.               concat(render(options.merge(:partial => partial_layout)), block.binding)
  64.             end
  65.           else
  66.             wrap_content_for_layout render(options) do
  67.               render(options.merge(:partial => partial_layout))
  68.             end
  69.           end
  70.         elsif options[:file]
  71.           render_file(options[:file], use_full_path || false, options[:locals])
  72.         elsif options[:partial] && options[:collection]
  73.           render_partial_collection(options[:partial], options[:collection], options[:spacer_template], options[:locals])
  74.         elsif options[:partial]
  75.           render_partial(options[:partial], ActionView::Base::ObjectWrapper.new(options[:object]), options[:locals])
  76.         elsif options[:inline]
  77.           template = InlineTemplate.new(self, options[:inline], options[:locals], options[:type])
  78.           render_template(template)
  79.         end
  80.       end
  81.     end
  82.     def render_template(template) #:nodoc:
  83.       template.render_template
  84.     end
  85.     # Returns true is the file may be rendered implicitly.
  86.     def file_public?(template_path)#:nodoc:
  87.       template_path.split('/').last[0,1] != '_'
  88.     end
  89.     # Returns a symbolized version of the <tt>:format</tt> parameter of the request,
  90.     # or <tt>:html</tt> by default.
  91.     #
  92.     # EXCEPTION: If the <tt>:format</tt> parameter is not set, the Accept header will be examined for
  93.     # whether it contains the JavaScript mime type as its first priority. If that's the case,
  94.     # it will be used. This ensures that Ajax applications can use the same URL to support both
  95.     # JavaScript and non-JavaScript users.
  96.     def template_format # 从request中得到template格式
  97.       return @template_format if @template_format
  98.       if controller && controller.respond_to?(:request)
  99.         parameter_format = controller.request.parameters[:format]
  100.         accept_format    = controller.request.accepts.first
  101.         case
  102.         when parameter_format.blank? && accept_format != :js
  103.           @template_format = :html
  104.         when parameter_format.blank? && accept_format == :js
  105.           @template_format = :js
  106.         else
  107.           @template_format = parameter_format.to_sym
  108.         end
  109.       else
  110.         @template_format = :html
  111.       end
  112.     end
  113.     private
  114.       def wrap_content_for_layout(content)
  115.         original_content_for_layout = @content_for_layout
  116.         @content_for_layout = content
  117.         returning(yield) { @content_for_layout = original_content_for_layout }
  118.       end
  119.       # Evaluate the local assigns and pushes them to the view.
  120.       def evaluate_assigns # 这个方法在template里会调用、在controller里render :update的时候调用
  121.         unless @assigns_added
  122.           assign_variables_from_controller
  123.           @assigns_added = true
  124.         end
  125.       end
  126.       # Assigns instance variables from the controller to the view.
  127.       def assign_variables_from_controller # 把controller的实例变量定义复制到view里
  128.         @assigns.each { |key, value| instance_variable_set("@#{key}", value) }
  129.       end
  130.       
  131.       def execute(template) # 执行template生成页面
  132.         send(template.method, template.locals) do |*names|
  133.           instance_variable_get "@content_for_#{names.first || 'layout'}"
  134.         end        
  135.       end



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值