Escaping Alpine and other JS in Velocity templates
As a relative newbie to Java and Velocity templates, some absolute pain I’ve endured is Velocity insisting on interpreting $
as a variable even when it’s included in JavaScript.
The app I’m working on uses a lot of Alpine, and so the magics like $dispatch
, $el
and similar get interpreted into Velocity-side variables!
Turns out there’s a simple solution: wrapping your code in a special ‘include but do not parse’ syntax:
#[[
<script>
$dispatch('some-event'); // this won't get borked!
</script>
]]#
That #[[
will save me a world of hurt.