How to add source code comments in Thymeleaf templates that don't get included in generated HTML?

Thymeleaf

Thymeleaf Problem Overview


I am using full stack Thymeleaf (spring mvc, security, layout dialect, webflow) in a mid-size web application.

Ok..now that we put so much of code in the html templates it would be nice to include source code comments that don't get included in the generated HTML file.

How do we achieve that?

Thymeleaf Solutions


Solution 1 - Thymeleaf

Version 2.1 is released so now you can upgrade your libraries and use comments in your code. With this version developers are able to use parser-level comment blocks:

<!--/* This code will be removed at thymeleaf parsing time! */-->

and prototype-only comment blocks:

<span>hello!</span>
<!--/*/
    <div th:text="${...}">

</div>
/*/-->
<span>goodbye!</span>

Detailed explanation can be found in the official documentation here: http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#comments-and-blocks

Solution 2 - Thymeleaf

As mentioned Rafal Borowiec to comment block of HTML code you should use

<!--/*something to comment*/--> construction (see documentation).

Also it is possible to comment/remove your javascript code using thymeleaf with

/*[- something to comment -]*/ construction (see documentation). So you can annotate your js code without leaking any information

/*[-
 *
 * Some information about function.
 *
 * -]*/
function someFunction() {
    console.log('Hello world');
}

Solution 3 - Thymeleaf

With Thymeleaf 3.0, the currently working version was

<!--/*-->
   this comment will be removed by thymeleaf on the template processing
<!--*/-->

Other answers, for earlier thymeleaf versions, did not work by me. The current thymeleaf documentation is here.

Solution 4 - Thymeleaf

Prior to version 2.1 you can do this

<th:block th:if="${false}"><!--   ignore me  --></th:block>

Its very ugly (the th:block needing a false th:if) but works.

Solution 5 - Thymeleaf

It's not possible in current stable version of Thymeleaf. It's planned for version 2.1 as mentioned in Thymeleaf Issue 10

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1020598View Question on Stackoverflow
Solution 1 - ThymeleafRafal BorowiecView Answer on Stackoverflow
Solution 2 - ThymeleafytterrrView Answer on Stackoverflow
Solution 3 - ThymeleafpeterhView Answer on Stackoverflow
Solution 4 - ThymeleafreevesyView Answer on Stackoverflow
Solution 5 - Thymeleafmichal.kreuzmanView Answer on Stackoverflow