[SOLVED] Empty Title when listing sections

I am using below code in template to list all the section name, but the title is empty, except for last. refer the output code below.

Template code

<ul>
  {{ range $.Site.Home.Sections }}
	<li><a href="{{ .Permalink }}">{{ .Title }}</a> </li>
  {{ end }}
</ul>

Output

<ul>
	<li><a href="http://localhost:1313/gallery.html"> </a> </li>
	<li><a href="http://localhost:1313/policy.html"> </a> </li>
	<li><a href="http://localhost:1313/article.html">Articles</a> </li>
</ul>

Is there anything wrong with my code ?

hugo version
Hugo Static Site Generator v0.25 windows/amd64 BuildDate: 2017-07-09T15:25:23+05:30

That is not the way to go about it.

Read this if you want Hugo to generate your sections menu.

Thanks onedrawingperday

I changed the template as per the link you provided , but other sections are not appearing in the output. Do I miss anything, do I need to specify the other sections somewhere?

Code

{{ range $.Site.Home.Sections }}
<li><a href="{{ .Permalink }}">{{ . }}</a> </li>
{{ end }}

Output

<ul>
	<li><a href="/article.html">Articles</a></li>
</ul>

Solution

The reason why the sections gallery and policy are not displayed.

In my case if the section has _index.md file, that is not included in the list.

So hugo expect the title to be present in the _index.md file, but there was not one, so the title was empty in the output too.

to fix this I added below content in _index.md file which solved the issue.

---
title: "Gallery"
---

Now the issue is solved for me.

Is this the intended behavior @bep? Heretofore, sections without _index.md would default to taking the section title from the dir, if my memory serves. This is a documentation question.

I don’t understand the question – and it would be good to see the complete and runnable project to be able to help. Snippets like the above loose to much info.

[[EDIT]]

@thalib I just tried the code I provided that you said didn’t work unless an _index.md was provided for a section.

I am unable to reproduce this issue. I have a local test project set up with the following content:

. β”œβ”€β”€ events β”‚ β”œβ”€β”€ event-1.md β”‚ └── event-2.md β”œβ”€β”€ posts β”‚ β”œβ”€β”€ _index.md β”‚ β”œβ”€β”€ post-1.md β”‚ └── post-2.md └── tree.txt

And when I run the following templating:

<ul>
{{ range $.Site.Home.Sections }}
<li><a href="{{ .Permalink }}">{{ .Title }}</a> </li>
{{ end }}
</ul>

I get…

    <ul>
        <li><a href="http://example.org/posts/">My Posts Section</a> </li>
        <li><a href="http://example.org/events/">Events</a> </li>
    </ul>
2 Likes

@rdwatters The problem was after upgrading to v0.25

Problem Case: _index.md file present without title:

In this case the .Title came empty.

  • But in hugo v0.18.1 the .Title variable was sections dir name
  • after upgrading to v0.25, hugo expects title: entry to be present in _index.md else the .Title variable is empty

So I fixed my template by adding below entry in every section _index.md file.

---
title: "Gallery"
---

This is a Gallery of arts and creation by our team.....

Note: If there was no _index.md file then the section dir name was the output for .Title variable.

To reproduce this, try removing title: from post/_index.md in your test setup.

To my knowledge, title has always been a required field along with date in any content file. Nevertheless, I’m glad you have it working.