Take the 2-minute tour ×

Is there a way to configure freemarker so that the Freemarker template engine automatically inserts the name of the current template as an HTML comment?

Example HTML output I would like to see:

<!-- template file: main.ftl -->
normal template code of the file main.ftl

<!-- template file: myinclude.ftl -->
This is the code from myinclude.ftl

I would like to use such functionality for debugging purposes only so that it is easier to find out which HTML fragments where rendered in which template.

Any hints?

share|improve this question
add comment

1 Answer

You could write a TemplateLoader implementation that just delegates to another TemplateLoader (to the one that you are using when not in debug-mode), but captures the stream that it returns, and inserts the HTML comment after the <#ftl ...> directive. (But don't add line-break to the comment, or else it will offset the line number in error messages; use ${'\n'}<!-- ... -->${'\n'} instead.)

share|improve this answer
add comment

Your Answer


