Fixing variable warnings

The following partial is throwing, nay printing, warnings:

{{- if or (and ($hit.IsDescendant $section) (and (not $hit.Draft) (not $hit.Params.private))) $section.IsHome -}}
  {{- .Scratch.SetInMap $hit.File.Path "objectID" $hit.File.UniqueID -}}
  {{- .Scratch.SetInMap $hit.File.Path "date" ($hit.Date.Format "January 2, 2006") -}}
  {{- .Scratch.SetInMap $hit.File.Path "description" $hit.Description -}}
  {{- .Scratch.SetInMap $hit.File.Path "publishdate" $hit.PublishDate -}}
  {{- .Scratch.SetInMap $hit.File.Path "summary" $hit.Summary -}}
  {{- .Scratch.SetInMap $hit.File.Path "title" $hit.Title -}}
  {{- .Scratch.SetInMap $hit.File.Path "url" $hit.Permalink -}}   
  {{- $.Scratch.SetInMap "hits" $hit.File.Path (.Scratch.Get $hit.File.Path) -}}
{{- end -}}

The warnings that are are thrown are:

WARN 2019/06/13 21:22:37 .File.Path on zero object. Wrap it in if or with: {{ with .File }}{{ .Path }}{{ end }}
WARN 2019/06/13 21:22:37 .File.UniqueID on zero object. Wrap it in if or with: {{ with .File }}{{ .UniqueID }}{{ end }}

As an aside: Why can’t Hugo tell me filename and line number of where the warning occurs? This must be a technical impossibility or an oversight :wink:

My problem is, that I have no idea how to implement the wrapping inside those more or less intricate scratch lines. How would you, all-knowing reader, attempt this? In other languages they have nice little constructs like $hit.File.Path ? dosomething : dosomething else - is there a comparable construct in GoLang?

See if this fixes one of the warnings. If so, use that same pattern for each.

{{ if $hit.File }}
  {{ .Scratch.SetInMap $hit.File.Path "objectID" $hit.File.UniqueID }}
{{ end }}
1 Like

Great, of course that works :slight_smile: I did use one single block around all those $hit.File lines and all looks good. Sometimes it’s better to take two steps back and see the whole thing instead of single lines.