Looping through a list parameter

could you help finish this partial ?

{{ with Params.Requirements }}
{{<hint warning>}}
{{ range . }}
 - {{ title . }}
{{ end }}
{{ end }}

I know it’s horrible, but that language is too… can we even do that, calling shortcodes from partials ? The whole thing gets my head ringing…

Requirements is a list, and if present it should generate the disclaimer before .content, so in conten-before.html

Finish how without knowing what Params.Requirements contains, what the HTML should look like, if and what error messages you’re seeing or if you’re not seeing them, what the HTML really looks like etc.

sorry, I honestly thought it was obvious from this excerpt.

“Requirements” look like this, an array or page names:

- - -
title: The deep depravation of the “wild”
- meta_en
- presentation_instincto_en
- cooking_origin_en
- - -

While the text generated should look like this:

- Metapsychoanalysis, the discovery
- Cooking: the Original Sin
- Cooking: when it began

then the rest of the file would follow, whatever is after the frontpage.

No, they only work in content files.

If you want to reuse some code, put it in a partial that you can call from templates as well as shortcodes.

for now I have

{{ with .Params.Requirements }}
<u>Reading requirements:</u>
	{{ range . }}
<li> {{ title . }}
	{{ end }}
{{ end }}

But it doesn’t extract the title, just put a nice majuscule to the filename !
How do I get the titles from the them ?

Sounds like you expect Hugo to retrieve a page based on “.Params.Requirements”.

For that to happen you need to instruct Hugo what do do, if not it just a random string.

The “GetPage” function should help you out.

That is exactly what the title function (!) is doing, and you’re calling that function on the current element of your slice. Variables, parameters etc. in Hugo do not start with a letter (afaict, there may be exceptions). So, if you want to get the title from (e.g.) the front matter, you’ll have to use .title (if dot refers to a page, which it doesn’t here). So, as @frjo wrote, you have to get the page first, and then get its title.

That is exactly what the title function (!) is doing

I figured that much :rofl:


{{ with .Params.Requirements }}
<div class=".book-toc" style="font-weight: bolder;color:silver">
<u>Reading requirements:</u>
<ol style="margin-top:auto;margin-left:-1.5rem">
	 {{ range . }}
<li> {{ with .Site.GetPage . }}{{ .Title }}{{ end }}
	 {{ end }}
{{ end }}

causes the errors:

Error: Error building site: failed to render pages: render of “page” failed: “/home/drm/WEBSITE/themes/hugo-book/layouts/_default/baseof.html:82:5”: execute of template failed: template: _default/single.html:82:5: executing “toc” at <partial “docs/toc” .>: error calling partial: “/home/drm/WEBSITE/themes/hugo-book/layouts/partials/docs/toc.html:1:3”: execute of template failed: template: partials/docs/toc.html:1:3: executing “partials/docs/toc.html” at <partial “docs/inject/toc-before” .>: error calling partial: “/home/drm/WEBSITE/themes/hugo-book/layouts/partials/docs/inject/toc-before.html:6:18”: execute of template failed: template: partials/docs/inject/toc-before.html:6:18: executing “partials/docs/inject/toc-before.html” at <.Site.GetPage>: can’t evaluate field Site in type string

I suppose I’m close but not quite.