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-06-17 18:39:41 -04:00
|
|
|
require "action_view/template/resolver"
|
2009-04-14 20:22:51 -04:00
|
|
|
|
2009-04-23 18:58:38 -04:00
|
|
|
module ActionView
|
2008-07-12 15:33:46 -04:00
|
|
|
class Template
|
2009-12-02 23:01:01 -05:00
|
|
|
extend ActiveSupport::Autoload
|
2009-12-22 18:27:37 -05:00
|
|
|
|
|
|
|
eager_autoload do
|
|
|
|
autoload :Error
|
|
|
|
autoload :Handler
|
|
|
|
autoload :Handlers
|
|
|
|
autoload :Text
|
|
|
|
end
|
|
|
|
|
2009-12-02 23:01:01 -05:00
|
|
|
extend Template::Handlers
|
2009-08-09 00:37:03 -04:00
|
|
|
attr_reader :source, :identifier, :handler, :mime_type, :formats, :details
|
2009-09-24 13:30:13 -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-08-09 00:37:03 -04:00
|
|
|
@method_names = {}
|
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-08-09 00:37:03 -04:00
|
|
|
@formats = [format.to_sym]
|
|
|
|
@formats << :html if format == :js
|
2009-06-17 15:00:23 -04:00
|
|
|
@details[:formats] = Array.wrap(format.to_sym)
|
2009-04-14 20:22:51 -04:00
|
|
|
end
|
2009-09-24 13:30:13 -04:00
|
|
|
|
2009-08-11 18:02:05 -04:00
|
|
|
def render(view, locals, &block)
|
2009-12-25 17:37:16 -05:00
|
|
|
method_name = compile(locals, view)
|
|
|
|
view.send(method_name, locals, &block)
|
2009-08-11 18:02:05 -04:00
|
|
|
rescue Exception => e
|
2009-12-02 23:01:01 -05:00
|
|
|
if e.is_a?(Template::Error)
|
2009-08-11 18:02:05 -04:00
|
|
|
e.sub_template_of(self)
|
|
|
|
raise e
|
|
|
|
else
|
2009-12-02 23:01:01 -05:00
|
|
|
raise Template::Error.new(self, view.assigns, e)
|
2009-08-11 18:02:05 -04:00
|
|
|
end
|
2009-04-23 18:58:38 -04:00
|
|
|
end
|
2009-09-24 13:30:13 -04:00
|
|
|
|
2009-04-23 18:58:38 -04:00
|
|
|
# TODO: Figure out how to abstract this
|
|
|
|
def variable_name
|
2009-08-08 22:43:45 -04:00
|
|
|
@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
|
2009-08-08 22:43:45 -04:00
|
|
|
@counter_name ||= "#{variable_name}_counter".to_sym
|
2009-04-23 18:58:38 -04:00
|
|
|
end
|
2009-09-24 13:30:13 -04:00
|
|
|
|
2009-04-23 18:58:38 -04:00
|
|
|
# TODO: kill hax
|
|
|
|
def partial?
|
|
|
|
@details[:partial]
|
|
|
|
end
|
2009-05-21 17:22:07 -04:00
|
|
|
|
2009-09-24 13:30:13 -04:00
|
|
|
def inspect
|
|
|
|
if defined?(Rails.root)
|
|
|
|
identifier.sub("#{Rails.root}/", '')
|
|
|
|
else
|
|
|
|
identifier
|
|
|
|
end
|
|
|
|
end
|
2008-02-05 23:26:40 -05:00
|
|
|
|
2009-09-24 13:30:13 -04:00
|
|
|
private
|
|
|
|
def compile(locals, view)
|
|
|
|
method_name = build_method_name(locals)
|
2009-04-23 18:58:38 -04:00
|
|
|
|
2009-09-24 13:30:13 -04:00
|
|
|
return method_name if view.respond_to?(method_name)
|
2009-05-28 18:12:28 -04:00
|
|
|
|
2009-09-24 13:30:13 -04:00
|
|
|
locals_code = locals.keys.map! { |key| "#{key} = local_assigns[:#{key}];" }.join
|
2009-04-23 18:58:38 -04:00
|
|
|
|
2009-09-24 13:30:13 -04:00
|
|
|
code = @handler.call(self)
|
|
|
|
if code.sub!(/\A(#.*coding.*)\n/, '')
|
|
|
|
encoding_comment = $1
|
|
|
|
elsif defined?(Encoding) && Encoding.respond_to?(:default_external)
|
|
|
|
encoding_comment = "#coding:#{Encoding.default_external}"
|
|
|
|
end
|
2009-05-28 18:12:28 -04:00
|
|
|
|
2009-09-24 13:30:13 -04:00
|
|
|
source = <<-end_src
|
|
|
|
def #{method_name}(local_assigns)
|
|
|
|
old_output_buffer = output_buffer;#{locals_code};#{code}
|
|
|
|
ensure
|
|
|
|
self.output_buffer = old_output_buffer
|
|
|
|
end
|
|
|
|
end_src
|
|
|
|
|
|
|
|
if encoding_comment
|
|
|
|
source = "#{encoding_comment}\n#{source}"
|
|
|
|
line = -1
|
|
|
|
else
|
|
|
|
line = 0
|
2009-04-23 18:58:38 -04:00
|
|
|
end
|
2008-07-12 15:33:46 -04:00
|
|
|
|
2009-09-24 13:30:13 -04:00
|
|
|
begin
|
|
|
|
ActionView::CompiledTemplates.module_eval(source, identifier, line)
|
|
|
|
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
|
|
|
|
|
2009-12-02 23:01:01 -05:00
|
|
|
raise ActionView::Template::Error.new(self, {}, e)
|
2009-09-24 13:30:13 -04:00
|
|
|
end
|
2009-04-23 18:58:38 -04:00
|
|
|
end
|
2009-08-14 15:00:52 -04:00
|
|
|
|
2009-09-24 13:30:13 -04:00
|
|
|
def build_method_name(locals)
|
|
|
|
# TODO: is locals.keys.hash reliably the same?
|
|
|
|
@method_names[locals.keys.hash] ||=
|
|
|
|
"_render_template_#{@identifier.hash}_#{__id__}_#{locals.keys.hash}".gsub('-', "_")
|
|
|
|
end
|
2009-04-23 18:58:38 -04:00
|
|
|
end
|
2009-05-28 18:12:28 -04:00
|
|
|
end
|