I have a param in front matter that look like this:
---
ids: [123, 456]
---
When I do console.log({{.Params.ids}})
in single.html I get ['123', '456']
I was expecting it to be [123, 456]
Same output using JSON format
{
ids: [123, 456]
}
Is this a bug or something that is expected?
I was trying to create a script that loop through all the ids
I need something like
<script>
let ids = {{.Params.ids}}
for (...)
...
pamubay
February 25, 2022, 10:08am
2
Yes, looks like it casted into array of string as the output of:
{{ printf "%#v" .Params.ids }}
Return:
[]string{"123", "456"}
You can try this:
<script>
let ids = [{{ delimit .Params.ids "," | safeJS }}];
</script
1 Like
Thank you @pamubay . This solves my problem.
I was wondering if I can just pass down js code from the front matter like this if I don’t need ids somewhere else.
<script>
{{ .Params.vardeclare | safeJS}}
Is this recommended?
Or is there a better way to pass down params that generate code?
pamubay
February 25, 2022, 12:00pm
4
Something like this?
title: My Title
js_string: |
var ids = [123, 456];
console.log(ids);
<script>
{{ .Params.js_string | safeJS }}
</script>
I think it’s okay
Here some examples how Hugo internal template populate inline script from config variable:
{{- $pc := .Site.Config.Privacy.GoogleAnalytics -}}
{{- if not $pc.Disable }}{{ with .Site.GoogleAnalytics -}}
{{ if hasPrefix . "G-"}}
<script async src="https://www.googletagmanager.com/gtag/js?id={{ . }}"></script>
<script>
{{ template "__ga_js_set_doNotTrack" $ }}
if (!doNotTrack) {
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '{{ . }}', { 'anonymize_ip': {{- $pc.AnonymizeIP -}} });
}
</script>
{{ else if hasPrefix . "UA-" }}
<script type="application/javascript">
{{ template "__ga_js_set_doNotTrack" $ }}
if (!doNotTrack) {
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
This file has been truncated. show original
{{- $pc := .Site.Config.Privacy.Disqus -}}
{{- if not $pc.Disable -}}
{{ if .Site.DisqusShortname }}<div id="disqus_thread"></div>
<script type="application/javascript">
window.disqus_config = function () {
{{with .Params.disqus_identifier }}this.page.identifier = '{{ . }}';{{end}}
{{with .Params.disqus_title }}this.page.title = '{{ . }}';{{end}}
{{with .Params.disqus_url }}this.page.url = '{{ . | html }}';{{end}}
};
(function() {
if (["localhost", "127.0.0.1"].indexOf(window.location.hostname) != -1) {
document.getElementById('disqus_thread').innerHTML = 'Disqus comments not available by default when the website is previewed locally.';
return;
}
var d = document, s = d.createElement('script'); s.async = true;
s.src = '//' + {{ .Site.DisqusShortname }} + '.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
})();
</script>
This file has been truncated. show original
More advanced use is using js.Build
to pass the data to your js bundle using params
options:
1 Like
Thanks, @pamubay . This is really helpful.
Expect everything coming from frontmatter or configuration to be a string and cast it into the type you want to see before using it.