<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Yihao</title>
    <description>The latest articles on DEV Community by Yihao (@yihao).</description>
    <link>https://dev.to/yihao</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F624698%2F945eb290-3c05-4e52-9dc5-9100448d367d.png</url>
      <title>DEV Community: Yihao</title>
      <link>https://dev.to/yihao</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/yihao"/>
    <language>en</language>
    <item>
      <title>Building a Hugo Theme</title>
      <dc:creator>Yihao</dc:creator>
      <pubDate>Mon, 03 May 2021 09:54:46 +0000</pubDate>
      <link>https://dev.to/yihao/building-a-hugo-theme-4moo</link>
      <guid>https://dev.to/yihao/building-a-hugo-theme-4moo</guid>
      <description>&lt;p&gt;&lt;em&gt;Alert: this is not a step-by-step guide on how to build a Hugo theme. It focuses on the overall picture and mental model that I built up during the process of learning. I hope this can help me pick up Hugo again quickly in the future when I need to. But I hope some of you will find it helpful, too :)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I've built a &lt;a href="https://gohugo.io/"&gt;Hugo&lt;/a&gt; theme, &lt;a href="https://coderyihaowang.github.io/yBlog"&gt;yBlog&lt;/a&gt;, for my personal blog. Here are some take-aways.&lt;/p&gt;

&lt;h2&gt;
  
  
  Mental model
&lt;/h2&gt;

&lt;p&gt;As a content writer, we write contents in markdown, put them in correct folders, add some meta-data in front matter, and that's it. Hugo takes care of everything else for us. But if we want to build our own themes, we need to understand a bit more about how Hugo works behind the scenes. The below diagram shows my mental model when I use Hugo:&lt;/p&gt;

&lt;p&gt;&lt;a href="/images/building-a-hugo-theme/Hugo-mental-model.png" class="article-body-image-wrapper"&gt;&lt;img src="/images/building-a-hugo-theme/Hugo-mental-model.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is perhaps an over-simplified version of Hugo's work flow. If you look at &lt;a href="https://github.com/gohugoio/hugo"&gt;Hugo's source code&lt;/a&gt;, you'll find the actual implementation much more complicated. But I find this mental model helpful for getting us thinking in Hugo. &lt;/p&gt;

&lt;p&gt;To summarise Hugo's model in one sentence: &lt;strong&gt;everything is a page&lt;/strong&gt;, and every page has a corresponding model that provides all the information needed to render it. This model is really the central piece of Hugo's flow. Everything you write will become its fields, and everything you see on the rendered pages are retrieved from it. You provide data to the model by writing markdown (including front matter), and you retrieve data in the templates by accessing the model's fields and methods. Unfortunately, you won't be able to find this "model" in any documentation, because it only exists in our mental model, the real implementation is much more sophisticated. You can refer to &lt;a href="https://gohugo.io/variables/"&gt;variables in hugo&lt;/a&gt;, they are corresponding to the model "fields" that we are talking about here.&lt;/p&gt;

&lt;p&gt;So, let's have a closer look at the data flow around this model, it can be separated into three steps (as shown in the diagram above):&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Hugo parses the markdown contents and the front matter, as well as the config file, gathering information into models (providing information)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Models are mapped to corresponding templates (grouping information)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Templates consume the information carried in the models, rendering them in HTML (rendering information)&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Providing information
&lt;/h2&gt;

&lt;p&gt;Information is the raw material for building the website. They come from 3 sources: the content markdown files, the &lt;code&gt;config.toml/yaml/json&lt;/code&gt; file, and the Hugo engine.&lt;/p&gt;

&lt;h3&gt;
  
  
  Information from contents
&lt;/h3&gt;

&lt;p&gt;The most notable piece of information is the markdown content itself. This content will be available as a page variable, and can be retrieved using &lt;code&gt;.Content&lt;/code&gt; in the page template (will explain shortly).&lt;/p&gt;

&lt;p&gt;We are also able to provide other information that is specific to a particular page. These are added as &lt;a href="https://gohugo.io/content-management/front-matter"&gt;front matter&lt;/a&gt; at the top of the content file. Some fields are &lt;a href="https://gohugo.io/content-management/front-matter#front-matter-formats"&gt;predefined&lt;/a&gt; by Hugo, including &lt;code&gt;title&lt;/code&gt;, &lt;code&gt;description&lt;/code&gt; and so on. When they are defined in the front matter, they are available for use in templates via their names (eg &lt;code&gt;.Title&lt;/code&gt;, &lt;code&gt;.Description&lt;/code&gt;, ...). &lt;/p&gt;

&lt;p&gt;Other than these predefined fields, we can also add whatever information we want to front matter. These user-defined fields will also be available in the page template. They are grouped together in the &lt;code&gt;.Params&lt;/code&gt; field. For example, if we add a &lt;code&gt;note&lt;/code&gt; field to front matter, as this is not Hugo predefiend, we can later use it via &lt;code&gt;.Params.Note&lt;/code&gt; in the templates.&lt;/p&gt;

&lt;h3&gt;
  
  
  Information from config
&lt;/h3&gt;

&lt;p&gt;We'll also need some information that are website-wise, for example, the base URL of the website, the website's author, etc. They belong to the &lt;code&gt;config.toml&lt;/code&gt; file. When you provide these fields in the config file, they become available via the &lt;code&gt;.Site&lt;/code&gt; template variable. For example, &lt;code&gt;baseURL = 'https://my.domain'&lt;/code&gt; in &lt;code&gt;config.toml&lt;/code&gt; becomes &lt;code&gt;.Site.BaseURL&lt;/code&gt; in the templates.&lt;/p&gt;

&lt;h3&gt;
  
  
  Information from Hugo engine
&lt;/h3&gt;

&lt;p&gt;During the building process, Hugo also adds a great amount of helpful information for us to use in templates. These include &lt;code&gt;.Permalink&lt;/code&gt;, &lt;code&gt;.TableOfContents&lt;/code&gt;, &lt;code&gt;.WordCount&lt;/code&gt;, &lt;code&gt;.ReadingTime&lt;/code&gt; and many more. Refer to the documentation of &lt;a href="https://gohugo.io/variables/page/"&gt;page variables&lt;/a&gt; and &lt;a href="https://gohugo.io/variables/site/"&gt;site variables&lt;/a&gt; to see what else are available.&lt;/p&gt;

&lt;h2&gt;
  
  
  Grouping information
&lt;/h2&gt;

&lt;p&gt;The information gathered from the above step will be grouped into models and dispatched to templates. Basically, the templates come in two versions (&lt;code&gt;Kind&lt;/code&gt;s, in Hugo's term): single page templates and list templates.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Single page templates&lt;/strong&gt; display individual contents. For example, a single markdown file that you put under the contents folder will be forwarded to a single page template.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;List templates&lt;/strong&gt; don't display the content of an individual content file; they are used to provide a summary page of a certain collection. For example, section, taxonomy, etc. For example, the &lt;a href="https://coderyihaowang.github.io/yBlog/blog"&gt;blogs&lt;/a&gt; page of the example site of my theme is rendered by a list template. That said, you CAN create a file corresponding to a particular list template; this way, you can provide front matter to the model of a list page. &lt;a href="https://gohugo.io/templates/lists/#add-content-and-front-matter-to-list-pages"&gt;see here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;By default, you should see these three default template files under the &lt;code&gt;layouts/_default&lt;/code&gt; folder:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;_default/
  |- single.html
  |- list.html
  `- baseof.html
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;single.html&lt;/code&gt; is the default single page template. Your individual content pages will be rendered by this template. &lt;code&gt;list.html&lt;/code&gt; is the default list template, you can use it to display the summary of a section. Obviously, Hugo supports many other types of templates, but those are basically special cases of either single page template or list page template. You can read more about Hugo's template lookup order &lt;a href="https://gohugo.io/templates/lookup-order/"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Note here &lt;code&gt;baseof.html&lt;/code&gt; is the base of all other templates. You can add the elements common to all pages here, like the head part of html. Other templates can be embedded inside this one. We'll explain more of template structuring in the next section.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rendering information
&lt;/h2&gt;

&lt;p&gt;Finally, the models go to corresponding templates, and we can retrieve the information we provided earlier in the contents and config file. To retrieve a field in model and insert it into the template, put it inside a pair of mustache symbol: &lt;code&gt;{{  }}&lt;/code&gt;. For example, &lt;code&gt;&amp;lt;h1&amp;gt;{{ .Title }}&amp;lt;/h1&amp;gt;&lt;/code&gt; in the template will be rendered as &lt;code&gt;&amp;lt;h1&amp;gt;[value of the model's 'title' field]&amp;lt;/h1&amp;gt;&lt;/code&gt; in the final output. Hugo uses Go's &lt;a href="https://golang.org/pkg/text/template/"&gt;templating language&lt;/a&gt;, which is very powerful and interesting to work with. You can read about it in &lt;a href="https://gohugo.io/templates/introduction/"&gt;Hugo's documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Hugo's templates can be embedded and make use of &lt;a href="https://gohugo.io/templates/partials/"&gt;partials&lt;/a&gt; to reuse components. To show an example, here is the &lt;code&gt;baseof.html&lt;/code&gt; of my theme:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"sticky"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"root"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    {{- partial "head.html" . -}}
    &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
        {{- partial "header.html" . -}}
        &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"content"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        {{- block "main" . }}{{- end }}
        &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
        {{- partial "footer.html" . -}}
        {{- partial "script.html" . -}}
    &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;{{- block "main" . }}{{- end }}&lt;/code&gt; part shows how to use blocks to define the outer shell of a template. My &lt;code&gt;_default/single.html&lt;/code&gt; is defined as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;{{ define "main" }}
    &lt;span class="c"&gt;&amp;lt;!-- content of a single page --&amp;gt;&lt;/span&gt;
{{ end }}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The content of the single page template will be embedded into &lt;code&gt;baseof.html&lt;/code&gt; template in the &lt;code&gt;block "main" .&lt;/code&gt; part. The dot (&lt;code&gt;.&lt;/code&gt;) after &lt;code&gt;"main"&lt;/code&gt; is the context passed to embedded templates (similar to function argument, but each template only has one context). You can read more about base template and blocks &lt;a href="https://gohugo.io/templates/base/"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;{{- partial "script.html" . -}}&lt;/code&gt; part shows how to use partial templates. I have created a &lt;code&gt;script.html&lt;/code&gt; template under the &lt;code&gt;partials&lt;/code&gt; folder with the following content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;{{ $script := resources.Get "/js/script.js" | minify }}
&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"{{ $script.RelPermalink }}"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This piece will be inserted into the &lt;code&gt;partial "script.html ."&lt;/code&gt; part. This way, we can decouple parts of a template and improve maintainability.&lt;/p&gt;

&lt;p&gt;You can read more about partial templates &lt;a href="https://gohugo.io/templates/partials/"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrap up
&lt;/h2&gt;

&lt;p&gt;Right, that's it. As said this article is not a step-by-step toturial but aims to provide a high level overview of how Hugo works. There are a lot of details I cannot cover here, but it summarises what I learned so far, and hopefully it will be useful when I leave Hugo for some time and come back one day. For detailed information on Hugo, go to the &lt;a href="https://gohugo.io/documentation/"&gt;documentation&lt;/a&gt;, and they also have a good &lt;a href="https://gohugo.io/getting-started/quick-start/"&gt;tutorial&lt;/a&gt; to get you started.&lt;/p&gt;

</description>
      <category>hugo</category>
      <category>go</category>
      <category>theme</category>
      <category>css</category>
    </item>
  </channel>
</rss>
