2009-04-23 18:58:38 -04:00
|
|
|
# encoding: utf-8
|
|
|
|
# This is so that templates compiled in this file are UTF-8
|
|
|
|
|
2009-04-22 19:41:06 -04:00
|
|
|
require 'set'
|
2009-04-14 20:22:51 -04:00
|
|
|
require "action_view/template/path"
|
|
|
|
|
2009-04-23 18:58:38 -04:00
|
|
|
module ActionView
|
2008-07-12 15:33:46 -04:00
|
|
|
class Template
|
2008-07-03 22:08:27 -04:00
|
|
|
extend TemplateHandlers
|
2009-05-22 18:16:28 -04:00
|
|
|
attr_reader :source, :identifier, :handler, :mime_type, :details
|
2009-04-14 20:22:51 -04:00
|
|
|
|
2009-04-23 18:58:38 -04:00
|
|
|
def initialize(source, identifier, handler, details)
|
|
|
|
@source = source
|
|
|
|
@identifier = identifier
|
|
|
|
@handler = handler
|
|
|
|
@details = details
|
2009-05-21 17:22:07 -04:00
|
|
|
|
2009-05-22 19:50:26 -04:00
|
|
|
format = details.delete(:format) || begin
|
2009-05-21 17:22:07 -04:00
|
|
|
# TODO: Clean this up
|
|
|
|
handler.respond_to?(:default_format) ? handler.default_format.to_sym.to_s : "html"
|
|
|
|
end
|
|
|
|
@mime_type = Mime::Type.lookup_by_extension(format.to_s)
|
2009-05-22 19:50:26 -04:00
|
|
|
@details[:formats] = Array.wrap(format && format.to_sym)
|
2009-04-14 20:22:51 -04:00
|
|
|
end
|
|
|
|
|
2009-04-23 18:58:38 -04:00
|
|
|
def render(view, locals, &blk)
|
|
|
|
method_name = compile(locals, view)
|
|
|
|
view.send(method_name, locals, &blk)
|
|
|
|
end
|
|
|
|
|
|
|
|
# TODO: Figure out how to abstract this
|
|
|
|
def variable_name
|
|
|
|
identifier[%r'_?(\w+)(\.\w+)*$', 1].to_sym
|
2008-12-21 18:23:53 -05:00
|
|
|
end
|
|
|
|
|
2009-04-23 18:58:38 -04:00
|
|
|
# TODO: Figure out how to abstract this
|
|
|
|
def counter_name
|
|
|
|
"#{variable_name}_counter".to_sym
|
|
|
|
end
|
|
|
|
|
|
|
|
# TODO: kill hax
|
|
|
|
def partial?
|
|
|
|
@details[:partial]
|
|
|
|
end
|
2009-05-21 17:22:07 -04:00
|
|
|
|
2009-04-23 18:58:38 -04:00
|
|
|
private
|
2008-02-05 23:26:40 -05:00
|
|
|
|
2009-04-23 18:58:38 -04:00
|
|
|
def compile(locals, view)
|
|
|
|
method_name = build_method_name(locals)
|
|
|
|
|
|
|
|
return method_name if view.respond_to?(method_name)
|
|
|
|
|
|
|
|
locals_code = locals.keys.map! { |key| "#{key} = local_assigns[:#{key}];" }.join
|
|
|
|
|
2009-05-28 18:12:28 -04:00
|
|
|
code = @handler.call(self)
|
|
|
|
encoding_comment = $1 if code.sub!(/\A(#.*coding.*)\n/, '')
|
|
|
|
|
2009-04-23 18:58:38 -04:00
|
|
|
source = <<-end_src
|
|
|
|
def #{method_name}(local_assigns)
|
2009-05-28 18:12:28 -04:00
|
|
|
old_output_buffer = output_buffer;#{locals_code};#{code}
|
2009-04-23 18:58:38 -04:00
|
|
|
ensure
|
|
|
|
self.output_buffer = old_output_buffer
|
|
|
|
end
|
|
|
|
end_src
|
|
|
|
|
2009-05-28 18:12:28 -04:00
|
|
|
if encoding_comment
|
|
|
|
source = "#{encoding_comment}\n#{source}"
|
|
|
|
line = -1
|
|
|
|
else
|
|
|
|
line = 0
|
|
|
|
end
|
|
|
|
|
2009-04-23 18:58:38 -04:00
|
|
|
begin
|
2009-05-28 18:12:28 -04:00
|
|
|
ActionView::Base::CompiledTemplates.module_eval(source, identifier, line)
|
2009-04-23 18:58:38 -04:00
|
|
|
method_name
|
|
|
|
rescue Exception => e # errors from template code
|
|
|
|
if logger = (view && view.logger)
|
|
|
|
logger.debug "ERROR: compiling #{method_name} RAISED #{e}"
|
|
|
|
logger.debug "Function body: #{source}"
|
|
|
|
logger.debug "Backtrace: #{e.backtrace.join("\n")}"
|
|
|
|
end
|
2008-07-12 15:33:46 -04:00
|
|
|
|
2009-04-23 18:58:38 -04:00
|
|
|
raise ActionView::TemplateError.new(self, {}, e)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_method_name(locals)
|
|
|
|
# TODO: is locals.keys.hash reliably the same?
|
|
|
|
"_render_template_#{@identifier.hash}_#{__id__}_#{locals.keys.hash}".gsub('-', "_")
|
2008-07-12 15:33:46 -04:00
|
|
|
end
|
2009-04-23 18:58:38 -04:00
|
|
|
end
|
2009-05-28 18:12:28 -04:00
|
|
|
end
|