Thymeleaf Tutorial: Chapter 11. Comments and Blocks

  • Tutorial
Table of contents

11 Comments and Blocks


11.1. Standard HTML / XML Comments


Standard HTML / XML comments can be used anywhere in Thymeleaf templates. Everything inside these comments will not be processed by Thymeleaf and will be copied verbatim:

...

11.2. Thymeleaf parser level comment blocks


Parser-level comments are pieces of code that are simply removed from the template when parsed. Let's look at them:


Thymeleaf will delete everything in between , so these comment blocks can also be used to display code when the template is statically open, knowing that it will be deleted when Thymeleaf processes it:

you can see me only before Thymeleaf processes me!

This can be useful for prototyping tables with a large number, for example:


     ...
   
     ...
   
     ...
   

11.3. Thymeleaf Prototyping Level Comments


Thymeleaf allows you to define special comment blocks that are convenient when displaying a prototype design, but they are considered normal markup when processing a template from Thymeleaf.

hello!

    ...
  
/*/-->goodbye!
Thymeleaf's parser will simply remove the markers, but not their contents, which will be left without comment. When processing the template at the output we will see:

hello!
...
goodbye!

As in the comment blocks at the parser level, this function is independent of the dialect.

11.4. Synthetic th: block tag


Thymeleaf's elemental processor (not attribute) and included in Standard Dialects is th: block .

th: block is a simple attribute container that allows template developers to specify which attributes they want. Thymeleaf will execute these attributes and then simply delete the block, but not its contents.

Thus, this can be useful, for example, when creating repeating tables that require more than one for each element:

......
...

And especially useful when used in conjunction with comment blocks for the prototype only:


     /*/-->
     /*/-->
......
...

Please note how this solution allows templates to be valid HTML (no forbidden blocks need to be added
inside), and it continues to work fine when opened in browsers as prototypes!

Also popular now: