How to get the first item from an array [SOLVED]

Given a definition of:
$mainSections := .Site.Params.mainSections

I need to pull out ONE section into a string (to use elsewhere).

I’m using this:

$section_name := printf "%s" (delimit ($mainSections) " ")

which does work as long as mainSections is one section, but for more than one item it fails:

I’ve tried various Hugo functions, using slice and first, etc, to no avail.

Here’s the full code:

This is where the index function will hopefully come in handy…

{{$section_name := index (.Site.Params.mainsections) 0}}
5 Likes

That’s still splicing out the first letter (the same result I was getting with first). Maybe I’m applying it wrong.

Huh. Do you have that set as a param?

Here is what I’m doing in a quick test site:

Templating

{{$mainSection := index (.Site.Params.mainSections) 0 }}
<h3>{{$mainSection}}</h3>

config.toml

[params]
  mainSections = ["one","two","three"]

Output

<h3>one</h3>
2 Likes

I got it now. I was blocked by something else. Thank you. And yours is much cleaner than the madness I had in there.

1 Like