Setting custom meta desc and title for a section

I’ve read the discussion about extending support for section frontmatter.

However, until that happens what is the recommendation on setting the <head> <title></title> and <meta name="description"> for a section layouts/section/mysection.html?

Do I add some extra logic-fu to my layouts/partials/header.html?

Two options:

  1. You can add a check for IsPage then use the site title if not a page. This means you will get generic titles for all the sections …
  2. You can combine the above with using ‘.Section’ variable in the title …
  3. Or you can put all your section titles (and description etc.) in /data and use ‘.Section’ as the lookup key.

Or variations of the above.

Just for testing I tried setting {{.Section}} in my header.html, but it is always empty even when I visit a section, e.g. http://example.com/mysection/ where mysection exists in /layouts/sections/mysection.html.

I’m still not sure how to implement your suggestions @bep since {{.Section}} doesn’t seem to work for me.

Any ideas on what I’m doing wrong?

The section being empty part sounds like a bug. But you could do 3) above and use something else (URL?) as key.

1 Like

@bep using {{if eq .URL "/mysection/"}}{{.Site.Data.section.mysection.title}}{{end}} worked great.

I have two further questions I’ve been thinking about which relates to what we’ve discussed so far.

  1. Is using a section a better way to create new “pages” akin to homepage, e.g. /about/ than to use a content file and layout, so

    using a section
    /layouts/section/about.html
    /data/section/about.toml <-- for custom title and meta description

    using a combination of content and layout
    /content/about.md
    /layouts/about/single.html

  2. For the data file I can’t decide whether to use a single file for all sections, e.g. /data/sections.toml or a unique file for each section, e.g. /data/section/mysection.toml

/data/sections.toml

[about]
title = "About Me"
description = "Lots of juicy details"

[mysection]
title = "My Section"
description = "My section description"

/data/section/about.toml

title = "About Me"
description = "Lots of juicy details"

/data/section/mysection.toml

title = "My Section"
description = "My section description"

I know none of this really matters as the output is the same, but I’d appreciate your thoughts nonetheless :wink:

On the first question about simple one-off pages, I usually create /content/about.md and set type = "about" in the front-matter. That will instruct Hugo to use the /layouts/about/single.html template.

Yeah that’s what I’ve done as well. I just started to think more about this as I was working on a “true” section and @bep suggested using a data file to solve my issue of setting a specific title and meta description for that section.

My reasoning for possibly changing my one-off pages from the way you describe to a section without a content file is that the single.html is meant for rendering a page within in a section. The idea being you’d usually have more than one page.

In this case we’re only talking about one page that’s “standalone” and so making it a section/about.html would perhaps fit better with the “hugo way”. Just my thoughts.

Btw my one-off pages content files tend to only have a few lines of frontmatter and zero content as the content is rather complex and I keep it in the single.html layout file.

Sorry to revive an old thread, but @mrsmith did you decide on an optimal way to set up section titles and descriptions? I am working on the same thing now, for a site with a lot of sections, so was hoping to benefit from your experience.

(As an example, this is about 1/10th of the current site - every section would need a title, 2-3 paragraphs of explanatory text and to list the pages it contains:)

.
└── content
    └── about-us
    |   └── contact-us.md
    |   ├── corporate-information
    |   |   ├── annual-report
    |   |   |   ├── annual-report-2015-2016
    |   |   |   |   ├── chapter1.md
    |   |   |   |   ├── chapter2.md
    |   |   |   |   ├── chapter3.md
    |   |   |   ├── annual-report-2014-2015
    |   |   |   |   ├── chapter1.md
    |   |   |   |   ├── chapter2.md
    |   |   |   |   ├── chapter3.md
    |   |   └── operational-guide
    |   |   |   ├── authorisation1.md
    |   |   |   ├── authorisation2.md
    ├── media-and-engagement
    |   ├── news
    |   |   └── news1.md
    |   |   └── news2.md
    |   |   └── news3.md
    |   ├── statement
    |   |   └── statement1.md
    |   |   └── statement2.md
    |   |   └── statement3.md
    |   └── subscribe.md
    └── resource
        ├── public
        |   |   ├── factsheet
        |   |   |   ├── factsheet1.md
        |   |   |   ├── factsheet2.md
        |   |   |   ├── factsheet3.md
        |   |   ├── training
        |   |   |   ├── training1.md
        |   |   |   ├── training2.md
        |   |   |   ├── training3.md
        ├── private
            └── faq.md