setting up a value for a variable name in thymeleaf

Deepak Ramakrishnan Kalidass picture Deepak Ramakrishnan Kalidass · Dec 22, 2013 · Viewed 90.8k times · Source

I am new to Thymeleaf and converting my Web page from JSP to Thymeleaf. I have a strut tag like this:

<c:set var="someVariable" value="${someValue}"/>

That variable can be used anywhere in JSP. Is there any such alternatives for this in Thymeleaf?

Answer

Sotirios Delimanolis picture Sotirios Delimanolis · Dec 22, 2013

You can use local variables.

Declare an HTML element with a th:with attribute. For example

<div th:with="someVariable=${someValue}">

The documentation states

When th:with is processed, that [someVariable] variable is created as a local variable and added to the variables map coming from the context, so that it is as available for evaluation as any other variables declared in the context from the beginning, but only within the bounds of the containing tag.