Skip to content

Instantly share code, notes, and snippets.

@mikezter
Created January 20, 2010 11:52
Show Gist options
  • Save mikezter/281794 to your computer and use it in GitHub Desktop.
Save mikezter/281794 to your computer and use it in GitHub Desktop.
Add Template Identification HTML Comments to ERB output in Rails
# Add Template Identification HTML Comments to ERB output
module ::ActionView
module TemplateHandlers
class ERB < TemplateHandler
include Compilable
cattr_accessor :erb_trim_mode
self.erb_trim_mode = '-'
def compile(template)
buf = <<-HTML
<% unless request and request.xhr? %>
<% __in_erb_template=true %>\n
<!-- // BEGIN #{ template } -->\n
<% end %>
#{ template.source }\n
<% unless request and request.xhr? %>
<!-- // END #{ template } -->\n
<% end %>
HTML
src = ::ERB.new(buf, nil, erb_trim_mode, '@output_buffer').src
RUBY_VERSION >= '1.9' ? src.sub(/\A#coding:.*\n/, '') : src
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment