Create static subpage with dynamic content

I’m trying to create a blog archive by year page that is a subpage of the blog index and I must be missing something b/c i can’t figure it out. Here’s what I’m working with:

├── archetypes
│   └── blog.md
├── content
│   └── blog
│       ├── entry-1.md
│       ├── entry-2.md
│       └── entry-3.md
└── layouts
    ├── _default
    │   └── single.html
    ├── blog
    │   ├── blog.rss.xml
    │   ├── excerpt.html
    │   ├── li.html
    │   └── single.html
    └── indexes
        └── blog.html

Now I’m trying to create an essentially static page at the url example.com/blog/archive/ with the following content:

{{ range .Data.Pages.GroupByDate "2006" }}
    <h2>{{ .Key }}</h3>
    <ul id="list">
        {{ range .Pages }}
            {{ .Render "li"}}
        {{ end }}
    </ul>
{{ end }}

I’ve been able to create a page at /blog/archive/ by utilizing the front matter url: "/blog/article/" but i can then only get static text/html to be parsed any {{ … }} tags are converted to plain text. I’m wondering what the most correct way to do something like this would be.

You need to create a layout with the logic you want. Then specify that layout in the front matter.

You can’t do it with a single file.

Okay i get it now.

├── content
│   └── archives
│       └── yearly.html
└── layouts
    └── archive
        └── single.html

Files:

content/archive/rearly.html

---
url: "/blog/archives/"
type: archive
---

hello world

But how do i get this page to pull blog posts by year? The following is just returning nothing.

layouts/archive/single.html

{{ range .Data.Pages.GroupByDate "2006" }}
  <h2>{{ .Key }}</h3>
  <ul id="list">
    {{ range .Pages }}
      <li>{{ .Title }}</li>
    {{ end }}
  </ul>
{{ end }}

How about using .Site.Pages instead of .Data.Pages?

Could have sworn i tried that but yes that works. I think I may have needed an update to (which introduced a whole bunch of other issues). Specifically here is what i needed:

{{ range (where .Site.Pages "Section" "blog").GroupByDate "2006" }}

I stumbled upon the same question and found an entirely different solution. Perhaps it’s usefull for someone.

You can define a archive index in the config (yaml) as:

indexes:
  archive: "archive"

Now for each post i set the year & month as:

archive: ["2015", "2015/01"]

Add a list template if you haven’t done so already:

{{ range .Data.Pages.ByDate }}
    <li>
        <a href="{{ .Permalink }}">{{ .Title }}</a> {{ .Date.Format "2 January, 2006" }}
    </li>
{{ end }}

And there you go a archive for each month & year

http://localhost:1313/archive/2015
http://localhost:1313/archive/2015/01

2 Likes

I’m wondering about this having ‘logic in the layout’? Can you give me some hint where to look/read about it?

The reason why I’m asking is that in my sites I have a need to mostly have static content in the form of articles, blog posts etc., but also have have need to have some kind of ‘widgets’ on certain pages like calendar showing events, or e…g. download counters for multimedia (mp3) files we provide in public area section etc.

So, what to do in such scenarion when one would like to use Hugo (as static site generator) for most of the web content, but has need for some dynamic content as well which is not worth to deploy some PHP/Python-based CMS with all their usual baggage?

A hint is to look in the documentation:

Also, use the search on this forum – a lot of this has been discussed before

On Pet, 2015-01-16 at 13:48 +0000, bjornerik wrote: