[solved] Syntax problem into thymeleaf template

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[solved] Syntax problem into thymeleaf template

BeRoots
This post was updated on .
Hi :)
i using spring-boot-starter-thymeleaf-1.5.1.RELEASE.jar

I want to add this into my template:
<script>window.jQuery || document.write('<script src="path/to/webjars/jquery/1.12.4/jquery.min.js"><\/script>')</script>
I want the backslash because is important.

I have test with this example but doesn't work and it throw a 500 error:
<script>window.jQuery || document.write('<script th:src="@{/webjars/jquery/1.12.4/jquery.min.js}"><\/script>')</script>

I have test more other way with double quotes and \ and \\ example but i don't find an issue.
If someone have an idea ?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Syntax problem into thymeleaf template

Metroids
Put your javascript into a CDATA block... Like this:

<script>
// <![CDATA[

window.jQuery || document.write('<script src="path/to/webjars/jquery/1.12.4/jquery.min.js"><\/script>')

// ]]>
</script>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Syntax problem into thymeleaf template

BeRoots
Thank you Metroids. It works nice :-)
But why it works without CDATA block in the same example in PHP symfony ?

If someone have time to explain this ;-)
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Syntax problem into thymeleaf template

Metroids
Depending on how it's configured, thymeleaf is expecting valid XML for its templates.  So, if you're using strings with XML tags in like
<script><\/script>
, thymeleaf won't be able to parse the file correctly.  I'm guessing that Symphony just operates on text, not caring about the format of your html.

The CDATA allows thymeleaf to treat that part of the code as raw text.
Loading...