Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

thymeleaf

People also ask

How do I include a JavaScript file in Thymeleaf?

We load the stylesheet using the link tag with Thymeleaf's special th:href attribute. If we've used the expected directory structure, we only need to specify the path below src/main/resources/static. In this case, that's /styles/cssandjs/main. css.

How do you display text in Thymeleaf?

In this case, you should use th:text . For example, Username: <p th:text=${account. username}>Username will be rendered here</p> . Note that, the text inside p tag won't be shown.

How do you use attributes in Thymeleaf?

In Thymeleaf, these model attributes (or context variables in Thymeleaf jargon) can be accessed with the following syntax: ${attributeName} , where attributeName in our case is messages . This is a Spring EL expression.


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


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');
}

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.


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.


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


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!