Creating a generic section template

I’d like to create a generic section template that makes use of content views. This would allow me to create a different li.html, or summary.html file for each of my sections. This would prevent the need to duplicate the templates by hard-coding the section names. My desired directory structure in layouts would be:

- /layouts
  - /_default
    - section.html
    - single.html
  -/news
    - li.html
    - summary.html
  -/projects
    - li.html
    - summary.html

My attempt leads to an error; Hugo is looking for an li.html file in Pages rather than my section of News:

<div class="wrapper">
  <h1>{{ .Title }}</h1>
  <ul>
  {{ range where .Paginator.Pages "Section" .Section }}
      {{ .Render "li" }}
  {{ end }}
  </ul>
  {{ template "_internal/pagination.html" . }}
</div>

OR

<div class="wrapper">
  <h1>{{ .Title }}</h1>
  <ul>
  {{ $section := .Section }}
  {{ range where .Paginator.Pages "Section" $section }}
    {{ .Render "li" }}
  {{ end }}
  </ul>
  {{ template "_internal/pagination.html" . }}
</div>

Error Message:
Expecting to find a template in either the theme/layouts or /layouts in one of the following relative locations [page/li.html, _default/li.html, theme/_default/li.html]

Bumping this.

Thanks to http://discuss.gohugo.io/t/howto-show-what-values-are-passed-to-a-template/41 for explaining how to dump variables for debugging I discovered that {{ .Section }} is an empty string when included in /layouts/_default/section.html. If I visit localhost/news shouldn’t {{ .Section }} be “news”?

I’d like to have a generic section.html and use a different {{ .Render "summary" }} for each section.

/layouts/_default/section.html

<!DOCTYPE html>
<html>
  {{ partial "head.html" . }}
  <body>
    {{ partial "header.html" . }}
    {{ partial "hero.html" . }}
    {{ partial "search-form.html" . }}
    <section id="main-content">
      <div class="wrapper">
        <h1>{{ .Title }}</h1>
        {{ printf "%#v" . }} <!-- .Section is an empty string -->
        <ul class="section-summary">
       <!-- I don't want to hard-code the section name here! -->
        {{ range where .Paginator.Pages "Section" "news" }}
          <!-- could be news, projects, etc. --> 
          {{ .Render "summary" }} 
        {{ end }}
        </ul>
        <div class="pagination">
        {{ partial "pagination.html" . }}
        </div>
      </div>
    </section>
    {{ partial "footer.html" . }}
  </body>
</html>

/layouts/news/summary.html

<li>
  <article class="news-story">
    <h3><a href="{{.Permalink }}">{{ .Title }}</a></h3>
    <span class="news-date">{{ .Date.Format "Jan _2, 2006"}}</span>
    <div class="news-summary">{{ .Summary }}</div>
    <a class="more" href="{{ .Permalink }}">Read full story...</a>
  </article>
</li>

Hi @rhewitt, I just worked on this problem, trying to make a generic list template to use, that will have some unique content depending upon the section. You need to use .Title to do this.

This is what I recently did, some of which is OT for your case, but I’ll mention it anyway. I have the below code in a partial which is called by my generic list.html. It picks up the section name and shows it, via .Title. Additionally, I am pulling some text from config.toml, via a parameter called postsblurb under the [params] section. I’m planning to add more of these as I need, for the main sections - like .Site.Params.portfolioblurb or the like.

 <div class="myclass">
    <h3>Welcome to the {{ .Title }} Section</h3>
       <p>
          {{ if eq .Title "Posts" }}
             {{ .Site.Params.postsblurb }}
          {{ end }}
        </p>
        <p>
         Click to filter the content by category: 
            {{ range $name, $taxonomy := .Site.Taxonomies.categories }}
		  <a href="/categories/{{ $name | urlize }}"> {{ $name }}, </a>
            {{ end }}
         </p>
</div>

I want the comma after the category name to be conditional but I have not yet looked into it. It’s good enough for me, for now, though. Maybe there’s a delimiter in the range function.

Thanks @RickCogley.

I was able to generalize for my use case using {{ .Title }} as you suggested.:

<!-- Title is "News" -->
{{ $section := lower .Title }}

{{ range where .Paginator.Pages "Section" $section }}
    {{ .Render "summary" }}
{{ end }}

Glad to hear it @rhewitt. Today I went through the docs looking at the github repo for spf13’s site, and that helped me make relevant and useful changes to my own.

It helped me to run the hugo server command with -v as well, for verbosity. More hints that way.