Inside script tag output surrounded with extra spaces

Consider the following snippet. The value of $bar is used as an HTML class name and a JS variable name (foo42 is valid variable name in JS).

{{$bar := 42}}
<div class="foo{{$bar}}">
    var foo{{$bar}} = 42;

The output is:

<div class="foo42">
    var foo 42  = 42;

And it is the same using {{- $bar -}}. It seems like Hugo wants to prevent me generating JS. But why???

version: Hugo Static Site Generator v0.55.6/extended linux/amd64 BuildDate: unknown

Have you tried with | safeJS ?

The safeJS solved it, thank you!
Though for me, it still seems to be an unforseeable and inconsistent behavior.

well you are inside a script tag, it’s only natural that hugo’s template engine tries to protect you from having someone inject their own javascript. :slight_smile: