Hello,
This is again in continuation of the debugprint.html
partial that I’ve been working on lately.
The issue is of a different kind this time.
I’d like to call {{ partial "debugprint.html" .File }}
in my template, and have it print all variables listed in Hugo | File Variables.
The documentation starts with:
The .File object contains the following fields:
.File.Path
(and so on…)
So I thought that within the .File
scope, printing .Path
would work. But that’s not the case. Correct me if I am wrong… but looks like .File.Path
calls the getter function Path
… there is no public field called Path
.
So below does not work in debugprint.html
:
{{ else if $typeIsFile }}
{{ $fileVarNames := (slice "Path" "LogicalName" "TranslationBaseName" "Ext" "Lang" "Dir") }}
<!-- Below does not work.. needs to be fixed.
The error is seen when you use access this if condition as:
{{/* partial "debugprint.html" .File */}}
The issue is that I don't know how set the $fileVarSymbols values so that the elements are
called as functions instead of fields. i.e. call Path function in .File instead of trying
to get the value of the field Path.
-->
{{ $fileVarSymbols := (slice .Path .LogicalName .TranslationBaseName .Ext .Lang .Dir ) }}
<table>
<tr><th class="key">File Variable</th><th class="value">Value</th></tr>
{{ range $idx, $elem := $fileVarNames }}
{{ printf "<tr><td class=\"key\">%s</td><td class=\"value\">" $elem | safeHTML }}
{{ partial "debugprint.html" (index $fileVarSymbols $idx) }}
{{ printf "</td></tr>" | safeHTML }}
{{ end }}
</table>
Full code: Pretty printing Hugo variables for debug · GitHub
With that partial in place, if I call {{ partial "debugprint.html" .File }}
(in a page template like single.html
, I get this error:
ERROR 2017/11/14 11:22:24 Error while rendering “page”: template: /home/kmodi/stow/pub_dotfiles/emacs/dot-emacs.d/elisp/ox-hugo/test/site/themes/bare_min/layouts/_default/single.html:76:7: executing “main” at <partial "debugprint…>: error calling partial: template: theme/partials/debugprint.html:88:41: executing “theme/partials/debugprint.html” at <.Path>: can’t evaluate field Path in type source.File
Is there a way to write that $fileVarSymbols
slice so that the .File.Path
gets called instead of an attempt to access .File.Path
field?
PS: Note that the same tactic worked for printing all the relevant .Site
and .GitInfo
object fields… {{ partial "debugprint.html" .Site }}
and {{ partial "debugprint.html" .GitInfo }}
works beautifully.