Something strange is happening and I don’t understand why …
I created a javascript-file “test.js” with a few lines of code
console.log("1");
console.log("2");
console.log("3");
and a shortcode “jscript” to embed the file in the page. The shortcode is located in the file “jscript.html”
{{- $file := printf "%s%s%s" "content/" .Page.File.Dir (.Get "file") -}}
{{ readFile $file | safeHTML }}
and is called like this
{{< jscript file="test.js" >}}
The content of the javascript file is displayed within the page content as expected.
However, when I enclose the two lines of code in a “script” tags
<script type="text/javascript">
{{- $file := printf "%s%s%s" "content/" .Page.File.Dir (.Get "file") -}}
{{ readFile $file | safeHTML }}
</script>
the output of the readFile is changed to this:
<script type="text/javascript">"console.log(\"1\");\nconsole.log(\"2\");\nconsole.log(\"3\");"
</script>
If I use “div” or “pre” tags instead of “script”,
<pre>
{{- $file := printf "%s%s%s" "content/" .Page.File.Dir (.Get "file") -}}
{{ readFile $file | safeHTML }}
</pre>
everything looks as expected:
<pre>console.log("1");
console.log("2");
console.log("3");
</pre>
Does anyone have an idea what is happening and why?