aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/template_handlers
diff options
context:
space:
mode:
authorMichael Koziarski <michael@koziarski.com>2008-01-22 01:46:34 +0000
committerMichael Koziarski <michael@koziarski.com>2008-01-22 01:46:34 +0000
commitc0110a9faa1fc414c960a1c639aa8b121d92ca82 (patch)
tree0e618f0c76ffb3fcb973859977f91d4ee9f7cf6d /actionpack/lib/action_view/template_handlers
parent91de20d6212b5203587d549ce2d63efcc5996eb0 (diff)
downloadrails-c0110a9faa1fc414c960a1c639aa8b121d92ca82.tar.gz
rails-c0110a9faa1fc414c960a1c639aa8b121d92ca82.tar.bz2
rails-c0110a9faa1fc414c960a1c639aa8b121d92ca82.zip
Refactor template compilation from AV::Base into the template handlers. Closes #10888 [lifofifo]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8689 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack/lib/action_view/template_handlers')
-rw-r--r--actionpack/lib/action_view/template_handlers/builder.rb8
-rw-r--r--actionpack/lib/action_view/template_handlers/compilable.rb116
-rw-r--r--actionpack/lib/action_view/template_handlers/erb.rb6
-rw-r--r--actionpack/lib/action_view/template_handlers/rjs.rb6
4 files changed, 123 insertions, 13 deletions
diff --git a/actionpack/lib/action_view/template_handlers/builder.rb b/actionpack/lib/action_view/template_handlers/builder.rb
index 098c90f5c7..cff9e6beb8 100644
--- a/actionpack/lib/action_view/template_handlers/builder.rb
+++ b/actionpack/lib/action_view/template_handlers/builder.rb
@@ -3,18 +3,16 @@ require 'builder'
module ActionView
module TemplateHandlers
class Builder < TemplateHandler
+ include Compilable
+
def self.line_offset
2
end
- def self.compilable?
- true
- end
-
def compile(template)
content_type_handler = (@view.send!(:controller).respond_to?(:response) ? "controller.response" : "controller")
"#{content_type_handler}.content_type ||= Mime::XML\n" +
- "xml = Builder::XmlMarkup.new(:indent => 2)\n" +
+ "xml = ::Builder::XmlMarkup.new(:indent => 2)\n" +
template +
"\nxml.target!\n"
end
diff --git a/actionpack/lib/action_view/template_handlers/compilable.rb b/actionpack/lib/action_view/template_handlers/compilable.rb
new file mode 100644
index 0000000000..2446436530
--- /dev/null
+++ b/actionpack/lib/action_view/template_handlers/compilable.rb
@@ -0,0 +1,116 @@
+module ActionView
+ module TemplateHandlers
+ module Compilable
+
+ def self.included(base)
+ base.extend ClassMethod
+ end
+
+ module ClassMethod
+ # If a handler is mixin this module, set compilable to true
+ def compilable?
+ true
+ end
+ end
+
+ # Compile and evaluate the template's code
+ def compile_template(template, file_name, local_assigns)
+ return unless compile_template?(template, file_name, local_assigns)
+
+ template ||= read_template_file(file_name, nil)
+
+ render_symbol = assign_method_name(template, file_name)
+ render_source = create_template_source(template, render_symbol, local_assigns.keys)
+ line_offset = self.template_args[render_symbol].size + self.line_offset
+
+ begin
+ file_name = 'compiled-template' if file_name.blank?
+ ActionView::Base::CompiledTemplates.module_eval(render_source, file_name, line_offset)
+ rescue Exception => e # errors from template code
+ if @view.logger
+ @view.logger.debug "ERROR: compiling #{render_symbol} RAISED #{e}"
+ @view.logger.debug "Function body: #{render_source}"
+ @view.logger.debug "Backtrace: #{e.backtrace.join("\n")}"
+ end
+
+ raise ActionView::TemplateError.new(@view.finder.extract_base_path_from(file_name) ||
+ @view.finder.view_paths.first, file_name || template, @view.assigns, template, e)
+ end
+
+ self.compile_time[render_symbol] = Time.now
+ # logger.debug "Compiled template #{file_name || template}\n ==> #{render_symbol}" if logger
+ end
+
+ private
+
+ # Method to check whether template compilation is necessary.
+ # The template will be compiled if the inline template or file has not been compiled yet,
+ # if local_assigns has a new key, which isn't supported by the compiled code yet,
+ # or if the file has changed on disk and checking file mods hasn't been disabled.
+ def compile_template?(template, file_name, local_assigns)
+ method_key = file_name || template
+ render_symbol = @view.method_names[method_key]
+
+ compile_time = self.compile_time[render_symbol]
+ if compile_time && supports_local_assigns?(render_symbol, local_assigns)
+ if file_name && !@view.cache_template_loading
+ template_changed_since?(file_name, compile_time)
+ end
+ else
+ true
+ end
+ end
+
+ def assign_method_name(template, file_name)
+ method_key = file_name || template
+ @view.method_names[method_key] ||= compiled_method_name(template, file_name)
+ end
+
+ def compiled_method_name(template, file_name)
+ ['_run', self.class.to_s.demodulize.underscore, compiled_method_name_file_path_segment(file_name)].compact.join('_').to_sym
+ end
+
+ def compiled_method_name_file_path_segment(file_name)
+ if file_name
+ s = File.expand_path(file_name)
+ s.sub!(/^#{Regexp.escape(File.expand_path(RAILS_ROOT))}/, '') if defined?(RAILS_ROOT)
+ s.gsub!(/([^a-zA-Z0-9_])/) { $1.ord }
+ s
+ else
+ (self.inline_template_count += 1).to_s
+ end
+ end
+
+ # Method to create the source code for a given template.
+ def create_template_source(template, render_symbol, locals)
+ body = compile(template)
+
+ self.template_args[render_symbol] ||= {}
+ locals_keys = self.template_args[render_symbol].keys | locals
+ self.template_args[render_symbol] = locals_keys.inject({}) { |h, k| h[k] = true; h }
+
+ locals_code = ""
+ locals_keys.each do |key|
+ locals_code << "#{key} = local_assigns[:#{key}]\n"
+ end
+
+ "def #{render_symbol}(local_assigns)\n#{locals_code}#{body}\nend"
+ end
+
+ # Return true if the given template was compiled for a superset of the keys in local_assigns
+ def supports_local_assigns?(render_symbol, local_assigns)
+ local_assigns.empty? ||
+ ((args = self.template_args[render_symbol]) && local_assigns.all? { |k,_| args.has_key?(k) })
+ end
+
+ # Method to handle checking a whether a template has changed since last compile; isolated so that templates
+ # not stored on the file system can hook and extend appropriately.
+ def template_changed_since?(file_name, compile_time)
+ lstat = File.lstat(file_name)
+ compile_time < lstat.mtime ||
+ (lstat.symlink? && compile_time < File.stat(file_name).mtime)
+ end
+
+ end
+ end
+end \ No newline at end of file
diff --git a/actionpack/lib/action_view/template_handlers/erb.rb b/actionpack/lib/action_view/template_handlers/erb.rb
index ffc1ab0e97..3389c124eb 100644
--- a/actionpack/lib/action_view/template_handlers/erb.rb
+++ b/actionpack/lib/action_view/template_handlers/erb.rb
@@ -22,14 +22,12 @@ end
module ActionView
module TemplateHandlers
class ERB < TemplateHandler
+ include Compilable
+
def compile(template)
::ERB.new(template, nil, @view.erb_trim_mode).src
end
- def self.compilable?
- true
- end
-
def cache_fragment(block, name = {}, options = nil) #:nodoc:
@view.fragment_for(block, name, options) do
eval(ActionView::Base.erb_variable, block.binding)
diff --git a/actionpack/lib/action_view/template_handlers/rjs.rb b/actionpack/lib/action_view/template_handlers/rjs.rb
index e4b3d0c2a6..e0f95205de 100644
--- a/actionpack/lib/action_view/template_handlers/rjs.rb
+++ b/actionpack/lib/action_view/template_handlers/rjs.rb
@@ -1,6 +1,8 @@
module ActionView
module TemplateHandlers
class RJS < TemplateHandler
+ include Compilable
+
def self.line_offset
2
end
@@ -10,10 +12,6 @@ module ActionView
"update_page do |page|\n#{template}\nend"
end
- def self.compilable?
- true
- end
-
def cache_fragment(block, name = {}, options = nil) #:nodoc:
@view.fragment_for(block, name, options) do
begin