Reputation: 2210
I'm using Spring Boot with Thymeleaf, and know I can use variables from my controller on the following way in javascript:
<script th:inline="javascript">
/*<![CDATA[*/
var username = [[${#authentication.principal.person.isSubscribedTo("random string")}]];
/*]]>*/
</script>
Now I tried to use a local variable outside the CDATA comment like this. I expected I could use that in the method.
var randomString = "can i use this?";
/*<![CDATA[*/
var username = [[${#authentication.principal.person.isSubscribedTo(randomString)}]];
/*]]>*/
This does not work and I can't test this because my debugger won't get it the method and is not giving back any errors.
How can I use a local javascript variable in a thymeleaf javascript expression?
Upvotes: 0
Views: 1486
Reputation:
You're showing a bit of confusion about what's happening when and where.
First, the server uses Thymeleaf to generate the HTML and dynamic Javascript for a page. In this process, as you've said Thymeleaf can call into your Spring beans as it's running on the server.
Then, once the HTML & dynamic Javascript is sent to the browser, it runs the Javascript all client-side.
The only real approaches are:
randomString
on the server side as well, within Thymeleaf or in the model accessible to Thymeleaf.Upvotes: 1