{{ define "header" }}
  {{ partial "header.html" . }}
{{ end }}

{{ define "main" }}
<header class="page">
  <h1>{{ .Title }}</h1>
</header>

{{- $pagesByTag := dict -}}
{{- range $page := .Pages }}
  {{- range $tag := .GetTerms "tags" -}}
    {{- $tagName := $tag.Name -}}
    {{- if not (in $pagesByTag $tagName) -}}
      {{- $pagesByTag = merge $pagesByTag (dict $tagName (slice $page)) -}}
    {{- else -}}
      {{- $pagesForTag := index $pagesByTag $tagName -}}
      {{- $pagesForTag = $pagesForTag | append $page -}}
      {{- $pagesByTag = merge $pagesByTag (dict $tagName $pagesForTag) -}}
    {{- end -}}
  {{- end -}}
{{- end -}}

{{ range $tag, $pages := $pagesByTag }}
<h3>{{ $tag }}</h3>
<ul>
  {{- range $pages -}}
  <li><a href="{{ .RelPermalink }}">{{ .Title }}</a></li>
  {{- end -}}
</ul>
{{ end }}

{{ end }}

{{ define "footer" }}
  {{ partial "footer.html" . }}
{{ end }}
