Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between th:text and th:value in Thymeleaf

Tags:

thymeleaf

I just recently started using Thymeleaf through one of my projects. I have seen few examples where th:text=${example} is being used in some places th:value=${example}.

I have gone through the Thymeleaf documentation but couldn't find anything explicitly citing the difference, nor did any question on SO.

Any help would be really appreciated! Thanks.

like image 484
ASR4 Avatar asked Oct 27 '25 05:10

ASR4


1 Answers

  • th:value is modification of html attribute value. For button, input and option elements, the value attribute specifies the initial value of the element
  • th:text is used for tag body modification.

div{background-color: lightblue; padding: 2px} // to highlight empty div
<!--th code:               <div th:value="${value}"/></div> -->
<br/>Result th:value div:  <div value="sometext"/></div>
        
<!--th code:               <form><input th:value="${value}"/></form>-->
<br/>Result th:value form: <form><input value="sometext"></form> 

<!--th code:               <div th:text="${value}"></div> 
Same as:                   <div>[[${value}]]</div> -->
<br/>Result th:text div:   <div>sometext</div>

Here is docs of different Thymeleaf attributes features

like image 60
varren Avatar answered Oct 28 '25 19:10

varren