Have used elisp, I am spoiled by “code is data and data is code”.
So I can generate a handle to a variable from a string if I like. Is that possible in Go templates?
See this as an example to understand what I mean…
{{ $siteVarNames := (slice "Title" "Author" "Social" "LastChange" "DisqusShortname" "GoogleAnalytics" "Copyright" "RSSLink" "LanguageCode" "BuildDrafts" "Params") }}
{{ $siteVarSymbols := (slice .Title .Author .Social .LastChange .DisqusShortname .GoogleAnalytics .Copyright .RSSLink .LanguageCode .BuildDrafts .Params ) }}
<table class="debugprint">
<tr><th class="key">SiteInfo variable</th><th class="value">Value</th></tr>
{{ range $idx, $elem := $siteVarNames }}
{{ printf "<tr><td class=\"key\">%s</td><td class=\"value\">" $elem| safeHTML }}
{{ partial "debugprint.html" (index $siteVarSymbols $idx) }}
{{ printf "</td></tr>" | safeHTML }}
{{ end }}
</table>
Above, as you see, I need to get the variable “handle” .Title
, as well as its associated string name "Title"
.
So is there a way I can avoid creating $siteVarNames
as well as $siteVarSymbols
, and instead derive one from another?
Full code debugprint.html
Why I do that? To get this: https://ox-hugo.scripter.co/test#debug