I’m currently in the process of refactoring the Beautiful Hugo theme to remove some of the repetition in the templates. There is some enticingly similar code that I would like to move into a partial template, but I can’t quite make it work.
In layouts/index.html:
{{ if or (.Paginator.HasPrev) (.Paginator.HasNext) }}
<ul class="pager main-pager">
{{ if .Paginator.HasPrev }}
<li class="previous">
<a href="{{ .Paginator.Prev.URL }}">← {{ i18n "newerPosts" }}</a>
</li>
{{ end }}
{{ if .Paginator.HasNext }}
<li class="next">
<a href="{{ .Paginator.Next.URL }}">{{ i18n "olderPosts" }} →</a>
</li>
{{ end }}
</ul>
{{ end }}
In layouts/post/single.html:
<ul class="pager blog-pager">
{{ if .PrevInSection }}
<li class="previous">
<a href="{{ .PrevInSection.Permalink }}" data-toggle="tooltip" data-placement="top" title="{{ .PrevInSection.Title }}">← {{ i18n "previousPost" }}</a>
</li>
{{ end }}
{{ if .NextInSection }}
<li class="next">
<a href="{{ .NextInSection.Permalink }}" data-toggle="tooltip" data-placement="top" title="{{ .NextInSection.Title }}">{{ i18n "nextPost" }} →</a>
</li>
{{ end }}
</ul>
When I tried to make this into one partial, I ran into problems with .Paginator.Prev.URL
in index.html. If you just try to pass it to a partial as-is, you get an error because .Paginator.Prev
is nil if there is no previous page. (And the same with .Paginator.Next.URL
if there is no next page.) Is there a neat way of doing this?