year wise archive page

Been trying to create a year wise archive page, last attempt was okay. The only downside was it listed Post as one of the entires in archives.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{{ partial "header.html" . }}

<div class="container" role="main">
  <div class="row">
    <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
      <div id="blog-archives" class="category">
        {{ range (where .Site.Pages "Type" "post") }}
              <h3 class="year-format">
                {{ .Date | dateFormat "2" }}   <!-- print publish day -->
                {{ .Date | dateFormat "Jan" }} <!-- print publish month -->
                {{ .Date | dateFormat "2006"}}                
              </h3>
              <article>
                <h3>
                  <a href="{{ .Permalink }}" title="{{ .Title }}"> {{ .Title }}</a>
                </h3>
              </article>
            
        {{ end }}
        </div>
    </div>
  </div>
</div>

Results in this page:

/images/old-archives-page.png


Recently found this article and was able to generate a much better archives page. Content is grouped by year instead of a flat list. Could be easily extented to Year-Month based categorization- but this works.

Here:

  • $.Scratch acts as temp storage for specific page. Hugo Link
  • $.Scratch.Set "count" a temp per page variable to neglect iteration thru entire range
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<div class="row">
  <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
    <div id="blog-archives" class="category">
      
      <!--  https://www.thedroneely.com/posts/generating-archive-pages-with-hugo/ -->

      <!--  we are interested in posts, and set initialize count to 1 upfront
            else the latest year will not be printed
      -->
      {{ $type := "post" }}
      {{ $.Scratch.Set "count" 1 }}

      <!--  for each post that we have, see if we have them in current year, 
            then only print year tag, else skip it.
            results in descending list by year
      -->
      {{ range (.Site.RegularPages.GroupByDate "2006") }}

        <!-- 
            This generates year link/tag only if there are some posts
            above range takes care of it `gt .Key 1`
        -->
        {{ if and (gt .Key 1) (gt (where .Pages "Type" $type) 0) }}

            <!-- 
                I will try to find if there is a better of breaking loop early
                rather than unncessarily iterating over entire range
            -->
          {{ range (where .Pages "Type" $type) }}
            {{ if (eq ($.Scratch.Get "count") 1) }}
              {{ $.Scratch.Set "count" 0 }}
              &nbsp;
              <h1>{{ .Date.Format "2006" }}</h1>
            {{ end }}
          {{ end }}
        {{ end }}

        <!-- reset for next iteration/year -->
        {{ $.Scratch.Set "count" 1 }}

        <!-- now for all the pages/posts in specific year -->
          {{ range (where .Pages "Type" $type) }}
            {{ if (ne .Params.hidden true) }}
              <h4>
                <a href="{{ .RelPermalink }}">
                  <span>{{ .Date.Format "02 Jan" }}</span> — {{ .Title }}
                </a>
              </h4>
            {{ end }}
          {{ end }}
          
      {{ end }}
    
      </div>
  </div>
</div>

Results in:

/images/new-archives-page.png