Preface
Goal: Miscellanous range loop to achieved wordpress like widget.
We can make a sidebar, so that we can have more information, just like what common in wordpress site or other blogging platform. I’m using custom css based on Bulma box to suit example needs, and of course you can use any class other than this custom css.
Source Code
You can download the source code of this article here.
Related Article: SASS
The SASS part of this article is already discussed in this two article:
It is time to reuse the class for real world situation.
Related Article: Hugo
I also wrote about Hugo Bootstrap Aside. Step by step article, that you can read here:
Again, a rewrite.
1: Prepare
Theme
Still with tutor-04
.
-
- config.toml
- gitlab.com/…/config.toml
theme = "tutor-04"
Layout: Single
Our final layout would be:
-
- themes/tutor-04/layouts/post/single.html
- gitlab.com/…/layouts/post/single.html
{{ define "main" }}
...
{{ end }}
{{ define "aside" }}
<aside class="col-md-4">
{{ partial "widget/recent-post.html" . }}
</aside>
{{ end }}
The beauty of this Go html/template,
is that we can define other template in the same file artefact,
even when we do not really use it.
For example I put this aside-long
, in the same file artefact,
so that I can copy-paste to aside
template whenever I need.
{{ define "aside-long" }}
<aside class="col-md-4">
{{ partial "widget/recent-post.html" . }}
{{ partial "widget/archives.html" . }}
{{ partial "widget/categories.html" . }}
{{ partial "widget/tags.html" . }}
{{ partial "widget/friends.html" . }}
{{ partial "widget/related-posts.html" . }}
</aside>
{{ end }}
We are going to discuss each partial, one by one.
SASS: List
As already mention in other article, You can see the code here:
-
- themes/tutor-04/sass/css/_list.scss
- gitlab.com/…/sass/css/_list.scss.
// -- -- -- -- --
// _list.scss
...
-._
2: Simple Example
Consider start with simple example.
We need something without any loop, go html/template
tag or whatsoever.
Pure HTML with a list of URLs,
that you might need to show on your side panel.
Layout: Single
Consider examine only recent posts:
-
- themes/tutor-04/layouts/post/single.html
- gitlab.com/…/layouts/post/single.html
{{ define "aside" }}
<aside class="side column is-one-thirds is-paddingless">
{{ partial "widget/affiliates.html" . }}
</aside>
{{ end }}
Partial: Widget
-
- themes/tutor-04/layouts/partials/widget/recent-posts.html
- gitlab.com/…/layouts/partials/widget/recent-posts.html
<section class="panel is-light">
<div class="panel-header">
<p>Affiliates</p>
<span class="fa fa-child"></span>
</div>
<div class="panel-body has-background-white">
<ul class="panel-list">
<li><a href="http://epsi-rns.github.io/"
>Linux/BSD Desktop Customization</a></li>
<li><a href="http://epsi-rns.gitlab.io/"
>Mobile/Web Development Blog</a></li>
<li><a href="http://oto-spies.info/"
>Car Painting and Body Repair.</a></li>
</ul>
</div>
</section>
Server Output: Browser
Just open one of the quote post:
Now you can see the result in the browser.
This the basic of HTML class required for the rest of this article.
3: Recent Post
Layout: Single
Consider examine only recent posts:
-
- themes/tutor-04/layouts/post/single.html
- gitlab.com/…/layouts/post/single.html
{{ define "aside" }}
<aside class="side column is-one-thirds is-paddingless">
{{ partial "widget/recent-post.html" . }}
</aside>
{{ end }}
Partial: Widget
-
- themes/tutor-04/layouts/partials/widget/recent-posts.html
- gitlab.com/…/layouts/partials/widget/recent-posts.html
<section class="panel is-light">
<div class="panel-header">
<p>Recent Post</p>
<span class="fa fa-newspaper"></span>
</div>
<div class="panel-body has-background-white">
<ul class="panel-list">
{{ range first 5 (where .Site.Pages "Type" "post") }}
<li><a href="{{ .URL | absURL }}">{{ .Title }}</a></li>
{{ end }}
</ul>
</div>
</section>
The Loop
I limit the result for only first five result.
{{ range first 5 (where .Site.Pages "Type" "post") }}
...
{{ end }}
Server Output: Browser
Now you can see the result in the browser.
4: Categories and Tags
Both are Taxonomies. So the layouts is pretty similar.
Partial: Widget: Categories
-
- themes/tutor-04/layouts/partials/widget/categories.html
- gitlab.com/…/layouts/partials/widget/categories.html
<section class="panel is-light">
<div class="panel-header">
<p>Categories</p>
<span class="fa fa-tag"></span>
</div>
<div class="panel-body has-background-white">
{{ range $name, $taxonomy := .Site.Taxonomies.categories }}
<a class="tag is-small is-info" href="{{ printf "categories/%s" $name | absURL }}">
{{ $name }} <span class="fa fa-folder"></span>
</a>
{{ end }}
</div>
</section>
The Loop
The loop is self explanatory.
{{ range $name, $taxonomy := .Site.Taxonomies.categories }}
...
{{ end }}
Server Output: Browser
Now you can see the result in the browser.
Partial: Widget: Tags
Consider make the code above a little bit more complex.
We can pass argument to side panels.
Here we can have a choice of tag icon to be shown,
after
or before
.
-
- themes/tutor-04/layouts/post/single.html
- gitlab.com/…/layouts/post/single.html
{{ define "aside" }}
<aside class="side column is-one-thirds is-paddingless">
{{ partial "widget/tags.html" (dict "taxonomies" .Site.Taxonomies "view_tag_type" "before") }}
</aside>
{{ end }}
-
- themes/tutor-04/layouts/partials/widget/tags.html
- gitlab.com/…/layouts/partials/widget/tags.html
{{/* view_tag_type : 'before' or 'after' */}}
<section class="panel is-light">
<div class="panel-header">
<p>Tags</p>
<span class="fa fa-tag"></span>
</div>
<div class="panel-body has-background-white">
{{ if eq .view_tag_type "after" }}
{{ range $name, $taxonomy := .taxonomies.tags }}
<a class="tag is-small is-light has-background-white"
href="{{ printf "tags/%s" $name | absURL }}">
{{ $name }} <span class="fa fa-folder"></span>
</a>
{{ end }}
{{ else }}
{{ range $name, $taxonomy := .taxonomies.tags }}
<a class="tag is-small is-light has-background-white"
href="{{ printf "tags/%s" $name | absURL }}">
<span class="fa fa-folder"></span> {{ $name }}
</a>
{{ end }}
{{ end }}
</div>
</section>
- .
The Loop
The loop is still self explanatory.
{{ range $name, $taxonomy := .Site.Taxonomies.tags }}
...
{{ end }}
Server Output: Browser
Now you can see the result in the browser.
5: Archives
Remember the Archives tutorial in a previous chapter ? We can also apply this in similar fashion.
Partial: Widget
-
- themes/tutor-04/layouts/partials/widget/archives.html
- gitlab.com/…/layouts/partials/widget/archives.html
<section class="panel is-light">
<div class="panel-header">
<p>Archives</p>
<span class="fa fa-archive"></span>
</div>
<div class="panel-body has-background-white">
{{ $posts := where .Site.Pages "Type" "post" }}
{{ $page_year := .Page.Date.Format "2006" }}
{{ $page_month := .Page.Date.Format "January" }}
{{ range ($posts.GroupByDate "2006") }}
{{ $year := .Key }}
<div class ="archive-year" id="{{ $year }}">
<a href="{{ "pages/archives" | absURL }}#{{ $year }}">
{{ $year }}</a>
</div>
{{ if eq $year $page_year }}
<ul class="panel-archive">
{{ range (.Pages.GroupByDate "January") }}
{{ $month := .Key }}
<li class="list-month">
<span id="{{ $year }}-{{ $month }}">
<a href="{{ "pages/archives" | absURL }}#{{ $year }}-{{ $month }}">
{{ $month }}</a> - {{ $year }}</span>
{{ if eq $month $page_month }}
<ul class="panel-list">
{{ range $post_month := .Pages }}
<li>
<a href="{{ $post_month.URL | absURL }}">
{{ $post_month.Title }}
</a>
</li>
{{ end }}
</ul>
{{ end }}
</li>
{{ end }}
</ul>
{{ end }}
{{ end }}
</div>
</section>
The Loop
I know it is complex.
{{ range ($posts.GroupByDate "2006") }}
{{ $year := .Key }}
...
{{ if eq $year $page_year }}
{{ range (.Pages.GroupByDate "January") }}
{{ $month := .Key }}
...
{{ if eq $month $page_month }}
{{ range $post_month := .Pages }}
...
{{ $post_month.Title }}
...
{{ end }}
{{ end }}
{{ end }}
{{ end }}
{{ end }}
Th effort is worthy.
Server Output: Browser
Now you can see the result in the browser.
What is Next ?
There are, some interesting topic about Example Static Data with Hugo. Consider continue reading [ Hugo - Data ].
Thank you for reading.