Created
January 27, 2017 05:57
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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