2008-07-03 14:06:00 -04:00
|
|
|
module ActionView
|
2008-09-08 22:57:32 -04:00
|
|
|
# NOTE: The template that this mixin is being included into is frozen
|
|
|
|
# so you cannot set or modify any instance variables
|
|
|
|
module Renderable #:nodoc:
|
2008-07-18 16:32:28 -04:00
|
|
|
extend ActiveSupport::Memoizable
|
|
|
|
|
2008-07-16 09:26:23 -04:00
|
|
|
def filename
|
|
|
|
'compiled-template'
|
|
|
|
end
|
|
|
|
|
2008-07-12 15:33:46 -04:00
|
|
|
def handler
|
2008-07-14 20:51:43 -04:00
|
|
|
Template.handler_class_for_extension(extension)
|
2008-07-12 15:33:46 -04:00
|
|
|
end
|
2008-07-14 21:02:59 -04:00
|
|
|
memoize :handler
|
2008-07-03 14:06:00 -04:00
|
|
|
|
2008-07-12 15:33:46 -04:00
|
|
|
def compiled_source
|
2008-07-18 17:00:20 -04:00
|
|
|
handler.call(self)
|
2008-07-14 18:40:03 -04:00
|
|
|
end
|
2008-07-14 21:02:59 -04:00
|
|
|
memoize :compiled_source
|
2008-07-14 18:40:03 -04:00
|
|
|
|
2008-12-21 18:23:53 -05:00
|
|
|
def method_name_without_locals
|
|
|
|
['_run', extension, method_segment].compact.join('_')
|
|
|
|
end
|
|
|
|
memoize :method_name_without_locals
|
|
|
|
|
2008-08-08 02:43:12 -04:00
|
|
|
def method_name(local_assigns)
|
2008-07-12 15:33:46 -04:00
|
|
|
if local_assigns && local_assigns.any?
|
2008-12-21 18:23:53 -05:00
|
|
|
method_name = method_name_without_locals.dup
|
|
|
|
method_name << "_locals_#{local_assigns.keys.map { |k| k.to_s }.sort.join('_')}"
|
|
|
|
else
|
|
|
|
method_name = method_name_without_locals
|
2008-07-12 15:33:46 -04:00
|
|
|
end
|
2008-12-21 18:23:53 -05:00
|
|
|
method_name.to_sym
|
2008-07-05 17:27:43 -04:00
|
|
|
end
|
|
|
|
|
2009-01-22 17:18:10 -05:00
|
|
|
# Compile and evaluate the template's code (if necessary)
|
|
|
|
def compile(local_assigns)
|
|
|
|
render_symbol = method_name(local_assigns)
|
2008-07-03 14:06:00 -04:00
|
|
|
|
2009-01-22 17:18:10 -05:00
|
|
|
if !Base::CompiledTemplates.method_defined?(render_symbol) || recompile?
|
|
|
|
compile!(render_symbol, local_assigns)
|
2008-07-16 09:26:23 -04:00
|
|
|
end
|
2009-01-22 17:18:10 -05:00
|
|
|
end
|
2008-07-12 15:33:46 -04:00
|
|
|
|
2009-01-22 17:18:10 -05:00
|
|
|
private
|
2008-07-16 09:26:23 -04:00
|
|
|
def compile!(render_symbol, local_assigns)
|
|
|
|
locals_code = local_assigns.keys.map { |key| "#{key} = local_assigns[:#{key}];" }.join
|
2008-07-12 15:33:46 -04:00
|
|
|
|
2008-07-16 09:26:23 -04:00
|
|
|
source = <<-end_src
|
|
|
|
def #{render_symbol}(local_assigns)
|
|
|
|
old_output_buffer = output_buffer;#{locals_code};#{compiled_source}
|
|
|
|
ensure
|
|
|
|
self.output_buffer = old_output_buffer
|
|
|
|
end
|
|
|
|
end_src
|
2008-07-03 14:06:00 -04:00
|
|
|
|
2008-07-16 09:26:23 -04:00
|
|
|
begin
|
|
|
|
ActionView::Base::CompiledTemplates.module_eval(source, filename, 0)
|
|
|
|
rescue Exception => e # errors from template code
|
2008-09-09 23:10:39 -04:00
|
|
|
if logger = defined?(ActionController) && Base.logger
|
2008-07-16 09:26:23 -04:00
|
|
|
logger.debug "ERROR: compiling #{render_symbol} RAISED #{e}"
|
|
|
|
logger.debug "Function body: #{source}"
|
|
|
|
logger.debug "Backtrace: #{e.backtrace.join("\n")}"
|
2008-07-12 15:33:46 -04:00
|
|
|
end
|
2008-07-16 09:26:23 -04:00
|
|
|
|
|
|
|
raise ActionView::TemplateError.new(self, {}, e)
|
2008-07-03 14:06:00 -04:00
|
|
|
end
|
|
|
|
end
|
2008-07-06 02:13:15 -04:00
|
|
|
|
2008-12-29 20:27:19 -05:00
|
|
|
def recompile?
|
|
|
|
false
|
2008-07-06 02:13:15 -04:00
|
|
|
end
|
2008-07-03 14:06:00 -04:00
|
|
|
end
|
|
|
|
end
|