How To Put Thymeleaf Variable Into Javascript? - Save My Knowledge
Wednesday, April 1, 2015

How To Put Thymeleaf Variable Into Javascript?

Hi, every beginner person who use thymelaf spring java should have this trouble since they are not familiar with the thymeleaf code. On PHP, we can inject the data we pass from controller. But on spring boot thymeleaf, we could only use <tag th:text="${passedData}"></tag>. It doesn't give us any clue to inject the passed data into javascript. We need to inject it to javascript because sometimes we need something to do on client page but the variable is from server. The simple example iswe need to refresh the page for every certain second and that second is from the calculation that server has made. We need to put the second into javascript so the countdown can be run.

Okay, the solution is we need to use inline data. This inline injection is very powerful when we need to put some variable on javascript. To use inline data is we just need to add two square bracket "[[]]" within the variable inside it. Example: [[${passedData}]]