How can I get a user-friendly Section name/title?

I have a few sections on my site. Let’s call them /content/books/, /content/videos/, /content/notes/, and /content/ious/. I’m writing some template code to loop through sections and then loop through their pages.

What I want is to be able to take any particular Section, held in a $section variable, and output a pretty name for it. If I just output {{ $section }}, I get a plain name like “books”. What I want is to specify a nicer name somewhere.

I thought of using some workarounds like changing it to title case somehow, but that doesn’t work for everything, e.g. /content/ious/ should display as “IOUs”.

I thought maybe I could set a linkTitle in the archetypes files to do this, but that doesn’t seem to work. I guess the linkTitle gets overwritten in the individual pages.

Any help? Thanks.

I use a Params set in the _index.html to output the name I want like this:

{{ with .Params.story_title }}<h2>{{ . }}</h2>{{ end }}

Not 100% sure what you’re trying to accomplish, but you can, for any given section (e.g. books), give it an index file content/books/_index.md and in that index file assign it a title (title: Books)

Then, in your templates, do something like this:

{{$section_heading := .Site.GetPage "section" .Section }}
{{with $section_heading}}
  {{- .Title -}}
{{end}}

Hope that helps!

2 Likes

Thanks a lot, both of you. That does help!

Just want to add that where I have "section" .Section It could also be "section" "SOME_SECTION" that is, any section you want. My original code picks up the section of the current page.