I ran into the following issue when authoring a partial. I’m using:
{{ dict "a" 1 "b" 2 | jsonify }}
This results in
{"a":1,"b":2}
which is perfectly valid.
Now I want to use the same expression inside <script> tags:
<script onload='myfunc({{ dict "a" 1 "b" 2 | jsonify }});'</script>
This results in
<script onload='myfunc("{\"a\":1,\"b\":2}");'</script>
Notation " is unwanted in this case, I want " instead. I don’t know how to achieve that, I tried with safeJS, to no avail.
Any help is appreciated here.
safeHTMLAttr to the help…
probably.
<script onload='myfunc({{ dict "a" 1 "b" 2 | jsonify | safeHTMLAttr }});'</script>
Same result, unfortunately. 
{{ $myFuncArgs := dict "a" 1 "b" 2 | jsonify }}
<script {{ printf "onload='%s'" (printf "myfunc(%s);" $myFuncArgs) | safeHTMLAttr }}></script>
safeHTMLAttr must be applied to the attribute name and value together, not just to the value.
@jmooring: Thanks for helping getting me started with hugo templating. Your code works like a charm and does exactly what its supposed to do.
system
Closed
6
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.