Phlow
July 13, 2020, 4:04pm
1
I am using v0.74.0. I want to produce JSON-data in a partial to include it in the head of a document. The code works but…
…it produces unwanted ""
Why? And how can I prevent these " ???
{{- $scratch := newScratch -}}
{{/* Set variables */}}
{{- $scratch.Set "pages" slice -}}
{{- $scratch.Set "reversed" slice -}}
{{- $scratch.Set "current" . -}}
{{/* Loop over parent pages */}}
{{- range slice 1 2 3 4 5 6 -}} {{/* Since there is no while loop in Hugo, I set it to a maximum of 6 crumbs. If there are more, you can increase the slice range */}}
{{- if ($scratch.Get "current") -}}
{{- $scratch.Add "reversed" ($scratch.Get "current") -}}
{{- $scratch.Set "current" ($scratch.Get "current").Parent -}}
{{- end -}}
{{- end -}}
{{/* Add "position" property */}}
{{- $scratch.Set "position" (len ($scratch.Get "reversed")) -}}
{{- range $scratch.Get "reversed" -}}
{{- $scratch.Add "pages" (dict "position" ($scratch.Get "position") "page" .) -}}
{{- $scratch.Set "position" (sub ($scratch.Get "position") 1) -}}
{{- end -}}
{{/* Reverse page order & create breadcrumb objects */}}
{{- range sort ($scratch.Get "pages") "position" "asc" -}}{{- if ne .position 1 -}},{{- end -}}
{
"@type": "ListItem",
"position": {{ .position }},
"item":
{
"@id": "{{ .page.Permalink}}",
"name": "{{ if .page.Params.BreadcrumbListName }}{{ .page.Params.BreadcrumbListName }}{{ else }}{{ .page.Title }}{{ end }}"
}
}
{{- end -}}
OUTPUT – excerpt
See the first """"""""""""
,""""""""""""""""""""""""""""""""""""""""{
"@type": "ListItem",
"position": 1 ,
"item":
{
"@id": "\/\/localhost:4000\/",
"name": "Phlow"
}
},{
"@type"
~~~
How and from where is this partial called?
Phlow
July 13, 2020, 4:48pm
3
Hey @jmooring , thank you for responding.
My partials folder looks like this
partials
├── helper
├── seo
├── _seo
├── canonical_and_robots.html
├── seo_description.html
├── seo_facebook.html
├── seo_pinterest.html
├── seo_schema.html
├── seo_search_console_webmaster_tools.html
├── seo_title.html
└── seo_twitter.html
And the _seo -partial which combines everything into one looks like this:
{{ partial "seo/seo_title.html" . }}
{{ partial "seo/seo_description.html" . }}
{{ partial "seo/canonical_and_robots.html" . }}
{{ partial "seo/seo_facebook.html" . }}
{{ partial "seo/seo_twitter.html" . }}
{{ partial "seo/seo_pinterest.html" . }}
{{ partial "seo/seo_schema.html" . }}
{{ partial "seo/seo_search_console_webmaster_tools.html" . }}
and it’s called in helper/head.html like so {{ partial "seo/_seo" . }}
and than in…
baseof.html
I work very modularized…
Unable to reproduce.
git clone --single-branch -b hugo-forum-topic-26922 https://github.com/jmooring/hugo-testing hugo-forum-topic-26922
cd hugo-forum-topic-26922
hugo server
This looks familiar; I’ve had to add safeJS in a few places to avoid quote-explosions from generated JavaScript.
-j
1 Like
Like I said, unable to reproduce.
Obviously there’s some missing context in the original question. When I ran into this issue , I had to move a $.Scratch.Add
loop from inside the <script>
tag and then add a safeJS
when I interpolated the results. Otherwise I got thousands of spurious double-quotes.
-j
2 Likes
Phlow
July 13, 2020, 6:32pm
8
That solved my problem, thankyou @jgreely
I put a | safeJS
like you recommend and the nasty " disappeared!
{{- $scratch := newScratch -}}
{{/* Set variables */}}
{{- $scratch.Set "pages" slice | safeJS -}}
{{- $scratch.Set "reversed" slice | safeJS -}}
{{- $scratch.Set "current" . | safeJS -}}
{{/* Loop over parent pages */}}
{{- range slice 1 2 3 4 5 6 -}} {{/* Since there is no while loop in Hugo, I set it to a maximum of 6 crumbs. If there are more, you can increase the slice range */}}
{{- if ($scratch.Get "current") -}}
{{- $scratch.Add "reversed" ($scratch.Get "current") | safeJS -}}
{{- $scratch.Set "current" ($scratch.Get "current").Parent | safeJS -}}
{{- end -}}
{{- end -}}
{{/* Add "position" property */}}
{{- $scratch.Set "position" (len ($scratch.Get "reversed")) | safeJS -}}
{{- range $scratch.Get "reversed" -}}
{{- $scratch.Add "pages" (dict "position" ($scratch.Get "position") "page" .) | safeJS -}}
{{- $scratch.Set "position" (sub ($scratch.Get "position") 1) | safeJS -}}
{{- end -}}
{{/* Reverse page order & create breadcrumb objects */}}
{{- range sort ($scratch.Get "pages") "position" "asc" -}}{{- if ne .position 1 -}},{{- end -}}
{
"@type": "ListItem",
"position": {{ .position }},
"item":
{
"@id": "{{ .page.Permalink}}",
"name": "{{ if .page.Params.BreadcrumbListName }}{{ .page.Params.BreadcrumbListName }}{{ else }}{{ .page.Title }}{{ end }}"
}
}
{{- end -}}
1 Like
system
Closed
July 15, 2020, 6:40pm
9
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.