I have a JSON file stored in content/operating-areas/areas.json. There’s also an index.md within the operating-areas folder.
I am trying to load the areas.json file as a resource like so:
{{ with .Site.GetPage "operating-areas/index.md" }}
<section class="uk-section operating-areas">
<div>
<h2 class="uk-text-lead uk-text-center"><span>{{ .Title }}</span></h2>
{{ with .Content }}
<p class="uk-text-center">{{- . -}}</p>
{{ end }}
</div>
<ul>
{{ with .Resources.GetMatch "areas.json" }}
{{ range .areas }}
<li>{{ . }}<li>
{{ end }}
{{ end }}
</ul>
</section>
{{ end }}
My JSON file:
{
"areas": [
"London",
"Manchester",
...
]
}
But I’m getting an error:
Building sites … ERROR 2018/11/09 19:25:04 Error while rendering "home" in "": template: index.html:7:5: executing "main" at <partial "operating-a...>: error calling partial: template: partials/operating-areas.html:12:13: executing "partials/operating-areas.html" at <.areas>: can't evaluate field areas in type resource.Resource
From my old C++ view unmarshal should be under the surfarce as implementation detail.
Normal user will not understand it. The PROs could use a .Raw property to access the unmarshalled data.
I started this comment with my understanding of marshalling. Could be I’m wrong with this.
In my head there are two fundamental different ways to do this (correct me if I’m wrong. You can either:
Hide that “implementation detail” and create a more abstract API, aka. getJSON, getTOML, getCSV, getYAML etc.
Assume your users are note idiots, and give them the freedom to do whatever: {{ resources.Get "my.json" | unmarshal }}
In my head, 2) is better for everyone.
So, the wordunmarshal may be unfriendly/foreign for many, but it’s in so widespread use that I think we can use it: " unmarshalling or unmarshaling refers to the process of transforming a representation of an object that was used for storage or transmission to a representation of the object that is executable."
So, in Hugo term: We have a Resource which is a string, but need to have it in a format that we can range over in the templates, so we unmarshal it.
But instead of me writing these long epos everytime the “why do it this way?”, I suggest you complete that sentence and explain to me “why not?” and “what would you recommend as an alternative approach?”
Certainly way more than you need, say, the JSON in text format. If you want to do something with JSON (or TOML etc.) and do not want to “hand parse” it (which is extremely unpractical, if at all possible, in Go templates), you need to unmarshal it.
If you want to quote Wikipedia, you need to copy and paste it, so no information gets lost. To me it looks like you mix marshal and unmarshal.