Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using freemarker with javascript ES6 Template Strings

Freemaker templates render variables with the following syntax: ${name}

Javascript ES6 template strings have a similar syntax ${name}

The problem is that when freemarker runs on the server, it will try to render the templates in the javascript code, because Freemarker thinks that it has encountered a variable (when in fact it is a javascript template that should be rendered on the client). Recommnedation on how to handle this?

One way is to wrap all the JS in a freemarker comment (so it is never evaluated),

or put the JS code in a seprate file (not inlined in the html page) so that it is never evaluated by freemarker.

like image 632
adamM Avatar asked Jul 14 '26 06:07

adamM


1 Answers

you can use javascripts in noparse tags

 <#noparse>...</#noparse>

or

const name = "WORLD";
Hello ${r"${name}"}

html view "Hello ${name}" output-> Hello WORLD

like image 87
egemen Avatar answered Jul 20 '26 15:07

egemen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!