thymeleaflocal-variablesinline-code

Create a custom variable in Thymeleaf that is used in Javascript


My object returned is a list like this:

data = [{'phone': '321-321-321',
          'email': 'test@test.com'
         },
        {'phone' : '123-123-123', 
         'email': 'bc@bcc.com'
        }]

I need a custom variable to be used in Javascript that looks like this (in fact a localstorage variable): //Javascript variable needs to iterate over the data and create a comma seperated list:

//var emails = 'test@test.com, bc@bcc.com' 

Is there any way of doing this efficiently?


Solution

  • I'm not sure if I understood correctly, but you can assign Spring object to javascript variable like this:

    <script th:inline="javascript">
    /*<![CDATA[*/
    ...
    
    var username = /*[[${session.user.name}]]*/ 'Sebastian';
    
    ...
    /*]]>*/
    </script>
    

    documentation