Skip to content

Instantly share code, notes, and snippets.

@almostwhitehat
Created January 27, 2017 05:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save almostwhitehat/2adc4d980326fcdbc37bfd73021d86c1 to your computer and use it in GitHub Desktop.
Save almostwhitehat/2adc4d980326fcdbc37bfd73021d86c1 to your computer and use it in GitHub Desktop.
Add HTML comments to rendered templates and views to discover where content is coming from
# Drop this in as a rails initializer and resstart rails
# View the source of pages - you'll see which template and views are used when rendering pages
module ActionView
class TemplateRenderer
alias_method :original_render, :render
def render(context, options)
rendered_content = original_render(context, options)
# Random string makes it easier to jump between matching start / stop when viewing source of the page
random_string = SecureRandom.hex
template_path = determine_template(options).inspect
# Single case is overkill - but some day this might handle other types of templates
case @lookup_context.rendered_format
when :html
"<!-- [#{random_string}] Start #{template_path} -->\n #{rendered_content} <!-- [#{random_string}] End #{template_path} -->\n"
else
rendered_content
end
end
end
class Template
alias_method :original_render, :render
def render(view, locals, buffer=nil, &block)
rendered_content = original_render(view, locals, buffer, &block)
# Inline templates do not have a path or type
return rendered_content unless type
# Random string makes it easier to jump between matching start / stop when viewing source of the page
random_string = SecureRandom.hex
template_path = self.inspect
# Single case is overkill - but some day this might handle other types of templates
case type.to_sym
when :html
"<!-- [#{random_string}] Start #{template_path} -->\n".html_safe + rendered_content + "<!-- [#{random_string}] End #{template_path} -->\n".html_safe
else
rendered_content
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment