Content adapters: make params (dict) into content

Halfway into converting one of my data sources using the new Content adapters | Hugo. I created a bunch of Params (based on fields in a remote JSON file) in the _content.gotmpl file and I would like to show those param fields as content in the single layout. How can this be achieved? Thanks in advance

I don’t understand the question. If the map you pass into AddPage includes a params map, the params are available in a template with {{ }}. The example in the docs has… an example of that.

I was looking for a way to extract them into content instead of repeating them again. something like

{{ range $k, $v := $params }}
  <li>{{ $k }}: {{ $v }}</li>
{{ end }}

Why can’t you do that?

Might have been a Hugo cache issue. Anything with trim foo " " was not working for the project until I restarted the server. Strange!

That makes no sense.

This worked

{{ range $k, $v := .Params }}
  <li>{{ $k }}: {{ $v }}</li>
{{ end }}

Might as well ask here. Hugo is lowercasing the $k value. How to prevent that?

You can’t. Keys are converted to lower case internally. If it’s something simple pass through strings.FirstUpper.

I am now just understanding that these keys are the .Params themselves. Gotta be careful with how they are defined.

Some keys are defined with an underscore `Foo_Bar".

EDIT: This works <li>{{- $k | replaceRE "_" " " | title }}: {{ $v }}</li>


replaceRE worked! Can the keys also appear in the order they are defined in the Params? They appear alphabetically on the front end.

No. Unlike slices, maps are not ordered. But when you range through the map it is alphabetized by key.

No problem! I can live with that. I hard hard corded the keys in the previous implementation.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.