Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thymeleaf Unescaped JavaScript Inlining

How can I get unescaped JavaScript inlining output with Thymeleaf 3.0.x? Escaped inlining works just fine. Example:

pom.xml

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
  <groupId>org.thymeleaf</groupId>
  <artifactId>thymeleaf-spring3</artifactId>
  <version>3.0.3.RELEASE</version>
</dependency>

servelet:

model.addAttribute("test", "testing...");

html template:

<script th:inline="javascript">
/*<![CDATA[*/
  [[${test}]]
  [(${test})]
/*]]>*/
</script>

generated output:

<script>
/*<![CDATA[*/
  'testing...'
  [(${test})]
/*]]>*/
</script>

So, escaped expression [[ ]] works, but unescaped expression [( )] doesn't. I have a need to generate js conditionally, and there's no "easy" workaround, so this would've been very helpful. Has anyone been able to get this to work? Any help much appreciated!

like image 853
ikcodez Avatar asked Jan 23 '17 23:01

ikcodez


1 Answers

I finally got it to work with Spring Boot with the following four dependencies, all four being required (I'm using the latest versions currently available):

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>    
<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf</artifactId>
    <version>3.0.3.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf-spring4</artifactId>
    <version>3.0.3.RELEASE</version>
</dependency>
<dependency>
    <groupId>nz.net.ultraq.thymeleaf</groupId>
    <artifactId>thymeleaf-layout-dialect</artifactId>
    <version>2.1.2</version>
</dependency>

Hope this helps.

like image 179
ikcodez Avatar answered Nov 07 '22 21:11

ikcodez