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

前端 未结 5 1730
無奈伤痛
無奈伤痛 2021-02-03 18:14

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

相关标签:
5条回答
  • 2021-02-03 18:25

    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.

    0 讨论(0)
  • 2021-02-03 18:42

    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

    0 讨论(0)
  • 2021-02-03 18:44

    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.

    0 讨论(0)
  • 2021-02-03 18:45

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

    0 讨论(0)
  • 2021-02-03 18:47

    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');
    }
    
    0 讨论(0)
提交回复
热议问题