Odd behaviour for {{- . - }} parsing. Hyphen works & doesn't works

Hello,

I have a shortcode with this very simple snipplet (I’m generating some javascript). Hugo 0.37.1 on Mac

{{ range (seq 1 3) }}
    document.getElementById("swap{{- . -}}").onclick=function(){
        swap{{- . -}}();
    };
{{ end }}

Result is OK (no white space) for the first {{- . -}} and with whitespace for the second.
This drives me mad :-). Dis I missed something obvious with hyphen & templates ?

    document.getElementById("swap1").onclick=function(){
        swap 1 ();
    };

    document.getElementById("swap2").onclick=function(){
        swap 2 ();
    };

    document.getElementById("swap3").onclick=function(){
        swap 3 ();
    };

Haven’t tested, but instead of:

swap{{- . -}}();

Try:

{{ printf "swap%d();" . | safeJS }}

I would actually do the whole document. … }; printing using printf piped to safeJS.

2 Likes

Your response is not only incredibly fast but also amazingly accurate !!! Thanks a lot

PS : it’s quite odd.