├── LICENSE.md ├── README.md ├── archetypes └── default.md ├── exampleSite ├── config.toml ├── content │ ├── about.md │ └── post │ │ ├── creating-a-new-theme.md │ │ ├── goisforlovers.md │ │ ├── hugoisforlovers.md │ │ ├── math-example.md │ │ └── migrate-from-jekyll.md └── static │ └── .gitkeep ├── gulpfile.js ├── images ├── screenshot.png ├── screenshot_github.png └── tn.png ├── layouts ├── 404.html ├── _default │ ├── list.html │ └── single.html ├── index.html └── partials │ ├── disqus.html │ ├── footer.html │ ├── header.html │ ├── nav.html │ ├── pager.html │ ├── sharing.html │ └── tags.html ├── package.json ├── sass └── style.sass ├── static ├── css │ └── style.css └── wave.ico └── theme.toml /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Morten G. Johansen 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Hucore 2 | 3 | Hucore is a minimal blog theme for [hugo](https://gohugo.io). The theme is based on [Hemingway2](https://gitlab.com/beli3ver/hemingway2). 4 | 5 | ## Features 6 | 7 | * Responsive & minimal design 8 | * Disqus support 9 | * Google Analytics 10 | * Basic [OpenGraph](http://ogp.me/) metadata support 11 | * Option for social sharing icons on posts 12 | * Option for author on posts 13 | 14 | ## Screenshot 15 | 16 | ![](https://raw.githubusercontent.com/mgjohansen/hucore/master/images/screenshot_github.png) 17 | 18 | See [technet.cc](https://technet.cc) for an example of this theme in use. 19 | 20 | ## Getting Started 21 | 22 | Clone this repository to your hugo theme directory. 23 | 24 | ``` 25 | mkdir themes 26 | cd themes 27 | git clone https://github.com/mgjohansen/hucore.git 28 | ``` 29 | 30 | ## Configuration 31 | 32 | Take a look in the [exampleSite](https://github.com/mgjohansen/hucore/tree/master/exampleSite) folder. 33 | 34 | This directory contains an example config file and the content for the demo. 35 | It serves as an example setup for your documentation. 36 | 37 | Copy the `config.toml` in the root directory of your website. Overwrite the existing config file if necessary. 38 | 39 | __[config.toml](https://github.com/mgjohansen/hucore/blob/master/exampleSite/config.toml)__: 40 | 41 | ```toml 42 | baseurl = "https://example.com" 43 | languageCode = "en-US" 44 | title = "[Hu]Core" 45 | theme = "Hucore" 46 | copyright = "© 2017 | Follow on Twitter | [Hu]Core theme & Hugo ♥" 47 | disqusShortname = "shortname" 48 | googleAnalytics = "trackingcode" 49 | 50 | [taxonomies] 51 | tag = "tags" 52 | category = "categories" 53 | 54 | [params] 55 | description = "Your description here" 56 | keywords = ["keyword 1", "keyword 2", "keyword 3"] 57 | author = "Morten Johansen" 58 | sharingicons = true 59 | displayauthor = true 60 | 61 | [params.highlight] 62 | style = "github" 63 | languages = ["go", "dockerfile"] 64 | 65 | [[params.social]] 66 | url = "https://github.com/mgjohansen" 67 | fa_icon = "fa-github" 68 | 69 | [[params.social]] 70 | url = "https://gitlab.com/mgjohansen" 71 | fa_icon = "fa-gitlab" 72 | 73 | [[params.social]] 74 | url = "https://twitter.com/mgjohansen" 75 | fa_icon = "fa-twitter" 76 | 77 | [[params.social]] 78 | url = "https://linkedin.com/in/mgjohansen" 79 | fa_icon = "fa-linkedin-square" 80 | 81 | [[params.social]] 82 | url = "/index.xml" 83 | fa_icon = "fa-rss" 84 | 85 | [[params.socialshare]] 86 | url = "https://linkedin.com/in/mgjohansen" 87 | fa_icon = "fa-linkedin-square" 88 | ``` 89 | 90 | ## Build 91 | 92 | ``` 93 | hugo server 94 | ``` 95 | 96 | You can go to localhost:1313 and this theme should be visible. 97 | 98 | ## License 99 | 100 | Hucore is licensed under the [MIT License](LICENSE.md). 101 | 102 | ## Author 103 | 104 | [Morten Johansen](https://github.com/mgjohansen) 105 | 106 | ## Credits 107 | 108 | Hucore is based on [Hemingway2](https://gitlab.com/beli3ver/hemingway2) created by [Malte Kiefer](https://github.com/beli3ver). 109 | -------------------------------------------------------------------------------- /archetypes/default.md: -------------------------------------------------------------------------------- 1 | +++ 2 | title = "{{ replace .TranslationBaseName "-" " " | title }}" 3 | tags = [ ] 4 | draft = true 5 | date = {{ .Date }} 6 | author = "" 7 | +++ 8 | -------------------------------------------------------------------------------- /exampleSite/config.toml: -------------------------------------------------------------------------------- 1 | baseurl = "https://example.com" 2 | languageCode = "en-US" 3 | title = "[Hu]Core" 4 | theme = "Hucore" 5 | copyright = "© 2017 | Follow on Twitter | [Hu]Core theme & Hugo ♥" 6 | disqusShortname = "shortname" 7 | googleAnalytics = "trackingcode" 8 | 9 | [taxonomies] 10 | tag = "tags" 11 | category = "categories" 12 | 13 | [params] 14 | description = "Your description here" 15 | keywords = ["keyword 1", "keyword 2", "keyword 3"] 16 | author = "Morten Johansen" 17 | sharingicons = true 18 | displayauthor = true 19 | 20 | [params.highlight] 21 | style = "github" 22 | languages = ["go", "dockerfile"] 23 | 24 | [[params.social]] 25 | url = "https://github.com/mgjohansen" 26 | fa_icon = "fa-github" 27 | 28 | [[params.social]] 29 | url = "https://gitlab.com/mgjohansen" 30 | fa_icon = "fa-gitlab" 31 | 32 | [[params.social]] 33 | url = "https://twitter.com/mgjohansen" 34 | fa_icon = "fa-twitter" 35 | 36 | [[params.social]] 37 | url = "https://linkedin.com/in/mgjohansen" 38 | fa_icon = "fa-linkedin-square" 39 | 40 | [[params.social]] 41 | url = "/index.xml" 42 | fa_icon = "fa-rss" 43 | 44 | [[params.socialshare]] 45 | url = "https://linkedin.com/in/mgjohansen" 46 | fa_icon = "fa-linkedin-square" -------------------------------------------------------------------------------- /exampleSite/content/about.md: -------------------------------------------------------------------------------- 1 | +++ 2 | title = "About Hugo" 3 | date = "2014-04-09" 4 | menu = "main" 5 | +++ 6 | 7 | Hugo is a static site engine written in Go. 8 | 9 | 10 | It makes use of a variety of open source projects including: 11 | 12 | * [Cobra](https://github.com/spf13/cobra) 13 | * [Viper](https://github.com/spf13/viper) 14 | * [J Walter Weatherman](https://github.com/spf13/jWalterWeatherman) 15 | * [Cast](https://github.com/spf13/cast) 16 | 17 | Learn more and contribute on [GitHub](https://github.com/spf13). 18 | 19 | ## Setup 20 | 21 | Some fun facts about [Hugo](http://gohugo.io/): 22 | 23 | * Built in [Go](http://golang.org/) 24 | * Loosely inspired by [Jekyll](http://jekyllrb.com/) 25 | * Primarily developed by [spf13](http://spf13.com/) on the train while commuting to and from Manhattan. 26 | * Coded in [Vim](http://vim.org) using [spf13-vim](http://vim.spf13.com/) 27 | 28 | Have questions or suggestions? Feel free to [open an issue on GitHub](https://github.com/spf13/hugo/issues/new) or [ask me on Twitter](https://twitter.com/spf13). 29 | 30 | Thanks for reading! 31 | -------------------------------------------------------------------------------- /exampleSite/content/post/creating-a-new-theme.md: -------------------------------------------------------------------------------- 1 | --- 2 | author: "Michael Henderson" 3 | date: 2014-09-28 4 | linktitle: Creating a New Theme 5 | menu: 6 | main: 7 | parent: tutorials 8 | next: /tutorials/github-pages-blog 9 | prev: /tutorials/automated-deployments 10 | title: Creating a New Theme 11 | weight: 10 12 | --- 13 | 14 | 15 | ## Introduction 16 | 17 | This tutorial will show you how to create a simple theme in Hugo. I assume that you are familiar with HTML, the bash command line, and that you are comfortable using Markdown to format content. I'll explain how Hugo uses templates and how you can organize your templates to create a theme. I won't cover using CSS to style your theme. 18 | 19 | We'll start with creating a new site with a very basic template. Then we'll add in a few pages and posts. With small variations on that, you will be able to create many different types of web sites. 20 | 21 | In this tutorial, commands that you enter will start with the "$" prompt. The output will follow. Lines that start with "#" are comments that I've added to explain a point. When I show updates to a file, the ":wq" on the last line means to save the file. 22 | 23 | Here's an example: 24 | 25 | ``` 26 | ## this is a comment 27 | $ echo this is a command 28 | this is a command 29 | 30 | ## edit the file 31 | $vi foo.md 32 | +++ 33 | date = "2014-09-28" 34 | title = "creating a new theme" 35 | +++ 36 | 37 | bah and humbug 38 | :wq 39 | 40 | ## show it 41 | $ cat foo.md 42 | +++ 43 | date = "2014-09-28" 44 | title = "creating a new theme" 45 | +++ 46 | 47 | bah and humbug 48 | $ 49 | ``` 50 | 51 | 52 | ## Some Definitions 53 | 54 | There are a few concepts that you need to understand before creating a theme. 55 | 56 | ### Skins 57 | 58 | Skins are the files responsible for the look and feel of your site. It’s the CSS that controls colors and fonts, it’s the Javascript that determines actions and reactions. It’s also the rules that Hugo uses to transform your content into the HTML that the site will serve to visitors. 59 | 60 | You have two ways to create a skin. The simplest way is to create it in the ```layouts/``` directory. If you do, then you don’t have to worry about configuring Hugo to recognize it. The first place that Hugo will look for rules and files is in the ```layouts/``` directory so it will always find the skin. 61 | 62 | Your second choice is to create it in a sub-directory of the ```themes/``` directory. If you do, then you must always tell Hugo where to search for the skin. It’s extra work, though, so why bother with it? 63 | 64 | The difference between creating a skin in ```layouts/``` and creating it in ```themes/``` is very subtle. A skin in ```layouts/``` can’t be customized without updating the templates and static files that it is built from. A skin created in ```themes/```, on the other hand, can be and that makes it easier for other people to use it. 65 | 66 | The rest of this tutorial will call a skin created in the ```themes/``` directory a theme. 67 | 68 | Note that you can use this tutorial to create a skin in the ```layouts/``` directory if you wish to. The main difference will be that you won’t need to update the site’s configuration file to use a theme. 69 | 70 | ### The Home Page 71 | 72 | The home page, or landing page, is the first page that many visitors to a site see. It is the index.html file in the root directory of the web site. Since Hugo writes files to the public/ directory, our home page is public/index.html. 73 | 74 | ### Site Configuration File 75 | 76 | When Hugo runs, it looks for a configuration file that contains settings that override default values for the entire site. The file can use TOML, YAML, or JSON. I prefer to use TOML for my configuration files. If you prefer to use JSON or YAML, you’ll need to translate my examples. You’ll also need to change the name of the file since Hugo uses the extension to determine how to process it. 77 | 78 | Hugo translates Markdown files into HTML. By default, Hugo expects to find Markdown files in your ```content/``` directory and template files in your ```themes/``` directory. It will create HTML files in your ```public/``` directory. You can change this by specifying alternate locations in the configuration file. 79 | 80 | ### Content 81 | 82 | Content is stored in text files that contain two sections. The first section is the “front matter,” which is the meta-information on the content. The second section contains Markdown that will be converted to HTML. 83 | 84 | #### Front Matter 85 | 86 | The front matter is information about the content. Like the configuration file, it can be written in TOML, YAML, or JSON. Unlike the configuration file, Hugo doesn’t use the file’s extension to know the format. It looks for markers to signal the type. TOML is surrounded by “`+++`”, YAML by “`---`”, and JSON is enclosed in curly braces. I prefer to use TOML, so you’ll need to translate my examples if you prefer YAML or JSON. 87 | 88 | The information in the front matter is passed into the template before the content is rendered into HTML. 89 | 90 | #### Markdown 91 | 92 | Content is written in Markdown which makes it easier to create the content. Hugo runs the content through a Markdown engine to create the HTML which will be written to the output file. 93 | 94 | ### Template Files 95 | 96 | Hugo uses template files to render content into HTML. Template files are a bridge between the content and presentation. Rules in the template define what content is published, where it's published to, and how it will rendered to the HTML file. The template guides the presentation by specifying the style to use. 97 | 98 | There are three types of templates: single, list, and partial. Each type takes a bit of content as input and transforms it based on the commands in the template. 99 | 100 | Hugo uses its knowledge of the content to find the template file used to render the content. If it can’t find a template that is an exact match for the content, it will shift up a level and search from there. It will continue to do so until it finds a matching template or runs out of templates to try. If it can’t find a template, it will use the default template for the site. 101 | 102 | Please note that you can use the front matter to influence Hugo’s choice of templates. 103 | 104 | #### Single Template 105 | 106 | A single template is used to render a single piece of content. For example, an article or post would be a single piece of content and use a single template. 107 | 108 | #### List Template 109 | 110 | A list template renders a group of related content. That could be a summary of recent postings or all articles in a category. List templates can contain multiple groups. 111 | 112 | The homepage template is a special type of list template. Hugo assumes that the home page of your site will act as the portal for the rest of the content in the site. 113 | 114 | #### Partial Template 115 | 116 | A partial template is a template that can be included in other templates. Partial templates must be called using the “partial” template command. They are very handy for rolling up common behavior. For example, your site may have a banner that all pages use. Instead of copying the text of the banner into every single and list template, you could create a partial with the banner in it. That way if you decide to change the banner, you only have to change the partial template. 117 | 118 | ## Create a New Site 119 | 120 | Let's use Hugo to create a new web site. I'm a Mac user, so I'll create mine in my home directory, in the Sites folder. If you're using Linux, you might have to create the folder first. 121 | 122 | The "new site" command will create a skeleton of a site. It will give you the basic directory structure and a useable configuration file. 123 | 124 | ``` 125 | $ hugo new site ~/Sites/zafta 126 | $ cd ~/Sites/zafta 127 | $ ls -l 128 | total 8 129 | drwxr-xr-x 7 quoha staff 238 Sep 29 16:49 . 130 | drwxr-xr-x 3 quoha staff 102 Sep 29 16:49 .. 131 | drwxr-xr-x 2 quoha staff 68 Sep 29 16:49 archetypes 132 | -rw-r--r-- 1 quoha staff 82 Sep 29 16:49 config.toml 133 | drwxr-xr-x 2 quoha staff 68 Sep 29 16:49 content 134 | drwxr-xr-x 2 quoha staff 68 Sep 29 16:49 layouts 135 | drwxr-xr-x 2 quoha staff 68 Sep 29 16:49 static 136 | $ 137 | ``` 138 | 139 | Take a look in the content/ directory to confirm that it is empty. 140 | 141 | The other directories (archetypes/, layouts/, and static/) are used when customizing a theme. That's a topic for a different tutorial, so please ignore them for now. 142 | 143 | ### Generate the HTML For the New Site 144 | 145 | Running the `hugo` command with no options will read all the available content and generate the HTML files. It will also copy all static files (that's everything that's not content). Since we have an empty site, it won't do much, but it will do it very quickly. 146 | 147 | ``` 148 | $ hugo --verbose 149 | INFO: 2014/09/29 Using config file: config.toml 150 | INFO: 2014/09/29 syncing from /Users/quoha/Sites/zafta/static/ to /Users/quoha/Sites/zafta/public/ 151 | WARN: 2014/09/29 Unable to locate layout: [index.html _default/list.html _default/single.html] 152 | WARN: 2014/09/29 Unable to locate layout: [404.html] 153 | 0 draft content 154 | 0 future content 155 | 0 pages created 156 | 0 tags created 157 | 0 categories created 158 | in 2 ms 159 | $ 160 | ``` 161 | 162 | The "`--verbose`" flag gives extra information that will be helpful when we build the template. Every line of the output that starts with "INFO:" or "WARN:" is present because we used that flag. The lines that start with "WARN:" are warning messages. We'll go over them later. 163 | 164 | We can verify that the command worked by looking at the directory again. 165 | 166 | ``` 167 | $ ls -l 168 | total 8 169 | drwxr-xr-x 2 quoha staff 68 Sep 29 16:49 archetypes 170 | -rw-r--r-- 1 quoha staff 82 Sep 29 16:49 config.toml 171 | drwxr-xr-x 2 quoha staff 68 Sep 29 16:49 content 172 | drwxr-xr-x 2 quoha staff 68 Sep 29 16:49 layouts 173 | drwxr-xr-x 4 quoha staff 136 Sep 29 17:02 public 174 | drwxr-xr-x 2 quoha staff 68 Sep 29 16:49 static 175 | $ 176 | ``` 177 | 178 | See that new public/ directory? Hugo placed all generated content there. When you're ready to publish your web site, that's the place to start. For now, though, let's just confirm that we have what we'd expect from a site with no content. 179 | 180 | ``` 181 | $ ls -l public 182 | total 16 183 | -rw-r--r-- 1 quoha staff 416 Sep 29 17:02 index.xml 184 | -rw-r--r-- 1 quoha staff 262 Sep 29 17:02 sitemap.xml 185 | $ 186 | ``` 187 | 188 | Hugo created two XML files, which is standard, but there are no HTML files. 189 | 190 | 191 | 192 | ### Test the New Site 193 | 194 | Verify that you can run the built-in web server. It will dramatically shorten your development cycle if you do. Start it by running the "server" command. If it is successful, you will see output similar to the following: 195 | 196 | ``` 197 | $ hugo server --verbose 198 | INFO: 2014/09/29 Using config file: /Users/quoha/Sites/zafta/config.toml 199 | INFO: 2014/09/29 syncing from /Users/quoha/Sites/zafta/static/ to /Users/quoha/Sites/zafta/public/ 200 | WARN: 2014/09/29 Unable to locate layout: [index.html _default/list.html _default/single.html] 201 | WARN: 2014/09/29 Unable to locate layout: [404.html] 202 | 0 draft content 203 | 0 future content 204 | 0 pages created 205 | 0 tags created 206 | 0 categories created 207 | in 2 ms 208 | Serving pages from /Users/quoha/Sites/zafta/public 209 | Web Server is available at http://localhost:1313 210 | Press Ctrl+C to stop 211 | ``` 212 | 213 | Connect to the listed URL (it's on the line that starts with "Web Server"). If everything is working correctly, you should get a page that shows the following: 214 | 215 | ``` 216 | index.xml 217 | sitemap.xml 218 | ``` 219 | 220 | That's a listing of your public/ directory. Hugo didn't create a home page because our site has no content. When there's no index.html file in a directory, the server lists the files in the directory, which is what you should see in your browser. 221 | 222 | Let’s go back and look at those warnings again. 223 | 224 | ``` 225 | WARN: 2014/09/29 Unable to locate layout: [index.html _default/list.html _default/single.html] 226 | WARN: 2014/09/29 Unable to locate layout: [404.html] 227 | ``` 228 | 229 | That second warning is easier to explain. We haven’t created a template to be used to generate “page not found errors.” The 404 message is a topic for a separate tutorial. 230 | 231 | Now for the first warning. It is for the home page. You can tell because the first layout that it looked for was “index.html.” That’s only used by the home page. 232 | 233 | I like that the verbose flag causes Hugo to list the files that it's searching for. For the home page, they are index.html, _default/list.html, and _default/single.html. There are some rules that we'll cover later that explain the names and paths. For now, just remember that Hugo couldn't find a template for the home page and it told you so. 234 | 235 | At this point, you've got a working installation and site that we can build upon. All that’s left is to add some content and a theme to display it. 236 | 237 | ## Create a New Theme 238 | 239 | Hugo doesn't ship with a default theme. There are a few available (I counted a dozen when I first installed Hugo) and Hugo comes with a command to create new themes. 240 | 241 | We're going to create a new theme called "zafta." Since the goal of this tutorial is to show you how to fill out the files to pull in your content, the theme will not contain any CSS. In other words, ugly but functional. 242 | 243 | All themes have opinions on content and layout. For example, Zafta uses "post" over "blog". Strong opinions make for simpler templates but differing opinions make it tougher to use themes. When you build a theme, consider using the terms that other themes do. 244 | 245 | 246 | ### Create a Skeleton 247 | 248 | Use the hugo "new" command to create the skeleton of a theme. This creates the directory structure and places empty files for you to fill out. 249 | 250 | ``` 251 | $ hugo new theme zafta 252 | 253 | $ ls -l 254 | total 8 255 | drwxr-xr-x 2 quoha staff 68 Sep 29 16:49 archetypes 256 | -rw-r--r-- 1 quoha staff 82 Sep 29 16:49 config.toml 257 | drwxr-xr-x 2 quoha staff 68 Sep 29 16:49 content 258 | drwxr-xr-x 2 quoha staff 68 Sep 29 16:49 layouts 259 | drwxr-xr-x 4 quoha staff 136 Sep 29 17:02 public 260 | drwxr-xr-x 2 quoha staff 68 Sep 29 16:49 static 261 | drwxr-xr-x 3 quoha staff 102 Sep 29 17:31 themes 262 | 263 | $ find themes -type f | xargs ls -l 264 | -rw-r--r-- 1 quoha staff 1081 Sep 29 17:31 themes/zafta/LICENSE.md 265 | -rw-r--r-- 1 quoha staff 0 Sep 29 17:31 themes/zafta/archetypes/default.md 266 | -rw-r--r-- 1 quoha staff 0 Sep 29 17:31 themes/zafta/layouts/_default/list.html 267 | -rw-r--r-- 1 quoha staff 0 Sep 29 17:31 themes/zafta/layouts/_default/single.html 268 | -rw-r--r-- 1 quoha staff 0 Sep 29 17:31 themes/zafta/layouts/index.html 269 | -rw-r--r-- 1 quoha staff 0 Sep 29 17:31 themes/zafta/layouts/partials/footer.html 270 | -rw-r--r-- 1 quoha staff 0 Sep 29 17:31 themes/zafta/layouts/partials/header.html 271 | -rw-r--r-- 1 quoha staff 93 Sep 29 17:31 themes/zafta/theme.toml 272 | $ 273 | ``` 274 | 275 | The skeleton includes templates (the files ending in .html), license file, a description of your theme (the theme.toml file), and an empty archetype. 276 | 277 | Please take a minute to fill out the theme.toml and LICENSE.md files. They're optional, but if you're going to be distributing your theme, it tells the world who to praise (or blame). It's also nice to declare the license so that people will know how they can use the theme. 278 | 279 | ``` 280 | $ vi themes/zafta/theme.toml 281 | author = "michael d henderson" 282 | description = "a minimal working template" 283 | license = "MIT" 284 | name = "zafta" 285 | source_repo = "" 286 | tags = ["tags", "categories"] 287 | :wq 288 | 289 | ## also edit themes/zafta/LICENSE.md and change 290 | ## the bit that says "YOUR_NAME_HERE" 291 | ``` 292 | 293 | Note that the the skeleton's template files are empty. Don't worry, we'll be changing that shortly. 294 | 295 | ``` 296 | $ find themes/zafta -name '*.html' | xargs ls -l 297 | -rw-r--r-- 1 quoha staff 0 Sep 29 17:31 themes/zafta/layouts/_default/list.html 298 | -rw-r--r-- 1 quoha staff 0 Sep 29 17:31 themes/zafta/layouts/_default/single.html 299 | -rw-r--r-- 1 quoha staff 0 Sep 29 17:31 themes/zafta/layouts/index.html 300 | -rw-r--r-- 1 quoha staff 0 Sep 29 17:31 themes/zafta/layouts/partials/footer.html 301 | -rw-r--r-- 1 quoha staff 0 Sep 29 17:31 themes/zafta/layouts/partials/header.html 302 | $ 303 | ``` 304 | 305 | 306 | 307 | ### Update the Configuration File to Use the Theme 308 | 309 | Now that we've got a theme to work with, it's a good idea to add the theme name to the configuration file. This is optional, because you can always add "-t zafta" on all your commands. I like to put it the configuration file because I like shorter command lines. If you don't put it in the configuration file or specify it on the command line, you won't use the template that you're expecting to. 310 | 311 | Edit the file to add the theme, add a title for the site, and specify that all of our content will use the TOML format. 312 | 313 | ``` 314 | $ vi config.toml 315 | theme = "zafta" 316 | baseurl = "" 317 | languageCode = "en-us" 318 | title = "zafta - totally refreshing" 319 | MetaDataFormat = "toml" 320 | :wq 321 | 322 | $ 323 | ``` 324 | 325 | ### Generate the Site 326 | 327 | Now that we have an empty theme, let's generate the site again. 328 | 329 | ``` 330 | $ hugo --verbose 331 | INFO: 2014/09/29 Using config file: /Users/quoha/Sites/zafta/config.toml 332 | INFO: 2014/09/29 syncing from /Users/quoha/Sites/zafta/themes/zafta/static/ to /Users/quoha/Sites/zafta/public/ 333 | INFO: 2014/09/29 syncing from /Users/quoha/Sites/zafta/static/ to /Users/quoha/Sites/zafta/public/ 334 | WARN: 2014/09/29 Unable to locate layout: [404.html theme/404.html] 335 | 0 draft content 336 | 0 future content 337 | 0 pages created 338 | 0 tags created 339 | 0 categories created 340 | in 2 ms 341 | $ 342 | ``` 343 | 344 | Did you notice that the output is different? The warning message for the home page has disappeared and we have an additional information line saying that Hugo is syncing from the theme's directory. 345 | 346 | Let's check the public/ directory to see what Hugo's created. 347 | 348 | ``` 349 | $ ls -l public 350 | total 16 351 | drwxr-xr-x 2 quoha staff 68 Sep 29 17:56 css 352 | -rw-r--r-- 1 quoha staff 0 Sep 29 17:56 index.html 353 | -rw-r--r-- 1 quoha staff 407 Sep 29 17:56 index.xml 354 | drwxr-xr-x 2 quoha staff 68 Sep 29 17:56 js 355 | -rw-r--r-- 1 quoha staff 243 Sep 29 17:56 sitemap.xml 356 | $ 357 | ``` 358 | 359 | Notice four things: 360 | 361 | 1. Hugo created a home page. This is the file public/index.html. 362 | 2. Hugo created a css/ directory. 363 | 3. Hugo created a js/ directory. 364 | 4. Hugo claimed that it created 0 pages. It created a file and copied over static files, but didn't create any pages. That's because it considers a "page" to be a file created directly from a content file. It doesn't count things like the index.html files that it creates automatically. 365 | 366 | #### The Home Page 367 | 368 | Hugo supports many different types of templates. The home page is special because it gets its own type of template and its own template file. The file, layouts/index.html, is used to generate the HTML for the home page. The Hugo documentation says that this is the only required template, but that depends. Hugo's warning message shows that it looks for three different templates: 369 | 370 | ``` 371 | WARN: 2014/09/29 Unable to locate layout: [index.html _default/list.html _default/single.html] 372 | ``` 373 | 374 | If it can't find any of these, it completely skips creating the home page. We noticed that when we built the site without having a theme installed. 375 | 376 | When Hugo created our theme, it created an empty home page template. Now, when we build the site, Hugo finds the template and uses it to generate the HTML for the home page. Since the template file is empty, the HTML file is empty, too. If the template had any rules in it, then Hugo would have used them to generate the home page. 377 | 378 | ``` 379 | $ find . -name index.html | xargs ls -l 380 | -rw-r--r-- 1 quoha staff 0 Sep 29 20:21 ./public/index.html 381 | -rw-r--r-- 1 quoha staff 0 Sep 29 17:31 ./themes/zafta/layouts/index.html 382 | $ 383 | ``` 384 | 385 | #### The Magic of Static 386 | 387 | Hugo does two things when generating the site. It uses templates to transform content into HTML and it copies static files into the site. Unlike content, static files are not transformed. They are copied exactly as they are. 388 | 389 | Hugo assumes that your site will use both CSS and JavaScript, so it creates directories in your theme to hold them. Remember opinions? Well, Hugo's opinion is that you'll store your CSS in a directory named css/ and your JavaScript in a directory named js/. If you don't like that, you can change the directory names in your theme directory or even delete them completely. Hugo's nice enough to offer its opinion, then behave nicely if you disagree. 390 | 391 | ``` 392 | $ find themes/zafta -type d | xargs ls -ld 393 | drwxr-xr-x 7 quoha staff 238 Sep 29 17:38 themes/zafta 394 | drwxr-xr-x 3 quoha staff 102 Sep 29 17:31 themes/zafta/archetypes 395 | drwxr-xr-x 5 quoha staff 170 Sep 29 17:31 themes/zafta/layouts 396 | drwxr-xr-x 4 quoha staff 136 Sep 29 17:31 themes/zafta/layouts/_default 397 | drwxr-xr-x 4 quoha staff 136 Sep 29 17:31 themes/zafta/layouts/partials 398 | drwxr-xr-x 4 quoha staff 136 Sep 29 17:31 themes/zafta/static 399 | drwxr-xr-x 2 quoha staff 68 Sep 29 17:31 themes/zafta/static/css 400 | drwxr-xr-x 2 quoha staff 68 Sep 29 17:31 themes/zafta/static/js 401 | $ 402 | ``` 403 | 404 | ## The Theme Development Cycle 405 | 406 | When you're working on a theme, you will make changes in the theme's directory, rebuild the site, and check your changes in the browser. Hugo makes this very easy: 407 | 408 | 1. Purge the public/ directory. 409 | 2. Run the built in web server in watch mode. 410 | 3. Open your site in a browser. 411 | 4. Update the theme. 412 | 5. Glance at your browser window to see changes. 413 | 6. Return to step 4. 414 | 415 | I’ll throw in one more opinion: never work on a theme on a live site. Always work on a copy of your site. Make changes to your theme, test them, then copy them up to your site. For added safety, use a tool like Git to keep a revision history of your content and your theme. Believe me when I say that it is too easy to lose both your mind and your changes. 416 | 417 | Check the main Hugo site for information on using Git with Hugo. 418 | 419 | ### Purge the public/ Directory 420 | 421 | When generating the site, Hugo will create new files and update existing ones in the ```public/``` directory. It will not delete files that are no longer used. For example, files that were created in the wrong directory or with the wrong title will remain. If you leave them, you might get confused by them later. I recommend cleaning out your site prior to generating it. 422 | 423 | Note: If you're building on an SSD, you should ignore this. Churning on a SSD can be costly. 424 | 425 | ### Hugo's Watch Option 426 | 427 | Hugo's "`--watch`" option will monitor the content/ and your theme directories for changes and rebuild the site automatically. 428 | 429 | ### Live Reload 430 | 431 | Hugo's built in web server supports live reload. As pages are saved on the server, the browser is told to refresh the page. Usually, this happens faster than you can say, "Wow, that's totally amazing." 432 | 433 | ### Development Commands 434 | 435 | Use the following commands as the basis for your workflow. 436 | 437 | ``` 438 | ## purge old files. hugo will recreate the public directory. 439 | ## 440 | $ rm -rf public 441 | ## 442 | ## run hugo in watch mode 443 | ## 444 | $ hugo server --watch --verbose 445 | ``` 446 | 447 | Here's sample output showing Hugo detecting a change to the template for the home page. Once generated, the web browser automatically reloaded the page. I've said this before, it's amazing. 448 | 449 | 450 | ``` 451 | $ rm -rf public 452 | $ hugo server --watch --verbose 453 | INFO: 2014/09/29 Using config file: /Users/quoha/Sites/zafta/config.toml 454 | INFO: 2014/09/29 syncing from /Users/quoha/Sites/zafta/themes/zafta/static/ to /Users/quoha/Sites/zafta/public/ 455 | INFO: 2014/09/29 syncing from /Users/quoha/Sites/zafta/static/ to /Users/quoha/Sites/zafta/public/ 456 | WARN: 2014/09/29 Unable to locate layout: [404.html theme/404.html] 457 | 0 draft content 458 | 0 future content 459 | 0 pages created 460 | 0 tags created 461 | 0 categories created 462 | in 2 ms 463 | Watching for changes in /Users/quoha/Sites/zafta/content 464 | Serving pages from /Users/quoha/Sites/zafta/public 465 | Web Server is available at http://localhost:1313 466 | Press Ctrl+C to stop 467 | INFO: 2014/09/29 File System Event: ["/Users/quoha/Sites/zafta/themes/zafta/layouts/index.html": MODIFY|ATTRIB] 468 | Change detected, rebuilding site 469 | 470 | WARN: 2014/09/29 Unable to locate layout: [404.html theme/404.html] 471 | 0 draft content 472 | 0 future content 473 | 0 pages created 474 | 0 tags created 475 | 0 categories created 476 | in 1 ms 477 | ``` 478 | 479 | ## Update the Home Page Template 480 | 481 | The home page is one of a few special pages that Hugo creates automatically. As mentioned earlier, it looks for one of three files in the theme's layout/ directory: 482 | 483 | 1. index.html 484 | 2. _default/list.html 485 | 3. _default/single.html 486 | 487 | We could update one of the default templates, but a good design decision is to update the most specific template available. That's not a hard and fast rule (in fact, we'll break it a few times in this tutorial), but it is a good generalization. 488 | 489 | ### Make a Static Home Page 490 | 491 | Right now, that page is empty because we don't have any content and we don't have any logic in the template. Let's change that by adding some text to the template. 492 | 493 | ``` 494 | $ vi themes/zafta/layouts/index.html 495 | 496 | 497 | 498 |

hugo says hello!

499 | 500 | 501 | :wq 502 | 503 | $ 504 | ``` 505 | 506 | Build the web site and then verify the results. 507 | 508 | ``` 509 | $ hugo --verbose 510 | INFO: 2014/09/29 Using config file: /Users/quoha/Sites/zafta/config.toml 511 | INFO: 2014/09/29 syncing from /Users/quoha/Sites/zafta/themes/zafta/static/ to /Users/quoha/Sites/zafta/public/ 512 | INFO: 2014/09/29 syncing from /Users/quoha/Sites/zafta/static/ to /Users/quoha/Sites/zafta/public/ 513 | WARN: 2014/09/29 Unable to locate layout: [404.html theme/404.html] 514 | 0 draft content 515 | 0 future content 516 | 0 pages created 517 | 0 tags created 518 | 0 categories created 519 | in 2 ms 520 | 521 | $ find public -type f -name '*.html' | xargs ls -l 522 | -rw-r--r-- 1 quoha staff 78 Sep 29 21:26 public/index.html 523 | 524 | $ cat public/index.html 525 | 526 | 527 | 528 |

hugo says hello!

529 | 530 | ``` 531 | 532 | #### Live Reload 533 | 534 | Note: If you're running the server with the `--watch` option, you'll see different content in the file: 535 | 536 | ``` 537 | $ cat public/index.html 538 | 539 | 540 | 541 |

hugo says hello!

542 | 546 | 547 | ``` 548 | 549 | When you use `--watch`, the Live Reload script is added by Hugo. Look for live reload in the documentation to see what it does and how to disable it. 550 | 551 | ### Build a "Dynamic" Home Page 552 | 553 | "Dynamic home page?" Hugo's a static web site generator, so this seems an odd thing to say. I mean let's have the home page automatically reflect the content in the site every time Hugo builds it. We'll use iteration in the template to do that. 554 | 555 | #### Create New Posts 556 | 557 | Now that we have the home page generating static content, let's add some content to the site. We'll display these posts as a list on the home page and on their own page, too. 558 | 559 | Hugo has a command to generate a skeleton post, just like it does for sites and themes. 560 | 561 | ``` 562 | $ hugo --verbose new post/first.md 563 | INFO: 2014/09/29 Using config file: /Users/quoha/Sites/zafta/config.toml 564 | INFO: 2014/09/29 attempting to create post/first.md of post 565 | INFO: 2014/09/29 curpath: /Users/quoha/Sites/zafta/themes/zafta/archetypes/default.md 566 | ERROR: 2014/09/29 Unable to Cast to map[string]interface{} 567 | 568 | $ 569 | ``` 570 | 571 | That wasn't very nice, was it? 572 | 573 | The "new" command uses an archetype to create the post file. Hugo created an empty default archetype file, but that causes an error when there's a theme. For me, the workaround was to create an archetypes file specifically for the post type. 574 | 575 | ``` 576 | $ vi themes/zafta/archetypes/post.md 577 | +++ 578 | Description = "" 579 | Tags = [] 580 | Categories = [] 581 | +++ 582 | :wq 583 | 584 | $ find themes/zafta/archetypes -type f | xargs ls -l 585 | -rw-r--r-- 1 quoha staff 0 Sep 29 21:53 themes/zafta/archetypes/default.md 586 | -rw-r--r-- 1 quoha staff 51 Sep 29 21:54 themes/zafta/archetypes/post.md 587 | 588 | $ hugo --verbose new post/first.md 589 | INFO: 2014/09/29 Using config file: /Users/quoha/Sites/zafta/config.toml 590 | INFO: 2014/09/29 attempting to create post/first.md of post 591 | INFO: 2014/09/29 curpath: /Users/quoha/Sites/zafta/themes/zafta/archetypes/post.md 592 | INFO: 2014/09/29 creating /Users/quoha/Sites/zafta/content/post/first.md 593 | /Users/quoha/Sites/zafta/content/post/first.md created 594 | 595 | $ hugo --verbose new post/second.md 596 | INFO: 2014/09/29 Using config file: /Users/quoha/Sites/zafta/config.toml 597 | INFO: 2014/09/29 attempting to create post/second.md of post 598 | INFO: 2014/09/29 curpath: /Users/quoha/Sites/zafta/themes/zafta/archetypes/post.md 599 | INFO: 2014/09/29 creating /Users/quoha/Sites/zafta/content/post/second.md 600 | /Users/quoha/Sites/zafta/content/post/second.md created 601 | 602 | $ ls -l content/post 603 | total 16 604 | -rw-r--r-- 1 quoha staff 104 Sep 29 21:54 first.md 605 | -rw-r--r-- 1 quoha staff 105 Sep 29 21:57 second.md 606 | 607 | $ cat content/post/first.md 608 | +++ 609 | Categories = [] 610 | Description = "" 611 | Tags = [] 612 | date = "2014-09-29T21:54:53-05:00" 613 | title = "first" 614 | 615 | +++ 616 | my first post 617 | 618 | $ cat content/post/second.md 619 | +++ 620 | Categories = [] 621 | Description = "" 622 | Tags = [] 623 | date = "2014-09-29T21:57:09-05:00" 624 | title = "second" 625 | 626 | +++ 627 | my second post 628 | 629 | $ 630 | ``` 631 | 632 | Build the web site and then verify the results. 633 | 634 | ``` 635 | $ rm -rf public 636 | $ hugo --verbose 637 | INFO: 2014/09/29 Using config file: /Users/quoha/Sites/zafta/config.toml 638 | INFO: 2014/09/29 syncing from /Users/quoha/Sites/zafta/themes/zafta/static/ to /Users/quoha/Sites/zafta/public/ 639 | INFO: 2014/09/29 syncing from /Users/quoha/Sites/zafta/static/ to /Users/quoha/Sites/zafta/public/ 640 | INFO: 2014/09/29 found taxonomies: map[string]string{"category":"categories", "tag":"tags"} 641 | WARN: 2014/09/29 Unable to locate layout: [404.html theme/404.html] 642 | 0 draft content 643 | 0 future content 644 | 2 pages created 645 | 0 tags created 646 | 0 categories created 647 | in 4 ms 648 | $ 649 | ``` 650 | 651 | The output says that it created 2 pages. Those are our new posts: 652 | 653 | ``` 654 | $ find public -type f -name '*.html' | xargs ls -l 655 | -rw-r--r-- 1 quoha staff 78 Sep 29 22:13 public/index.html 656 | -rw-r--r-- 1 quoha staff 0 Sep 29 22:13 public/post/first/index.html 657 | -rw-r--r-- 1 quoha staff 0 Sep 29 22:13 public/post/index.html 658 | -rw-r--r-- 1 quoha staff 0 Sep 29 22:13 public/post/second/index.html 659 | $ 660 | ``` 661 | 662 | The new files are empty because because the templates used to generate the content are empty. The homepage doesn't show the new content, either. We have to update the templates to add the posts. 663 | 664 | ### List and Single Templates 665 | 666 | In Hugo, we have three major kinds of templates. There's the home page template that we updated previously. It is used only by the home page. We also have "single" templates which are used to generate output for a single content file. We also have "list" templates that are used to group multiple pieces of content before generating output. 667 | 668 | Generally speaking, list templates are named "list.html" and single templates are named "single.html." 669 | 670 | There are three other types of templates: partials, content views, and terms. We will not go into much detail on these. 671 | 672 | ### Add Content to the Homepage 673 | 674 | The home page will contain a list of posts. Let's update its template to add the posts that we just created. The logic in the template will run every time we build the site. 675 | 676 | ``` 677 | $ vi themes/zafta/layouts/index.html 678 | 679 | 680 | 681 | {{ range first 10 .Data.Pages }} 682 |

{{ .Title }}

683 | {{ end }} 684 | 685 | 686 | :wq 687 | 688 | $ 689 | ``` 690 | 691 | Hugo uses the Go template engine. That engine scans the template files for commands which are enclosed between "{{" and "}}". In our template, the commands are: 692 | 693 | 1. range 694 | 2. .Title 695 | 3. end 696 | 697 | The "range" command is an iterator. We're going to use it to go through the first ten pages. Every HTML file that Hugo creates is treated as a page, so looping through the list of pages will look at every file that will be created. 698 | 699 | The ".Title" command prints the value of the "title" variable. Hugo pulls it from the front matter in the Markdown file. 700 | 701 | The "end" command signals the end of the range iterator. The engine loops back to the top of the iteration when it finds "end." Everything between the "range" and "end" is evaluated every time the engine goes through the iteration. In this file, that would cause the title from the first ten pages to be output as heading level one. 702 | 703 | It's helpful to remember that some variables, like .Data, are created before any output files. Hugo loads every content file into the variable and then gives the template a chance to process before creating the HTML files. 704 | 705 | Build the web site and then verify the results. 706 | 707 | ``` 708 | $ rm -rf public 709 | $ hugo --verbose 710 | INFO: 2014/09/29 Using config file: /Users/quoha/Sites/zafta/config.toml 711 | INFO: 2014/09/29 syncing from /Users/quoha/Sites/zafta/themes/zafta/static/ to /Users/quoha/Sites/zafta/public/ 712 | INFO: 2014/09/29 syncing from /Users/quoha/Sites/zafta/static/ to /Users/quoha/Sites/zafta/public/ 713 | INFO: 2014/09/29 found taxonomies: map[string]string{"tag":"tags", "category":"categories"} 714 | WARN: 2014/09/29 Unable to locate layout: [404.html theme/404.html] 715 | 0 draft content 716 | 0 future content 717 | 2 pages created 718 | 0 tags created 719 | 0 categories created 720 | in 4 ms 721 | $ find public -type f -name '*.html' | xargs ls -l 722 | -rw-r--r-- 1 quoha staff 94 Sep 29 22:23 public/index.html 723 | -rw-r--r-- 1 quoha staff 0 Sep 29 22:23 public/post/first/index.html 724 | -rw-r--r-- 1 quoha staff 0 Sep 29 22:23 public/post/index.html 725 | -rw-r--r-- 1 quoha staff 0 Sep 29 22:23 public/post/second/index.html 726 | $ cat public/index.html 727 | 728 | 729 | 730 | 731 |

second

732 | 733 |

first

734 | 735 | 736 | 737 | $ 738 | ``` 739 | 740 | Congratulations, the home page shows the title of the two posts. The posts themselves are still empty, but let's take a moment to appreciate what we've done. Your template now generates output dynamically. Believe it or not, by inserting the range command inside of those curly braces, you've learned everything you need to know to build a theme. All that's really left is understanding which template will be used to generate each content file and becoming familiar with the commands for the template engine. 741 | 742 | And, if that were entirely true, this tutorial would be much shorter. There are a few things to know that will make creating a new template much easier. Don't worry, though, that's all to come. 743 | 744 | ### Add Content to the Posts 745 | 746 | We're working with posts, which are in the content/post/ directory. That means that their section is "post" (and if we don't do something weird, their type is also "post"). 747 | 748 | Hugo uses the section and type to find the template file for every piece of content. Hugo will first look for a template file that matches the section or type name. If it can't find one, then it will look in the _default/ directory. There are some twists that we'll cover when we get to categories and tags, but for now we can assume that Hugo will try post/single.html, then _default/single.html. 749 | 750 | Now that we know the search rule, let's see what we actually have available: 751 | 752 | ``` 753 | $ find themes/zafta -name single.html | xargs ls -l 754 | -rw-r--r-- 1 quoha staff 132 Sep 29 17:31 themes/zafta/layouts/_default/single.html 755 | ``` 756 | 757 | We could create a new template, post/single.html, or change the default. Since we don't know of any other content types, let's start with updating the default. 758 | 759 | Remember, any content that we haven't created a template for will end up using this template. That can be good or bad. Bad because I know that we're going to be adding different types of content and we're going to end up undoing some of the changes we've made. It's good because we'll be able to see immediate results. It's also good to start here because we can start to build the basic layout for the site. As we add more content types, we'll refactor this file and move logic around. Hugo makes that fairly painless, so we'll accept the cost and proceed. 760 | 761 | Please see the Hugo documentation on template rendering for all the details on determining which template to use. And, as the docs mention, if you're building a single page application (SPA) web site, you can delete all of the other templates and work with just the default single page. That's a refreshing amount of joy right there. 762 | 763 | #### Update the Template File 764 | 765 | ``` 766 | $ vi themes/zafta/layouts/_default/single.html 767 | 768 | 769 | 770 | {{ .Title }} 771 | 772 | 773 |

{{ .Title }}

774 | {{ .Content }} 775 | 776 | 777 | :wq 778 | 779 | $ 780 | ``` 781 | 782 | Build the web site and verify the results. 783 | 784 | ``` 785 | $ rm -rf public 786 | $ hugo --verbose 787 | INFO: 2014/09/29 Using config file: /Users/quoha/Sites/zafta/config.toml 788 | INFO: 2014/09/29 syncing from /Users/quoha/Sites/zafta/themes/zafta/static/ to /Users/quoha/Sites/zafta/public/ 789 | INFO: 2014/09/29 syncing from /Users/quoha/Sites/zafta/static/ to /Users/quoha/Sites/zafta/public/ 790 | INFO: 2014/09/29 found taxonomies: map[string]string{"tag":"tags", "category":"categories"} 791 | WARN: 2014/09/29 Unable to locate layout: [404.html theme/404.html] 792 | 0 draft content 793 | 0 future content 794 | 2 pages created 795 | 0 tags created 796 | 0 categories created 797 | in 4 ms 798 | 799 | $ find public -type f -name '*.html' | xargs ls -l 800 | -rw-r--r-- 1 quoha staff 94 Sep 29 22:40 public/index.html 801 | -rw-r--r-- 1 quoha staff 125 Sep 29 22:40 public/post/first/index.html 802 | -rw-r--r-- 1 quoha staff 0 Sep 29 22:40 public/post/index.html 803 | -rw-r--r-- 1 quoha staff 128 Sep 29 22:40 public/post/second/index.html 804 | 805 | $ cat public/post/first/index.html 806 | 807 | 808 | 809 | first 810 | 811 | 812 |

first

813 |

my first post

814 | 815 | 816 | 817 | 818 | $ cat public/post/second/index.html 819 | 820 | 821 | 822 | second 823 | 824 | 825 |

second

826 |

my second post

827 | 828 | 829 | 830 | $ 831 | ``` 832 | 833 | Notice that the posts now have content. You can go to localhost:1313/post/first to verify. 834 | 835 | ### Linking to Content 836 | 837 | The posts are on the home page. Let's add a link from there to the post. Since this is the home page, we'll update its template. 838 | 839 | ``` 840 | $ vi themes/zafta/layouts/index.html 841 | 842 | 843 | 844 | {{ range first 10 .Data.Pages }} 845 |

{{ .Title }}

846 | {{ end }} 847 | 848 | 849 | ``` 850 | 851 | Build the web site and verify the results. 852 | 853 | ``` 854 | $ rm -rf public 855 | $ hugo --verbose 856 | INFO: 2014/09/29 Using config file: /Users/quoha/Sites/zafta/config.toml 857 | INFO: 2014/09/29 syncing from /Users/quoha/Sites/zafta/themes/zafta/static/ to /Users/quoha/Sites/zafta/public/ 858 | INFO: 2014/09/29 syncing from /Users/quoha/Sites/zafta/static/ to /Users/quoha/Sites/zafta/public/ 859 | INFO: 2014/09/29 found taxonomies: map[string]string{"tag":"tags", "category":"categories"} 860 | WARN: 2014/09/29 Unable to locate layout: [404.html theme/404.html] 861 | 0 draft content 862 | 0 future content 863 | 2 pages created 864 | 0 tags created 865 | 0 categories created 866 | in 4 ms 867 | 868 | $ find public -type f -name '*.html' | xargs ls -l 869 | -rw-r--r-- 1 quoha staff 149 Sep 29 22:44 public/index.html 870 | -rw-r--r-- 1 quoha staff 125 Sep 29 22:44 public/post/first/index.html 871 | -rw-r--r-- 1 quoha staff 0 Sep 29 22:44 public/post/index.html 872 | -rw-r--r-- 1 quoha staff 128 Sep 29 22:44 public/post/second/index.html 873 | 874 | $ cat public/index.html 875 | 876 | 877 | 878 | 879 |

second

880 | 881 |

first

882 | 883 | 884 | 885 | 886 | $ 887 | ``` 888 | 889 | ### Create a Post Listing 890 | 891 | We have the posts displaying on the home page and on their own page. We also have a file public/post/index.html that is empty. Let's make it show a list of all posts (not just the first ten). 892 | 893 | We need to decide which template to update. This will be a listing, so it should be a list template. Let's take a quick look and see which list templates are available. 894 | 895 | ``` 896 | $ find themes/zafta -name list.html | xargs ls -l 897 | -rw-r--r-- 1 quoha staff 0 Sep 29 17:31 themes/zafta/layouts/_default/list.html 898 | ``` 899 | 900 | As with the single post, we have to decide to update _default/list.html or create post/list.html. We still don't have multiple content types, so let's stay consistent and update the default list template. 901 | 902 | ## Creating Top Level Pages 903 | 904 | Let's add an "about" page and display it at the top level (as opposed to a sub-level like we did with posts). 905 | 906 | The default in Hugo is to use the directory structure of the content/ directory to guide the location of the generated html in the public/ directory. Let's verify that by creating an "about" page at the top level: 907 | 908 | ``` 909 | $ vi content/about.md 910 | +++ 911 | title = "about" 912 | description = "about this site" 913 | date = "2014-09-27" 914 | slug = "about time" 915 | +++ 916 | 917 | ## about us 918 | 919 | i'm speechless 920 | :wq 921 | ``` 922 | 923 | Generate the web site and verify the results. 924 | 925 | ``` 926 | $ find public -name '*.html' | xargs ls -l 927 | -rw-rw-r-- 1 mdhender staff 334 Sep 27 15:08 public/about-time/index.html 928 | -rw-rw-r-- 1 mdhender staff 527 Sep 27 15:08 public/index.html 929 | -rw-rw-r-- 1 mdhender staff 358 Sep 27 15:08 public/post/first-post/index.html 930 | -rw-rw-r-- 1 mdhender staff 0 Sep 27 15:08 public/post/index.html 931 | -rw-rw-r-- 1 mdhender staff 342 Sep 27 15:08 public/post/second-post/index.html 932 | ``` 933 | 934 | Notice that the page wasn't created at the top level. It was created in a sub-directory named 'about-time/'. That name came from our slug. Hugo will use the slug to name the generated content. It's a reasonable default, by the way, but we can learn a few things by fighting it for this file. 935 | 936 | One other thing. Take a look at the home page. 937 | 938 | ``` 939 | $ cat public/index.html 940 | 941 | 942 | 943 |

creating a new theme

944 |

about

945 |

second

946 |

first

947 | 951 | 952 | ``` 953 | 954 | Notice that the "about" link is listed with the posts? That's not desirable, so let's change that first. 955 | 956 | ``` 957 | $ vi themes/zafta/layouts/index.html 958 | 959 | 960 | 961 |

posts

962 | {{ range first 10 .Data.Pages }} 963 | {{ if eq .Type "post"}} 964 |

{{ .Title }}

965 | {{ end }} 966 | {{ end }} 967 | 968 |

pages

969 | {{ range .Data.Pages }} 970 | {{ if eq .Type "page" }} 971 |

{{ .Title }}

972 | {{ end }} 973 | {{ end }} 974 | 975 | 976 | :wq 977 | ``` 978 | 979 | Generate the web site and verify the results. The home page has two sections, posts and pages, and each section has the right set of headings and links in it. 980 | 981 | But, that about page still renders to about-time/index.html. 982 | 983 | ``` 984 | $ find public -name '*.html' | xargs ls -l 985 | -rw-rw-r-- 1 mdhender staff 334 Sep 27 15:33 public/about-time/index.html 986 | -rw-rw-r-- 1 mdhender staff 645 Sep 27 15:33 public/index.html 987 | -rw-rw-r-- 1 mdhender staff 358 Sep 27 15:33 public/post/first-post/index.html 988 | -rw-rw-r-- 1 mdhender staff 0 Sep 27 15:33 public/post/index.html 989 | -rw-rw-r-- 1 mdhender staff 342 Sep 27 15:33 public/post/second-post/index.html 990 | ``` 991 | 992 | Knowing that hugo is using the slug to generate the file name, the simplest solution is to change the slug. Let's do it the hard way and change the permalink in the configuration file. 993 | 994 | ``` 995 | $ vi config.toml 996 | [permalinks] 997 | page = "/:title/" 998 | about = "/:filename/" 999 | ``` 1000 | 1001 | Generate the web site and verify that this didn't work. Hugo lets "slug" or "URL" override the permalinks setting in the configuration file. Go ahead and comment out the slug in content/about.md, then generate the web site to get it to be created in the right place. 1002 | 1003 | ## Sharing Templates 1004 | 1005 | If you've been following along, you probably noticed that posts have titles in the browser and the home page doesn't. That's because we didn't put the title in the home page's template (layouts/index.html). That's an easy thing to do, but let's look at a different option. 1006 | 1007 | We can put the common bits into a shared template that's stored in the themes/zafta/layouts/partials/ directory. 1008 | 1009 | ### Create the Header and Footer Partials 1010 | 1011 | In Hugo, a partial is a sugar-coated template. Normally a template reference has a path specified. Partials are different. Hugo searches for them along a TODO defined search path. This makes it easier for end-users to override the theme's presentation. 1012 | 1013 | ``` 1014 | $ vi themes/zafta/layouts/partials/header.html 1015 | 1016 | 1017 | 1018 | {{ .Title }} 1019 | 1020 | 1021 | :wq 1022 | 1023 | $ vi themes/zafta/layouts/partials/footer.html 1024 | 1025 | 1026 | :wq 1027 | ``` 1028 | 1029 | ### Update the Home Page Template to Use the Partials 1030 | 1031 | The most noticeable difference between a template call and a partials call is the lack of path: 1032 | 1033 | ``` 1034 | {{ template "theme/partials/header.html" . }} 1035 | ``` 1036 | versus 1037 | ``` 1038 | {{ partial "header.html" . }} 1039 | ``` 1040 | Both pass in the context. 1041 | 1042 | Let's change the home page template to use these new partials. 1043 | 1044 | ``` 1045 | $ vi themes/zafta/layouts/index.html 1046 | {{ partial "header.html" . }} 1047 | 1048 |

posts

1049 | {{ range first 10 .Data.Pages }} 1050 | {{ if eq .Type "post"}} 1051 |

{{ .Title }}

1052 | {{ end }} 1053 | {{ end }} 1054 | 1055 |

pages

1056 | {{ range .Data.Pages }} 1057 | {{ if or (eq .Type "page") (eq .Type "about") }} 1058 |

{{ .Type }} - {{ .Title }} - {{ .RelPermalink }}

1059 | {{ end }} 1060 | {{ end }} 1061 | 1062 | {{ partial "footer.html" . }} 1063 | :wq 1064 | ``` 1065 | 1066 | Generate the web site and verify the results. The title on the home page is now "your title here", which comes from the "title" variable in the config.toml file. 1067 | 1068 | ### Update the Default Single Template to Use the Partials 1069 | 1070 | ``` 1071 | $ vi themes/zafta/layouts/_default/single.html 1072 | {{ partial "header.html" . }} 1073 | 1074 |

{{ .Title }}

1075 | {{ .Content }} 1076 | 1077 | {{ partial "footer.html" . }} 1078 | :wq 1079 | ``` 1080 | 1081 | Generate the web site and verify the results. The title on the posts and the about page should both reflect the value in the markdown file. 1082 | 1083 | ## Add “Date Published” to Posts 1084 | 1085 | It's common to have posts display the date that they were written or published, so let's add that. The front matter of our posts has a variable named "date." It's usually the date the content was created, but let's pretend that's the value we want to display. 1086 | 1087 | ### Add “Date Published” to the Template 1088 | 1089 | We'll start by updating the template used to render the posts. The template code will look like: 1090 | 1091 | ``` 1092 | {{ .Date.Format "Mon, Jan 2, 2006" }} 1093 | ``` 1094 | 1095 | Posts use the default single template, so we'll change that file. 1096 | 1097 | ``` 1098 | $ vi themes/zafta/layouts/_default/single.html 1099 | {{ partial "header.html" . }} 1100 | 1101 |

{{ .Title }}

1102 |

{{ .Date.Format "Mon, Jan 2, 2006" }}

1103 | {{ .Content }} 1104 | 1105 | {{ partial "footer.html" . }} 1106 | :wq 1107 | ``` 1108 | 1109 | Generate the web site and verify the results. The posts now have the date displayed in them. There's a problem, though. The "about" page also has the date displayed. 1110 | 1111 | As usual, there are a couple of ways to make the date display only on posts. We could do an "if" statement like we did on the home page. Another way would be to create a separate template for posts. 1112 | 1113 | The "if" solution works for sites that have just a couple of content types. It aligns with the principle of "code for today," too. 1114 | 1115 | Let's assume, though, that we've made our site so complex that we feel we have to create a new template type. In Hugo-speak, we're going to create a section template. 1116 | 1117 | Let's restore the default single template before we forget. 1118 | 1119 | ``` 1120 | $ mkdir themes/zafta/layouts/post 1121 | $ vi themes/zafta/layouts/_default/single.html 1122 | {{ partial "header.html" . }} 1123 | 1124 |

{{ .Title }}

1125 | {{ .Content }} 1126 | 1127 | {{ partial "footer.html" . }} 1128 | :wq 1129 | ``` 1130 | 1131 | Now we'll update the post's version of the single template. If you remember Hugo's rules, the template engine will use this version over the default. 1132 | 1133 | ``` 1134 | $ vi themes/zafta/layouts/post/single.html 1135 | {{ partial "header.html" . }} 1136 | 1137 |

{{ .Title }}

1138 |

{{ .Date.Format "Mon, Jan 2, 2006" }}

1139 | {{ .Content }} 1140 | 1141 | {{ partial "footer.html" . }} 1142 | :wq 1143 | 1144 | ``` 1145 | 1146 | Note that we removed the date logic from the default template and put it in the post template. Generate the web site and verify the results. Posts have dates and the about page doesn't. 1147 | 1148 | ### Don't Repeat Yourself 1149 | 1150 | DRY is a good design goal and Hugo does a great job supporting it. Part of the art of a good template is knowing when to add a new template and when to update an existing one. While you're figuring that out, accept that you'll be doing some refactoring. Hugo makes that easy and fast, so it's okay to delay splitting up a template. 1151 | -------------------------------------------------------------------------------- /exampleSite/content/post/goisforlovers.md: -------------------------------------------------------------------------------- 1 | +++ 2 | title = "(Hu)go Template Primer" 3 | description = "" 4 | tags = [ 5 | "go", 6 | "golang", 7 | "templates", 8 | "themes", 9 | "development", 10 | ] 11 | date = "2014-04-02" 12 | categories = [ 13 | "Development", 14 | "golang", 15 | ] 16 | menu = "main" 17 | +++ 18 | 19 | Hugo uses the excellent [go][] [html/template][gohtmltemplate] library for 20 | its template engine. It is an extremely lightweight engine that provides a very 21 | small amount of logic. In our experience that it is just the right amount of 22 | logic to be able to create a good static website. If you have used other 23 | template systems from different languages or frameworks you will find a lot of 24 | similarities in go templates. 25 | 26 | This document is a brief primer on using go templates. The [go docs][gohtmltemplate] 27 | provide more details. 28 | 29 | ## Introduction to Go Templates 30 | 31 | Go templates provide an extremely simple template language. It adheres to the 32 | belief that only the most basic of logic belongs in the template or view layer. 33 | One consequence of this simplicity is that go templates parse very quickly. 34 | 35 | A unique characteristic of go templates is they are content aware. Variables and 36 | content will be sanitized depending on the context of where they are used. More 37 | details can be found in the [go docs][gohtmltemplate]. 38 | 39 | ## Basic Syntax 40 | 41 | Go lang templates are html files with the addition of variables and 42 | functions. 43 | 44 | **Go variables and functions are accessible within {{ }}** 45 | 46 | Accessing a predefined variable "foo": 47 | 48 | {{ foo }} 49 | 50 | **Parameters are separated using spaces** 51 | 52 | Calling the add function with input of 1, 2: 53 | 54 | {{ add 1 2 }} 55 | 56 | **Methods and fields are accessed via dot notation** 57 | 58 | Accessing the Page Parameter "bar" 59 | 60 | {{ .Params.bar }} 61 | 62 | **Parentheses can be used to group items together** 63 | 64 | {{ if or (isset .Params "alt") (isset .Params "caption") }} Caption {{ end }} 65 | 66 | 67 | ## Variables 68 | 69 | Each go template has a struct (object) made available to it. In hugo each 70 | template is passed either a page or a node struct depending on which type of 71 | page you are rendering. More details are available on the 72 | [variables](/layout/variables) page. 73 | 74 | A variable is accessed by referencing the variable name. 75 | 76 | {{ .Title }} 77 | 78 | Variables can also be defined and referenced. 79 | 80 | {{ $address := "123 Main St."}} 81 | {{ $address }} 82 | 83 | 84 | ## Functions 85 | 86 | Go template ship with a few functions which provide basic functionality. The go 87 | template system also provides a mechanism for applications to extend the 88 | available functions with their own. [Hugo template 89 | functions](/layout/functions) provide some additional functionality we believe 90 | are useful for building websites. Functions are called by using their name 91 | followed by the required parameters separated by spaces. Template 92 | functions cannot be added without recompiling hugo. 93 | 94 | **Example:** 95 | 96 | {{ add 1 2 }} 97 | 98 | ## Includes 99 | 100 | When including another template you will pass to it the data it will be 101 | able to access. To pass along the current context please remember to 102 | include a trailing dot. The templates location will always be starting at 103 | the /layout/ directory within Hugo. 104 | 105 | **Example:** 106 | 107 | {{ template "chrome/header.html" . }} 108 | 109 | 110 | ## Logic 111 | 112 | Go templates provide the most basic iteration and conditional logic. 113 | 114 | ### Iteration 115 | 116 | Just like in go, the go templates make heavy use of range to iterate over 117 | a map, array or slice. The following are different examples of how to use 118 | range. 119 | 120 | **Example 1: Using Context** 121 | 122 | {{ range array }} 123 | {{ . }} 124 | {{ end }} 125 | 126 | **Example 2: Declaring value variable name** 127 | 128 | {{range $element := array}} 129 | {{ $element }} 130 | {{ end }} 131 | 132 | **Example 2: Declaring key and value variable name** 133 | 134 | {{range $index, $element := array}} 135 | {{ $index }} 136 | {{ $element }} 137 | {{ end }} 138 | 139 | ### Conditionals 140 | 141 | If, else, with, or, & and provide the framework for handling conditional 142 | logic in Go Templates. Like range, each statement is closed with `end`. 143 | 144 | 145 | Go Templates treat the following values as false: 146 | 147 | * false 148 | * 0 149 | * any array, slice, map, or string of length zero 150 | 151 | **Example 1: If** 152 | 153 | {{ if isset .Params "title" }}

{{ index .Params "title" }}

{{ end }} 154 | 155 | **Example 2: If -> Else** 156 | 157 | {{ if isset .Params "alt" }} 158 | {{ index .Params "alt" }} 159 | {{else}} 160 | {{ index .Params "caption" }} 161 | {{ end }} 162 | 163 | **Example 3: And & Or** 164 | 165 | {{ if and (or (isset .Params "title") (isset .Params "caption")) (isset .Params "attr")}} 166 | 167 | **Example 4: With** 168 | 169 | An alternative way of writing "if" and then referencing the same value 170 | is to use "with" instead. With rebinds the context `.` within its scope, 171 | and skips the block if the variable is absent. 172 | 173 | The first example above could be simplified as: 174 | 175 | {{ with .Params.title }}

{{ . }}

{{ end }} 176 | 177 | **Example 5: If -> Else If** 178 | 179 | {{ if isset .Params "alt" }} 180 | {{ index .Params "alt" }} 181 | {{ else if isset .Params "caption" }} 182 | {{ index .Params "caption" }} 183 | {{ end }} 184 | 185 | ## Pipes 186 | 187 | One of the most powerful components of go templates is the ability to 188 | stack actions one after another. This is done by using pipes. Borrowed 189 | from unix pipes, the concept is simple, each pipeline's output becomes the 190 | input of the following pipe. 191 | 192 | Because of the very simple syntax of go templates, the pipe is essential 193 | to being able to chain together function calls. One limitation of the 194 | pipes is that they only can work with a single value and that value 195 | becomes the last parameter of the next pipeline. 196 | 197 | A few simple examples should help convey how to use the pipe. 198 | 199 | **Example 1 :** 200 | 201 | {{ if eq 1 1 }} Same {{ end }} 202 | 203 | is the same as 204 | 205 | {{ eq 1 1 | if }} Same {{ end }} 206 | 207 | It does look odd to place the if at the end, but it does provide a good 208 | illustration of how to use the pipes. 209 | 210 | **Example 2 :** 211 | 212 | {{ index .Params "disqus_url" | html }} 213 | 214 | Access the page parameter called "disqus_url" and escape the HTML. 215 | 216 | **Example 3 :** 217 | 218 | {{ if or (or (isset .Params "title") (isset .Params "caption")) (isset .Params "attr")}} 219 | Stuff Here 220 | {{ end }} 221 | 222 | Could be rewritten as 223 | 224 | {{ isset .Params "caption" | or isset .Params "title" | or isset .Params "attr" | if }} 225 | Stuff Here 226 | {{ end }} 227 | 228 | 229 | ## Context (aka. the dot) 230 | 231 | The most easily overlooked concept to understand about go templates is that {{ . }} 232 | always refers to the current context. In the top level of your template this 233 | will be the data set made available to it. Inside of a iteration it will have 234 | the value of the current item. When inside of a loop the context has changed. . 235 | will no longer refer to the data available to the entire page. If you need to 236 | access this from within the loop you will likely want to set it to a variable 237 | instead of depending on the context. 238 | 239 | **Example:** 240 | 241 | {{ $title := .Site.Title }} 242 | {{ range .Params.tags }} 243 |
  • {{ . }} - {{ $title }}
  • 244 | {{ end }} 245 | 246 | Notice how once we have entered the loop the value of {{ . }} has changed. We 247 | have defined a variable outside of the loop so we have access to it from within 248 | the loop. 249 | 250 | # Hugo Parameters 251 | 252 | Hugo provides the option of passing values to the template language 253 | through the site configuration (for sitewide values), or through the meta 254 | data of each specific piece of content. You can define any values of any 255 | type (supported by your front matter/config format) and use them however 256 | you want to inside of your templates. 257 | 258 | 259 | ## Using Content (page) Parameters 260 | 261 | In each piece of content you can provide variables to be used by the 262 | templates. This happens in the [front matter](/content/front-matter). 263 | 264 | An example of this is used in this documentation site. Most of the pages 265 | benefit from having the table of contents provided. Sometimes the TOC just 266 | doesn't make a lot of sense. We've defined a variable in our front matter 267 | of some pages to turn off the TOC from being displayed. 268 | 269 | Here is the example front matter: 270 | 271 | ``` 272 | --- 273 | title: "Permalinks" 274 | date: "2013-11-18" 275 | aliases: 276 | - "/doc/permalinks/" 277 | groups: ["extras"] 278 | groups_weight: 30 279 | notoc: true 280 | --- 281 | ``` 282 | 283 | Here is the corresponding code inside of the template: 284 | 285 | {{ if not .Params.notoc }} 286 |
    287 | {{ .TableOfContents }} 288 |
    289 | {{ end }} 290 | 291 | 292 | 293 | ## Using Site (config) Parameters 294 | In your top-level configuration file (eg, `config.yaml`) you can define site 295 | parameters, which are values which will be available to you in chrome. 296 | 297 | For instance, you might declare: 298 | 299 | ```yaml 300 | params: 301 | CopyrightHTML: "Copyright © 2013 John Doe. All Rights Reserved." 302 | TwitterUser: "spf13" 303 | SidebarRecentLimit: 5 304 | ``` 305 | 306 | Within a footer layout, you might then declare a `