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



内容概要:本文档详细介绍了基于MATLAB实现多目标差分进化(MODE)算法进行无人机三维路径规划的项目实例。项目旨在提升无人机在复杂三维环境中路径规划的精度、实时性、多目标协调处理能力、障碍物避让能力和路径平滑性。通过引入多目标差分进化算法,项目解决了传统路径规划算法在动态环境和多目标优化中的不足,实现了路径长度、飞行安全距离、能耗等多个目标的协调优化。文档涵盖了环境建模、路径编码、多目标优化策略、障碍物检测与避让、路径平滑处理等关键技术模块,并提供了部分MATLAB代码示例。 适合人群:具备一定编程基础,对无人机路径规划和多目标优化算法感兴趣的科研人员、工程师和研究生。 使用场景及目标:①适用于无人机在军事侦察、环境监测、灾害救援、物流运输、城市管理等领域的三维路径规划;②通过多目标差分进化算法,优化路径长度、飞行安全距离、能耗等多目标,提升无人机任务执行效率和安全性;③解决动态环境变化、实时路径调整和复杂障碍物避让等问题。 其他说明:项目采用模块化设计,便于集成不同的优化目标和动态环境因素,支持后续算法升级与功能扩展。通过系统实现和仿真实验验证,项目不仅提升了理论研究的实用价值,还为无人机智能自主飞行提供了技术基础。文档提供了详细的代码示例,有助于读者深入理解和实践该项目。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值