Partial render changes in v0.55

I had a partial template that I use to print some schema data for SEO purposes, it looks like this:

<script type="application/ld+json">
{{/* Generate our vars */}}
{{- if .IsHome -}}
  {{- $.Scratch.Set "schematype" "website" -}}
{{- else -}}
  {{- $.Scratch.Set "schematype" "Article" -}}
{{- end -}}

{{- $.Scratch.SetInMap "schema" "@context" "https://schema.org" -}}

<!-- Post type //-->
{{- $.Scratch.SetInMap "schema" "@type" ( $.Scratch.Get "schematype" ) -}}

<!-- Page Details //-->
{{- $.Scratch.SetInMap "schema" "url" .Permalink -}}
{{- $.Scratch.SetInMap "schema" "mainEntityOfPage" ( dict "@type" "WebPage" "@id" .Site.BaseURL ) -}}

<!-- Publisher Details //-->
{{- with (imageConfig ( printf "static/%s" .Site.Params.logo )) -}}
  {{- $.Scratch.Add "publisherImageWidth"  ( int .Width  ) -}}
  {{- $.Scratch.Add "publisherImageHeight" ( int .Height ) -}}
{{- end -}}
{{- $.Scratch.SetInMap "schema" "publisher" ( dict "@type" "Organization" "name" .Site.Author.james.name "sameAs" .Site.Author.james.social "logo" ( dict "@type" "ImageObject" "url" ( .Site.Params.logo | absURL ) "width" ($.Scratch.Get "publisherImageWidth") "height" ($.Scratch.Get "publisherImageHeight"))) -}}

{{- if .IsPage -}}
  <!-- Post Author //-->
  <!-- Get author image size -->
  {{- with (imageConfig ( printf "static/%s" .Site.Author.james.image )) -}}
    {{- $.Scratch.Add "authorImageWidth"  ( int .Width  ) -}}
    {{- $.Scratch.Add "authorImageHeight" ( int .Height ) -}}
  {{- end -}}
  {{- $.Scratch.SetInMap "schema" "author" ( dict "@type" "Person" "name" .Site.Author.james.name "sameAs" .Site.Author.james.social "description" .Site.Author.james.description "image" (dict "@type" "ImageObject" "url" ( .Site.Author.james.image | absURL ) "height" ($.Scratch.Get "authorImageHeight") "width" ($.Scratch.Get "authorImageWidth" ) ) "url" "https://jloh.co/author/james/" ) -}}

  <!-- Post details //-->
  <!-- TODO: Look at unescaping this?? //-->
  {{- $.Scratch.SetInMap "schema" "headline" .Title -}}
  {{- $.Scratch.SetInMap "schema" "datePublished" ( .Date.Format "2006-01-02T15:04:05-07:00" ) -}}
  {{- if .Lastmod -}}
    {{- $.Scratch.SetInMap "schema" "dateModified" ( .Lastmod.Format "2006-01-02T15:04:05-07:00" ) -}}
  {{- end -}}
  {{- with .Params.tags -}}
    {{- $.Scratch.SetInMap "schema" "keywords" (delimit . ", " ) -}}
  {{-  end -}}
  {{- with .Params.cover_image -}}
    {{- with (imageConfig ( printf "static/%s" . )) -}}
      {{- $.Scratch.Add "coverImageWidth" ( int .Width ) -}}
      {{- $.Scratch.Add "coverImageHeight" (int .Height ) -}}
    {{- end -}}
    {{- $.Scratch.SetInMap "schema" "image" ( dict "@type" "ImageObject" "url" ( . | absURL) "width" ($.Scratch.Get "coverImageWidth") "height" ($.Scratch.Get "coverImageHeight")) -}}
  {{- end -}}
{{- end -}}
{{- $.Scratch.SetInMap "schema" "description" "Description" -}}

<!-- Generate schema //-->
{{- $.Scratch.Get "schema" | jsonify -}}
</script>

Its included via a block in the single.html template:

{{- define "header" -}}
{{ partial "schema" . }}
{{- end -}}

And rendered like this in v0.54 (I’ve prettified it to make it a bit nicer to read, otherwise the JSON is just in a big blob)

<script type="application/ld+json">
{
  "@context": "https://schema.org",
  "@type": "Article",
  "author": {
    "@type": "Person",
    "description": "Sysadmin for a web solutions company deploying cloud solutions across the globe",
    "image": {
      "@type": "ImageObject",
      "height": 1130,
      "url": "http://localhost:62435/images/2017/08/jimmy.jpg",
      "width": 1000
    },
    "name": "James Loh",
    "sameAs": [
      "https://jloh.co",
      "https://github.com/jloh",
      "https://twitter.com/itsjloh"
    ],
    "url": "https://jloh.co/author/james/"
  },
  "dateModified": "2019-01-26T15:43:00+11:00",
  "datePublished": "2019-01-26T15:43:00+11:00",
  "description": "Description",
  "headline": "GeoJS gets a 2018 overhaul",
  "keywords": "GeoJS, Projects",
  "mainEntityOfPage": {
    "@id": "http://localhost:62435/",
    "@type": "WebPage"
  },
  "publisher": {
    "@type": "Organization",
    "logo": {
      "@type": "ImageObject",
      "height": 384,
      "url": "http://localhost:62435/images/2017/08/android-chrome-192x192-jloh-1.png",
      "width": 384
    },
    "name": "James Loh",
    "sameAs": [
      "https://jloh.co",
      "https://github.com/jloh",
      "https://twitter.com/itsjloh"
    ]
  },
  "url": "http://localhost:62435/posts/geojs-2018-overhaul/"
}
</script>

In v0.55 however the partial is completely busted and ends up rendered this like:

<script type="application/ld+json">
  """"<!-- Post type 
    <!-- Get author image size -->""""""<!-- Post details 
    <!-- TODO: Look at unescaping this?? </script>

I’ve fixed it by changing the include to be like this:

{{- define "header" -}}
<script type="application/ld+json">
{{- partial "schema" . | safeJS -}}
</script>
{{- end -}}

And removed any HTML (comments, scripts etc) so that they’re outside of the template. This now renders it as expected like how it was in v0.55:

  <script type="application/ld+json">


  
  {"@context":"https://schema.org","@type":"Article","dateModified":"2019-01-26T15:43:00+11:00","datePublished":"2019-01-26T15:43:00+11:00","description":"Description","headline":"GeoJS gets a 2018 overhaul","keywords":"GeoJS, Projects","mainEntityOfPage":{"@id":"http://localhost:1313/","@type":"WebPage"},"publisher":{"@type":"Organization","logo":{"@type":"ImageObject","height":192,"url":"http://localhost:1313/images/2017/08/android-chrome-192x192-jloh-1.png","width":192},"name":"James Loh","sameAs":["https://jloh.co","https://github.com/jloh","https://twitter.com/itsjloh"]},"url":"http://localhost:1313/posts/geojs-2018-overhaul/"}</script>

Just posting because I’m unsure whether this change was expected and in case it helps anyone else!

1 Like

I am not a big fan of {{- something -}} because these days you have the --minify parameter for the hugo command to remove unneeded spaces. There is probably an issue in your template with multiple inline " in between other "'s that cancel themselves out. A syntax-scheme for the editor of choice might help to locate them. I am using Sublime Edit and the syntax stops looking nice at most points.

Try using {{/* bla */}} instead of HTML comments for a start.

  <!-- Post Author //-->
  <!-- Get author image size -->

There might be // missing on the second comment, but as I said: use Go template comments and see what happens?

SafeJS is always a good solution by the way.

Also: In the syntax rendering of your template above the “Generate” is marked fat red which means it might be a command in Go or their templates. Maybe that is the reason. Because you are using HTML comments as opposed to Go comments this command is found and parsed.

Noticed the same thing when I upgraded and asked for help on here: Upgraded from v0.53 to v0.55.6 - breaks schema.org

Used safeJS as Regis suggested and it solved everything.

I have a more severe issue with some change in partial handling after upgrading from .50 to .58 (that’s what Gentoo offered me as choices). I’m using landing-page-hugo as template which is based on partials. Yes, I know this template is quite old and seems not to be maintained anymore, but it used to serve my purpose.
But after upgrading Hugo to 0.58 the rendered page is mostly empty. My quick solution was downgrading to 0.50, but obviously this is not a permanent solution.

Any ideas what broke the template? As I jumped from 0.50 to 0.58 I cannot tell exactly which version introduced the change.

Edit: I found the commit to fix this.