Hugo misc stuff

created onMarch 18, 2022

this is a kitchen sink for stuff that I throw in, before I forget it and to redistribute it later. The stuff in here will get redistributed to the proper pages in the near future.

create new content file

setup new web project with scaffolding, with project root dir name

hugo new site <site-name>

archetypes

Archetypes define the frontmatter of a markdown content file, with the frontmatter enclosed in two lines of three hyphens like in the example below:

--- title: 'clean code' weight: 2 date: 2018-12-06T09:29:16+10:00 background: 'https://source.unsplash.com/_v-EHHKKW3w/1600x700' align: left --- ... some markdown ...

The default archetype for a directory is defined in the archetype markdown yaml file in the directory ‘archetype’. In the example below there is a archetype ‘blog’ defined in archetypes. Creating a new markdown file with

hugo new blog/whatever.md

creates the file blog/whatever.md with the blog archetype.

. ├── archetypes │   ├── default.md │   ├── blog.md │   └── services.md ├── content │   ├── 404 │   │   └── _index.md │   ├── contact │   │   └── _index.md │   ├── homepage │   │   ├── about.md │   │   ├── index.md │   │   └── work.md │   └── _index.md ├── blog │   ├── whatever.md │   └── someother.md

taxonomies

By deafult, Hugo has two taxonomies:

  • tags
  • categories

taxonomies are listed in the frontmatter as arrays:

--- title: 'Who am I' tags: ["banana", "cherry", "kiwi"] categories: ["yummy", "tasty", "delicious"] ---

automatically created pages for taxonomies

Hugo automatically creates pages for taxonimies under

<host>:<port>/<taxonomy>/<value>

i.e. localhost:1313/tags/banana

creating custom taxonomies

Custom categories are defined in the config.toml. If custom taxonomies are defined in the config.toml, the standard Hugo taxonomies have to be included as well, otherwise the two default taxonomies are no longer available.

[taxonomies] tag = "tags" category = "categories" area = "areas"

templates

There are two different types of pages in Hugo:

  • single pages
  • list pages

Hugo templates folder structure

├── template-folder │   ├── layouts <- default templates │   │   ├── _default │   │   │   ├── list.html │   │   │   └── single.html │   │   ├── section1 <- section1 templates │   │   │   ├── list.html │   │   │   └── single.html │   │   ├── partials <- partials templates │   │   │   ├── partial1.html │   │   │   └── partial2.html │   │   ├── shortcodes <- shortcodes templates │   │   │   ├── shortcodes1.html │   │   │   └── shortcodes2.html │   │   └── index.html <- home page template

list page templates

Hugo generates list pages automatically. The automatically generated pages can be replaced with an list.html in the ‘_layouts’ folder of the template directory.

single page templates

home page template

By default, the home page template is a list page template. It can be overwritten with a file ‘index.html’ in the _layouts folder

section templates

Section templates are defined in a subfolder of ‘_layouts’, with the folder named the same as the section. Section folders can include list page templates and single page templates

baseof templates

The ‘master template’ baseof template is defined the file baseof.html in the ‘_layouts’ folder that is located directly under the web page root, that is, not the theme layouts folder.

blocks

Define a block in a template

{{ base "main" . }} some default content {{end}}

Use the block in a page template

{{ define "main" }} some specific content {{end}}

partial templates

Define the partial template in _layouts/partials/whatever.html

<h1>{{ .Title }}</h1>

Use the partial in a template:

{{ partial "whatever" . }}

The dot represents the scope of vars the partial has access to.

shortcode templates

Shortcode templates a partial templates that can be used in markdown files, that is, content files.

Define the shortcode in a file named like the shortcode that is going to be used, i.e. whatever.html for the ‘whatever’ shortcode.

Shortcodes are accessed with

{{ < shortcode1 > }}

passing vars and params to shortcodes

Define the shortcode:

<p style color="{{.Get `color`}}";>some colored text</p>

Note that inside css the double parenthesis do not work, but backticks do the trick.

The parameter can also be accessed with its position:

<p style color="{{.Get 0}}";>some colored text</p>

Use the shortcode in a markdown file:

{{ < shortcode1 color="blue" > }}

shortcodes with opening and closing ‘tags

Shortcodes can also be used with opening and closing ‘tag’ of the shortcode:

{{ < shortcode2 > }} {{ < /shortcode2 > }}

The content between the opening and closing tag can be accessed within the shortcode:

{{.Inner}}

markdown in shortcodes with opening and closing ‘tags

By default, markdown between the opening and closing tag of the shortcode is not processed, i.e. marking a text as bold like

**whatever**

does not work. To change this behaviour define the shortcode with the percent sign instead of angle brackets:

{{ % shortcode2 % }} {{ % /shortcode2 % }}

template vars

As the name suggests, template vars can only be used within templates.

predefined template vars

insert content

{{.Content}}

custom vars

custom front matter vars

define the custom var in the front matter:

--- ... myCustomVar: "some value" ---

Access the custom var in a template with

{{ .Params.myCustomVar }}

custom vars

define the custom var

{{ $myCustomVar := "some string" }}

or

{{ $myCustomVar := 7 }}

Access the custom var with

{{ $myCustomVar }}