aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/template
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2009-05-28 17:12:28 -0500
committerJeremy Kemper <jeremy@bitsweat.net>2009-05-28 17:12:48 -0500
commit5ec2c7dc29b36d85b2658465b8a979deb0529d7e (patch)
tree33c3ee6bbd8fbdc53492cd0b6b9fd6ad6121be36 /actionpack/lib/action_view/template
parente23554d79e2db2324144f3ab58f82a895b93fbf5 (diff)
downloadrails-5ec2c7dc29b36d85b2658465b8a979deb0529d7e.tar.gz
rails-5ec2c7dc29b36d85b2658465b8a979deb0529d7e.tar.bz2
rails-5ec2c7dc29b36d85b2658465b8a979deb0529d7e.zip
Ruby 1.9: ERB template encoding using a magic comment at the top of the file
Diffstat (limited to 'actionpack/lib/action_view/template')
-rw-r--r--actionpack/lib/action_view/template/handlers/erb.rb6
-rw-r--r--actionpack/lib/action_view/template/template.rb16
2 files changed, 14 insertions, 8 deletions
diff --git a/actionpack/lib/action_view/template/handlers/erb.rb b/actionpack/lib/action_view/template/handlers/erb.rb
index 95f11d6490..d773df7d29 100644
--- a/actionpack/lib/action_view/template/handlers/erb.rb
+++ b/actionpack/lib/action_view/template/handlers/erb.rb
@@ -16,11 +16,7 @@ module ActionView
self.default_format = Mime::HTML
def compile(template)
- src = ::ERB.new("<% __in_erb_template=true %>#{template.source}", nil, erb_trim_mode, '@output_buffer').src
-
- # Ruby 1.9 prepends an encoding to the source. However this is
- # useless because you can only set an encoding on the first line
- RUBY_VERSION >= '1.9' ? src.sub(/\A#coding:.*\n/, '') : src
+ ::ERB.new("<% __in_erb_template=true %>#{template.source}", nil, erb_trim_mode, '@output_buffer').src
end
end
end
diff --git a/actionpack/lib/action_view/template/template.rb b/actionpack/lib/action_view/template/template.rb
index f61dd591a5..a9897258d2 100644
--- a/actionpack/lib/action_view/template/template.rb
+++ b/actionpack/lib/action_view/template/template.rb
@@ -52,16 +52,26 @@ module ActionView
locals_code = locals.keys.map! { |key| "#{key} = local_assigns[:#{key}];" }.join
+ code = @handler.call(self)
+ encoding_comment = $1 if code.sub!(/\A(#.*coding.*)\n/, '')
+
source = <<-end_src
def #{method_name}(local_assigns)
- old_output_buffer = output_buffer;#{locals_code};#{@handler.call(self)}
+ 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
+ end
+
begin
- ActionView::Base::CompiledTemplates.module_eval(source, identifier, 0)
+ ActionView::Base::CompiledTemplates.module_eval(source, identifier, line)
method_name
rescue Exception => e # errors from template code
if logger = (view && view.logger)
@@ -79,4 +89,4 @@ module ActionView
"_render_template_#{@identifier.hash}_#{__id__}_#{locals.keys.hash}".gsub('-', "_")
end
end
-end \ No newline at end of file
+end