Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

creating-a-new-theme.md 46KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  1. ---
  2. title: "Creating a New Theme"
  3. date: 2014-09-28
  4. tags: ["go", "golang", "hugo", "themes"]
  5. draft: false
  6. ---
  7. ## Introduction
  8. 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.
  9. 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.
  10. 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.
  11. Here's an example:
  12. ```
  13. ## this is a comment
  14. $ echo this is a command
  15. this is a command
  16. ## edit the file
  17. $vi foo.md
  18. +++
  19. date = "2014-09-28"
  20. title = "creating a new theme"
  21. +++
  22. bah and humbug
  23. :wq
  24. ## show it
  25. $ cat foo.md
  26. +++
  27. date = "2014-09-28"
  28. title = "creating a new theme"
  29. +++
  30. bah and humbug
  31. $
  32. ```
  33. ## Some Definitions
  34. There are a few concepts that you need to understand before creating a theme.
  35. ### Skins
  36. 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.
  37. 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.
  38. 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?
  39. 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.
  40. The rest of this tutorial will call a skin created in the ```themes/``` directory a theme.
  41. 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.
  42. ### The Home Page
  43. 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.
  44. ### Site Configuration File
  45. 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.
  46. 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.
  47. ### Content
  48. 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.
  49. #### Front Matter
  50. 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.
  51. The information in the front matter is passed into the template before the content is rendered into HTML.
  52. #### Markdown
  53. 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.
  54. ### Template Files
  55. 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.
  56. 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.
  57. 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.
  58. Please note that you can use the front matter to influence Hugo’s choice of templates.
  59. #### Single Template
  60. 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.
  61. #### List Template
  62. 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.
  63. 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.
  64. #### Partial Template
  65. 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.
  66. ## Create a New Site
  67. 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.
  68. The "new site" command will create a skeleton of a site. It will give you the basic directory structure and a useable configuration file.
  69. ```
  70. $ hugo new site ~/Sites/zafta
  71. $ cd ~/Sites/zafta
  72. $ ls -l
  73. total 8
  74. drwxr-xr-x 7 quoha staff 238 Sep 29 16:49 .
  75. drwxr-xr-x 3 quoha staff 102 Sep 29 16:49 ..
  76. drwxr-xr-x 2 quoha staff 68 Sep 29 16:49 archetypes
  77. -rw-r--r-- 1 quoha staff 82 Sep 29 16:49 config.toml
  78. drwxr-xr-x 2 quoha staff 68 Sep 29 16:49 content
  79. drwxr-xr-x 2 quoha staff 68 Sep 29 16:49 layouts
  80. drwxr-xr-x 2 quoha staff 68 Sep 29 16:49 static
  81. $
  82. ```
  83. Take a look in the content/ directory to confirm that it is empty.
  84. 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.
  85. ### Generate the HTML For the New Site
  86. 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.
  87. ```
  88. $ hugo --verbose
  89. INFO: 2014/09/29 Using config file: config.toml
  90. INFO: 2014/09/29 syncing from /Users/quoha/Sites/zafta/static/ to /Users/quoha/Sites/zafta/public/
  91. WARN: 2014/09/29 Unable to locate layout: [index.html _default/list.html _default/single.html]
  92. WARN: 2014/09/29 Unable to locate layout: [404.html]
  93. 0 draft content
  94. 0 future content
  95. 0 pages created
  96. 0 tags created
  97. 0 categories created
  98. in 2 ms
  99. $
  100. ```
  101. 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.
  102. We can verify that the command worked by looking at the directory again.
  103. ```
  104. $ ls -l
  105. total 8
  106. drwxr-xr-x 2 quoha staff 68 Sep 29 16:49 archetypes
  107. -rw-r--r-- 1 quoha staff 82 Sep 29 16:49 config.toml
  108. drwxr-xr-x 2 quoha staff 68 Sep 29 16:49 content
  109. drwxr-xr-x 2 quoha staff 68 Sep 29 16:49 layouts
  110. drwxr-xr-x 4 quoha staff 136 Sep 29 17:02 public
  111. drwxr-xr-x 2 quoha staff 68 Sep 29 16:49 static
  112. $
  113. ```
  114. 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.
  115. ```
  116. $ ls -l public
  117. total 16
  118. -rw-r--r-- 1 quoha staff 416 Sep 29 17:02 index.xml
  119. -rw-r--r-- 1 quoha staff 262 Sep 29 17:02 sitemap.xml
  120. $
  121. ```
  122. Hugo created two XML files, which is standard, but there are no HTML files.
  123. ### Test the New Site
  124. 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:
  125. ```
  126. $ hugo server --verbose
  127. INFO: 2014/09/29 Using config file: /Users/quoha/Sites/zafta/config.toml
  128. INFO: 2014/09/29 syncing from /Users/quoha/Sites/zafta/static/ to /Users/quoha/Sites/zafta/public/
  129. WARN: 2014/09/29 Unable to locate layout: [index.html _default/list.html _default/single.html]
  130. WARN: 2014/09/29 Unable to locate layout: [404.html]
  131. 0 draft content
  132. 0 future content
  133. 0 pages created
  134. 0 tags created
  135. 0 categories created
  136. in 2 ms
  137. Serving pages from /Users/quoha/Sites/zafta/public
  138. Web Server is available at http://localhost:1313
  139. Press Ctrl+C to stop
  140. ```
  141. 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:
  142. ```
  143. index.xml
  144. sitemap.xml
  145. ```
  146. 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.
  147. Let’s go back and look at those warnings again.
  148. ```
  149. WARN: 2014/09/29 Unable to locate layout: [index.html _default/list.html _default/single.html]
  150. WARN: 2014/09/29 Unable to locate layout: [404.html]
  151. ```
  152. 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.
  153. 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.
  154. 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.
  155. 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.
  156. ## Create a New Theme
  157. 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.
  158. 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.
  159. 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.
  160. ### Create a Skeleton
  161. 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.
  162. ```
  163. $ hugo new theme zafta
  164. $ ls -l
  165. total 8
  166. drwxr-xr-x 2 quoha staff 68 Sep 29 16:49 archetypes
  167. -rw-r--r-- 1 quoha staff 82 Sep 29 16:49 config.toml
  168. drwxr-xr-x 2 quoha staff 68 Sep 29 16:49 content
  169. drwxr-xr-x 2 quoha staff 68 Sep 29 16:49 layouts
  170. drwxr-xr-x 4 quoha staff 136 Sep 29 17:02 public
  171. drwxr-xr-x 2 quoha staff 68 Sep 29 16:49 static
  172. drwxr-xr-x 3 quoha staff 102 Sep 29 17:31 themes
  173. $ find themes -type f | xargs ls -l
  174. -rw-r--r-- 1 quoha staff 1081 Sep 29 17:31 themes/zafta/LICENSE.md
  175. -rw-r--r-- 1 quoha staff 0 Sep 29 17:31 themes/zafta/archetypes/default.md
  176. -rw-r--r-- 1 quoha staff 0 Sep 29 17:31 themes/zafta/layouts/_default/list.html
  177. -rw-r--r-- 1 quoha staff 0 Sep 29 17:31 themes/zafta/layouts/_default/single.html
  178. -rw-r--r-- 1 quoha staff 0 Sep 29 17:31 themes/zafta/layouts/index.html
  179. -rw-r--r-- 1 quoha staff 0 Sep 29 17:31 themes/zafta/layouts/partials/footer.html
  180. -rw-r--r-- 1 quoha staff 0 Sep 29 17:31 themes/zafta/layouts/partials/header.html
  181. -rw-r--r-- 1 quoha staff 93 Sep 29 17:31 themes/zafta/theme.toml
  182. $
  183. ```
  184. The skeleton includes templates (the files ending in .html), license file, a description of your theme (the theme.toml file), and an empty archetype.
  185. 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.
  186. ```
  187. $ vi themes/zafta/theme.toml
  188. author = "michael d henderson"
  189. description = "a minimal working template"
  190. license = "MIT"
  191. name = "zafta"
  192. source_repo = ""
  193. tags = ["tags", "categories"]
  194. :wq
  195. ## also edit themes/zafta/LICENSE.md and change
  196. ## the bit that says "YOUR_NAME_HERE"
  197. ```
  198. Note that the the skeleton's template files are empty. Don't worry, we'll be changing that shortly.
  199. ```
  200. $ find themes/zafta -name '*.html' | xargs ls -l
  201. -rw-r--r-- 1 quoha staff 0 Sep 29 17:31 themes/zafta/layouts/_default/list.html
  202. -rw-r--r-- 1 quoha staff 0 Sep 29 17:31 themes/zafta/layouts/_default/single.html
  203. -rw-r--r-- 1 quoha staff 0 Sep 29 17:31 themes/zafta/layouts/index.html
  204. -rw-r--r-- 1 quoha staff 0 Sep 29 17:31 themes/zafta/layouts/partials/footer.html
  205. -rw-r--r-- 1 quoha staff 0 Sep 29 17:31 themes/zafta/layouts/partials/header.html
  206. $
  207. ```
  208. ### Update the Configuration File to Use the Theme
  209. 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.
  210. 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.
  211. ```
  212. $ vi config.toml
  213. theme = "zafta"
  214. baseurl = ""
  215. languageCode = "en-us"
  216. title = "zafta - totally refreshing"
  217. MetaDataFormat = "toml"
  218. :wq
  219. $
  220. ```
  221. ### Generate the Site
  222. Now that we have an empty theme, let's generate the site again.
  223. ```
  224. $ hugo --verbose
  225. INFO: 2014/09/29 Using config file: /Users/quoha/Sites/zafta/config.toml
  226. INFO: 2014/09/29 syncing from /Users/quoha/Sites/zafta/themes/zafta/static/ to /Users/quoha/Sites/zafta/public/
  227. INFO: 2014/09/29 syncing from /Users/quoha/Sites/zafta/static/ to /Users/quoha/Sites/zafta/public/
  228. WARN: 2014/09/29 Unable to locate layout: [404.html theme/404.html]
  229. 0 draft content
  230. 0 future content
  231. 0 pages created
  232. 0 tags created
  233. 0 categories created
  234. in 2 ms
  235. $
  236. ```
  237. 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.
  238. Let's check the public/ directory to see what Hugo's created.
  239. ```
  240. $ ls -l public
  241. total 16
  242. drwxr-xr-x 2 quoha staff 68 Sep 29 17:56 css
  243. -rw-r--r-- 1 quoha staff 0 Sep 29 17:56 index.html
  244. -rw-r--r-- 1 quoha staff 407 Sep 29 17:56 index.xml
  245. drwxr-xr-x 2 quoha staff 68 Sep 29 17:56 js
  246. -rw-r--r-- 1 quoha staff 243 Sep 29 17:56 sitemap.xml
  247. $
  248. ```
  249. Notice four things:
  250. 1. Hugo created a home page. This is the file public/index.html.
  251. 2. Hugo created a css/ directory.
  252. 3. Hugo created a js/ directory.
  253. 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.
  254. #### The Home Page
  255. 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:
  256. ```
  257. WARN: 2014/09/29 Unable to locate layout: [index.html _default/list.html _default/single.html]
  258. ```
  259. 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.
  260. 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.
  261. ```
  262. $ find . -name index.html | xargs ls -l
  263. -rw-r--r-- 1 quoha staff 0 Sep 29 20:21 ./public/index.html
  264. -rw-r--r-- 1 quoha staff 0 Sep 29 17:31 ./themes/zafta/layouts/index.html
  265. $
  266. ```
  267. #### The Magic of Static
  268. 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.
  269. 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.
  270. ```
  271. $ find themes/zafta -type d | xargs ls -ld
  272. drwxr-xr-x 7 quoha staff 238 Sep 29 17:38 themes/zafta
  273. drwxr-xr-x 3 quoha staff 102 Sep 29 17:31 themes/zafta/archetypes
  274. drwxr-xr-x 5 quoha staff 170 Sep 29 17:31 themes/zafta/layouts
  275. drwxr-xr-x 4 quoha staff 136 Sep 29 17:31 themes/zafta/layouts/_default
  276. drwxr-xr-x 4 quoha staff 136 Sep 29 17:31 themes/zafta/layouts/partials
  277. drwxr-xr-x 4 quoha staff 136 Sep 29 17:31 themes/zafta/static
  278. drwxr-xr-x 2 quoha staff 68 Sep 29 17:31 themes/zafta/static/css
  279. drwxr-xr-x 2 quoha staff 68 Sep 29 17:31 themes/zafta/static/js
  280. $
  281. ```
  282. ## The Theme Development Cycle
  283. 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:
  284. 1. Purge the public/ directory.
  285. 2. Run the built in web server in watch mode.
  286. 3. Open your site in a browser.
  287. 4. Update the theme.
  288. 5. Glance at your browser window to see changes.
  289. 6. Return to step 4.
  290. 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.
  291. Check the main Hugo site for information on using Git with Hugo.
  292. ### Purge the public/ Directory
  293. 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.
  294. Note: If you're building on an SSD, you should ignore this. Churning on a SSD can be costly.
  295. ### Hugo's Watch Option
  296. Hugo's "`--watch`" option will monitor the content/ and your theme directories for changes and rebuild the site automatically.
  297. ### Live Reload
  298. 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."
  299. ### Development Commands
  300. Use the following commands as the basis for your workflow.
  301. ```
  302. ## purge old files. hugo will recreate the public directory.
  303. ##
  304. $ rm -rf public
  305. ##
  306. ## run hugo in watch mode
  307. ##
  308. $ hugo server --watch --verbose
  309. ```
  310. 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.
  311. ```
  312. $ rm -rf public
  313. $ hugo server --watch --verbose
  314. INFO: 2014/09/29 Using config file: /Users/quoha/Sites/zafta/config.toml
  315. INFO: 2014/09/29 syncing from /Users/quoha/Sites/zafta/themes/zafta/static/ to /Users/quoha/Sites/zafta/public/
  316. INFO: 2014/09/29 syncing from /Users/quoha/Sites/zafta/static/ to /Users/quoha/Sites/zafta/public/
  317. WARN: 2014/09/29 Unable to locate layout: [404.html theme/404.html]
  318. 0 draft content
  319. 0 future content
  320. 0 pages created
  321. 0 tags created
  322. 0 categories created
  323. in 2 ms
  324. Watching for changes in /Users/quoha/Sites/zafta/content
  325. Serving pages from /Users/quoha/Sites/zafta/public
  326. Web Server is available at http://localhost:1313
  327. Press Ctrl+C to stop
  328. INFO: 2014/09/29 File System Event: ["/Users/quoha/Sites/zafta/themes/zafta/layouts/index.html": MODIFY|ATTRIB]
  329. Change detected, rebuilding site
  330. WARN: 2014/09/29 Unable to locate layout: [404.html theme/404.html]
  331. 0 draft content
  332. 0 future content
  333. 0 pages created
  334. 0 tags created
  335. 0 categories created
  336. in 1 ms
  337. ```
  338. ## Update the Home Page Template
  339. 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:
  340. 1. index.html
  341. 2. _default/list.html
  342. 3. _default/single.html
  343. 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.
  344. ### Make a Static Home Page
  345. 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.
  346. ```
  347. $ vi themes/zafta/layouts/index.html
  348. <!DOCTYPE html>
  349. <html>
  350. <body>
  351. <p>hugo says hello!</p>
  352. </body>
  353. </html>
  354. :wq
  355. $
  356. ```
  357. Build the web site and then verify the results.
  358. ```
  359. $ hugo --verbose
  360. INFO: 2014/09/29 Using config file: /Users/quoha/Sites/zafta/config.toml
  361. INFO: 2014/09/29 syncing from /Users/quoha/Sites/zafta/themes/zafta/static/ to /Users/quoha/Sites/zafta/public/
  362. INFO: 2014/09/29 syncing from /Users/quoha/Sites/zafta/static/ to /Users/quoha/Sites/zafta/public/
  363. WARN: 2014/09/29 Unable to locate layout: [404.html theme/404.html]
  364. 0 draft content
  365. 0 future content
  366. 0 pages created
  367. 0 tags created
  368. 0 categories created
  369. in 2 ms
  370. $ find public -type f -name '*.html' | xargs ls -l
  371. -rw-r--r-- 1 quoha staff 78 Sep 29 21:26 public/index.html
  372. $ cat public/index.html
  373. <!DOCTYPE html>
  374. <html>
  375. <body>
  376. <p>hugo says hello!</p>
  377. </html>
  378. ```
  379. #### Live Reload
  380. Note: If you're running the server with the `--watch` option, you'll see different content in the file:
  381. ```
  382. $ cat public/index.html
  383. <!DOCTYPE html>
  384. <html>
  385. <body>
  386. <p>hugo says hello!</p>
  387. <script>document.write('<script src="http://'
  388. + (location.host || 'localhost').split(':')[0]
  389. + ':1313/livereload.js?mindelay=10"></'
  390. + 'script>')</script></body>
  391. </html>
  392. ```
  393. 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.
  394. ### Build a "Dynamic" Home Page
  395. "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.
  396. #### Create New Posts
  397. 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.
  398. Hugo has a command to generate a skeleton post, just like it does for sites and themes.
  399. ```
  400. $ hugo --verbose new post/first.md
  401. INFO: 2014/09/29 Using config file: /Users/quoha/Sites/zafta/config.toml
  402. INFO: 2014/09/29 attempting to create post/first.md of post
  403. INFO: 2014/09/29 curpath: /Users/quoha/Sites/zafta/themes/zafta/archetypes/default.md
  404. ERROR: 2014/09/29 Unable to Cast <nil> to map[string]interface{}
  405. $
  406. ```
  407. That wasn't very nice, was it?
  408. 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.
  409. ```
  410. $ vi themes/zafta/archetypes/post.md
  411. +++
  412. Description = ""
  413. Tags = []
  414. Categories = []
  415. +++
  416. :wq
  417. $ find themes/zafta/archetypes -type f | xargs ls -l
  418. -rw-r--r-- 1 quoha staff 0 Sep 29 21:53 themes/zafta/archetypes/default.md
  419. -rw-r--r-- 1 quoha staff 51 Sep 29 21:54 themes/zafta/archetypes/post.md
  420. $ hugo --verbose new post/first.md
  421. INFO: 2014/09/29 Using config file: /Users/quoha/Sites/zafta/config.toml
  422. INFO: 2014/09/29 attempting to create post/first.md of post
  423. INFO: 2014/09/29 curpath: /Users/quoha/Sites/zafta/themes/zafta/archetypes/post.md
  424. INFO: 2014/09/29 creating /Users/quoha/Sites/zafta/content/post/first.md
  425. /Users/quoha/Sites/zafta/content/post/first.md created
  426. $ hugo --verbose new post/second.md
  427. INFO: 2014/09/29 Using config file: /Users/quoha/Sites/zafta/config.toml
  428. INFO: 2014/09/29 attempting to create post/second.md of post
  429. INFO: 2014/09/29 curpath: /Users/quoha/Sites/zafta/themes/zafta/archetypes/post.md
  430. INFO: 2014/09/29 creating /Users/quoha/Sites/zafta/content/post/second.md
  431. /Users/quoha/Sites/zafta/content/post/second.md created
  432. $ ls -l content/post
  433. total 16
  434. -rw-r--r-- 1 quoha staff 104 Sep 29 21:54 first.md
  435. -rw-r--r-- 1 quoha staff 105 Sep 29 21:57 second.md
  436. $ cat content/post/first.md
  437. +++
  438. Categories = []
  439. Description = ""
  440. Tags = []
  441. date = "2014-09-29T21:54:53-05:00"
  442. title = "first"
  443. +++
  444. my first post
  445. $ cat content/post/second.md
  446. +++
  447. Categories = []
  448. Description = ""
  449. Tags = []
  450. date = "2014-09-29T21:57:09-05:00"
  451. title = "second"
  452. +++
  453. my second post
  454. $
  455. ```
  456. Build the web site and then verify the results.
  457. ```
  458. $ rm -rf public
  459. $ hugo --verbose
  460. INFO: 2014/09/29 Using config file: /Users/quoha/Sites/zafta/config.toml
  461. INFO: 2014/09/29 syncing from /Users/quoha/Sites/zafta/themes/zafta/static/ to /Users/quoha/Sites/zafta/public/
  462. INFO: 2014/09/29 syncing from /Users/quoha/Sites/zafta/static/ to /Users/quoha/Sites/zafta/public/
  463. INFO: 2014/09/29 found taxonomies: map[string]string{"category":"categories", "tag":"tags"}
  464. WARN: 2014/09/29 Unable to locate layout: [404.html theme/404.html]
  465. 0 draft content
  466. 0 future content
  467. 2 pages created
  468. 0 tags created
  469. 0 categories created
  470. in 4 ms
  471. $
  472. ```
  473. The output says that it created 2 pages. Those are our new posts:
  474. ```
  475. $ find public -type f -name '*.html' | xargs ls -l
  476. -rw-r--r-- 1 quoha staff 78 Sep 29 22:13 public/index.html
  477. -rw-r--r-- 1 quoha staff 0 Sep 29 22:13 public/post/first/index.html
  478. -rw-r--r-- 1 quoha staff 0 Sep 29 22:13 public/post/index.html
  479. -rw-r--r-- 1 quoha staff 0 Sep 29 22:13 public/post/second/index.html
  480. $
  481. ```
  482. 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.
  483. ### List and Single Templates
  484. 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.
  485. Generally speaking, list templates are named "list.html" and single templates are named "single.html."
  486. There are three other types of templates: partials, content views, and terms. We will not go into much detail on these.
  487. ### Add Content to the Homepage
  488. 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.
  489. ```
  490. $ vi themes/zafta/layouts/index.html
  491. <!DOCTYPE html>
  492. <html>
  493. <body>
  494. {{ range first 10 .Data.Pages }}
  495. <h1>{{ .Title }}</h1>
  496. {{ end }}
  497. </body>
  498. </html>
  499. :wq
  500. $
  501. ```
  502. 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:
  503. 1. range
  504. 2. .Title
  505. 3. end
  506. 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.
  507. The ".Title" command prints the value of the "title" variable. Hugo pulls it from the front matter in the Markdown file.
  508. 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.
  509. 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.
  510. Build the web site and then verify the results.
  511. ```
  512. $ rm -rf public
  513. $ hugo --verbose
  514. INFO: 2014/09/29 Using config file: /Users/quoha/Sites/zafta/config.toml
  515. INFO: 2014/09/29 syncing from /Users/quoha/Sites/zafta/themes/zafta/static/ to /Users/quoha/Sites/zafta/public/
  516. INFO: 2014/09/29 syncing from /Users/quoha/Sites/zafta/static/ to /Users/quoha/Sites/zafta/public/
  517. INFO: 2014/09/29 found taxonomies: map[string]string{"tag":"tags", "category":"categories"}
  518. WARN: 2014/09/29 Unable to locate layout: [404.html theme/404.html]
  519. 0 draft content
  520. 0 future content
  521. 2 pages created
  522. 0 tags created
  523. 0 categories created
  524. in 4 ms
  525. $ find public -type f -name '*.html' | xargs ls -l
  526. -rw-r--r-- 1 quoha staff 94 Sep 29 22:23 public/index.html
  527. -rw-r--r-- 1 quoha staff 0 Sep 29 22:23 public/post/first/index.html
  528. -rw-r--r-- 1 quoha staff 0 Sep 29 22:23 public/post/index.html
  529. -rw-r--r-- 1 quoha staff 0 Sep 29 22:23 public/post/second/index.html
  530. $ cat public/index.html
  531. <!DOCTYPE html>
  532. <html>
  533. <body>
  534. <h1>second</h1>
  535. <h1>first</h1>
  536. </body>
  537. </html>
  538. $
  539. ```
  540. 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.
  541. 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.
  542. ### Add Content to the Posts
  543. 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").
  544. 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.
  545. Now that we know the search rule, let's see what we actually have available:
  546. ```
  547. $ find themes/zafta -name single.html | xargs ls -l
  548. -rw-r--r-- 1 quoha staff 132 Sep 29 17:31 themes/zafta/layouts/_default/single.html
  549. ```
  550. 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.
  551. 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.
  552. 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.
  553. #### Update the Template File
  554. ```
  555. $ vi themes/zafta/layouts/_default/single.html
  556. <!DOCTYPE html>
  557. <html>
  558. <head>
  559. <title>{{ .Title }}</title>
  560. </head>
  561. <body>
  562. <h1>{{ .Title }}</h1>
  563. {{ .Content }}
  564. </body>
  565. </html>
  566. :wq
  567. $
  568. ```
  569. Build the web site and verify the results.
  570. ```
  571. $ rm -rf public
  572. $ hugo --verbose
  573. INFO: 2014/09/29 Using config file: /Users/quoha/Sites/zafta/config.toml
  574. INFO: 2014/09/29 syncing from /Users/quoha/Sites/zafta/themes/zafta/static/ to /Users/quoha/Sites/zafta/public/
  575. INFO: 2014/09/29 syncing from /Users/quoha/Sites/zafta/static/ to /Users/quoha/Sites/zafta/public/
  576. INFO: 2014/09/29 found taxonomies: map[string]string{"tag":"tags", "category":"categories"}
  577. WARN: 2014/09/29 Unable to locate layout: [404.html theme/404.html]
  578. 0 draft content
  579. 0 future content
  580. 2 pages created
  581. 0 tags created
  582. 0 categories created
  583. in 4 ms
  584. $ find public -type f -name '*.html' | xargs ls -l
  585. -rw-r--r-- 1 quoha staff 94 Sep 29 22:40 public/index.html
  586. -rw-r--r-- 1 quoha staff 125 Sep 29 22:40 public/post/first/index.html
  587. -rw-r--r-- 1 quoha staff 0 Sep 29 22:40 public/post/index.html
  588. -rw-r--r-- 1 quoha staff 128 Sep 29 22:40 public/post/second/index.html
  589. $ cat public/post/first/index.html
  590. <!DOCTYPE html>
  591. <html>
  592. <head>
  593. <title>first</title>
  594. </head>
  595. <body>
  596. <h1>first</h1>
  597. <p>my first post</p>
  598. </body>
  599. </html>
  600. $ cat public/post/second/index.html
  601. <!DOCTYPE html>
  602. <html>
  603. <head>
  604. <title>second</title>
  605. </head>
  606. <body>
  607. <h1>second</h1>
  608. <p>my second post</p>
  609. </body>
  610. </html>
  611. $
  612. ```
  613. Notice that the posts now have content. You can go to localhost:1313/post/first to verify.
  614. ### Linking to Content
  615. 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.
  616. ```
  617. $ vi themes/zafta/layouts/index.html
  618. <!DOCTYPE html>
  619. <html>
  620. <body>
  621. {{ range first 10 .Data.Pages }}
  622. <h1><a href="{{ .Permalink }}">{{ .Title }}</a></h1>
  623. {{ end }}
  624. </body>
  625. </html>
  626. ```
  627. Build the web site and verify the results.
  628. ```
  629. $ rm -rf public
  630. $ hugo --verbose
  631. INFO: 2014/09/29 Using config file: /Users/quoha/Sites/zafta/config.toml
  632. INFO: 2014/09/29 syncing from /Users/quoha/Sites/zafta/themes/zafta/static/ to /Users/quoha/Sites/zafta/public/
  633. INFO: 2014/09/29 syncing from /Users/quoha/Sites/zafta/static/ to /Users/quoha/Sites/zafta/public/
  634. INFO: 2014/09/29 found taxonomies: map[string]string{"tag":"tags", "category":"categories"}
  635. WARN: 2014/09/29 Unable to locate layout: [404.html theme/404.html]
  636. 0 draft content
  637. 0 future content
  638. 2 pages created
  639. 0 tags created
  640. 0 categories created
  641. in 4 ms
  642. $ find public -type f -name '*.html' | xargs ls -l
  643. -rw-r--r-- 1 quoha staff 149 Sep 29 22:44 public/index.html
  644. -rw-r--r-- 1 quoha staff 125 Sep 29 22:44 public/post/first/index.html
  645. -rw-r--r-- 1 quoha staff 0 Sep 29 22:44 public/post/index.html
  646. -rw-r--r-- 1 quoha staff 128 Sep 29 22:44 public/post/second/index.html
  647. $ cat public/index.html
  648. <!DOCTYPE html>
  649. <html>
  650. <body>
  651. <h1><a href="/post/second/">second</a></h1>
  652. <h1><a href="/post/first/">first</a></h1>
  653. </body>
  654. </html>
  655. $
  656. ```
  657. ### Create a Post Listing
  658. 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).
  659. 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.
  660. ```
  661. $ find themes/zafta -name list.html | xargs ls -l
  662. -rw-r--r-- 1 quoha staff 0 Sep 29 17:31 themes/zafta/layouts/_default/list.html
  663. ```
  664. 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.
  665. ## Creating Top Level Pages
  666. Let's add an "about" page and display it at the top level (as opposed to a sub-level like we did with posts).
  667. 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:
  668. ```
  669. $ vi content/about.md
  670. +++
  671. title = "about"
  672. description = "about this site"
  673. date = "2014-09-27"
  674. slug = "about time"
  675. +++
  676. ## about us
  677. i'm speechless
  678. :wq
  679. ```
  680. Generate the web site and verify the results.
  681. ```
  682. $ find public -name '*.html' | xargs ls -l
  683. -rw-rw-r-- 1 mdhender staff 334 Sep 27 15:08 public/about-time/index.html
  684. -rw-rw-r-- 1 mdhender staff 527 Sep 27 15:08 public/index.html
  685. -rw-rw-r-- 1 mdhender staff 358 Sep 27 15:08 public/post/first-post/index.html
  686. -rw-rw-r-- 1 mdhender staff 0 Sep 27 15:08 public/post/index.html
  687. -rw-rw-r-- 1 mdhender staff 342 Sep 27 15:08 public/post/second-post/index.html
  688. ```
  689. 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.
  690. One other thing. Take a look at the home page.
  691. ```
  692. $ cat public/index.html
  693. <!DOCTYPE html>
  694. <html>
  695. <body>
  696. <h1><a href="http://localhost:1313/post/theme/">creating a new theme</a></h1>
  697. <h1><a href="http://localhost:1313/about-time/">about</a></h1>
  698. <h1><a href="http://localhost:1313/post/second-post/">second</a></h1>
  699. <h1><a href="http://localhost:1313/post/first-post/">first</a></h1>
  700. <script>document.write('<script src="http://'
  701. + (location.host || 'localhost').split(':')[0]
  702. + ':1313/livereload.js?mindelay=10"></'
  703. + 'script>')</script></body>
  704. </html>
  705. ```
  706. Notice that the "about" link is listed with the posts? That's not desirable, so let's change that first.
  707. ```
  708. $ vi themes/zafta/layouts/index.html
  709. <!DOCTYPE html>
  710. <html>
  711. <body>
  712. <h1>posts</h1>
  713. {{ range first 10 .Data.Pages }}
  714. {{ if eq .Type "post"}}
  715. <h2><a href="{{ .Permalink }}">{{ .Title }}</a></h2>
  716. {{ end }}
  717. {{ end }}
  718. <h1>pages</h1>
  719. {{ range .Data.Pages }}
  720. {{ if eq .Type "page" }}
  721. <h2><a href="{{ .Permalink }}">{{ .Title }}</a></h2>
  722. {{ end }}
  723. {{ end }}
  724. </body>
  725. </html>
  726. :wq
  727. ```
  728. 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.
  729. But, that about page still renders to about-time/index.html.
  730. ```
  731. $ find public -name '*.html' | xargs ls -l
  732. -rw-rw-r-- 1 mdhender staff 334 Sep 27 15:33 public/about-time/index.html
  733. -rw-rw-r-- 1 mdhender staff 645 Sep 27 15:33 public/index.html
  734. -rw-rw-r-- 1 mdhender staff 358 Sep 27 15:33 public/post/first-post/index.html
  735. -rw-rw-r-- 1 mdhender staff 0 Sep 27 15:33 public/post/index.html
  736. -rw-rw-r-- 1 mdhender staff 342 Sep 27 15:33 public/post/second-post/index.html
  737. ```
  738. 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.
  739. ```
  740. $ vi config.toml
  741. [permalinks]
  742. page = "/:title/"
  743. about = "/:filename/"
  744. ```
  745. 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.
  746. ## Sharing Templates
  747. 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.
  748. We can put the common bits into a shared template that's stored in the themes/zafta/layouts/partials/ directory.
  749. ### Create the Header and Footer Partials
  750. 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.
  751. ```
  752. $ vi themes/zafta/layouts/partials/header.html
  753. <!DOCTYPE html>
  754. <html>
  755. <head>
  756. <title>{{ .Title }}</title>
  757. </head>
  758. <body>
  759. :wq
  760. $ vi themes/zafta/layouts/partials/footer.html
  761. </body>
  762. </html>
  763. :wq
  764. ```
  765. ### Update the Home Page Template to Use the Partials
  766. The most noticeable difference between a template call and a partials call is the lack of path:
  767. ```
  768. {{ template "theme/partials/header.html" . }}
  769. ```
  770. versus
  771. ```
  772. {{ partial "header.html" . }}
  773. ```
  774. Both pass in the context.
  775. Let's change the home page template to use these new partials.
  776. ```
  777. $ vi themes/zafta/layouts/index.html
  778. {{ partial "header.html" . }}
  779. <h1>posts</h1>
  780. {{ range first 10 .Data.Pages }}
  781. {{ if eq .Type "post"}}
  782. <h2><a href="{{ .Permalink }}">{{ .Title }}</a></h2>
  783. {{ end }}
  784. {{ end }}
  785. <h1>pages</h1>
  786. {{ range .Data.Pages }}
  787. {{ if or (eq .Type "page") (eq .Type "about") }}
  788. <h2><a href="{{ .Permalink }}">{{ .Type }} - {{ .Title }} - {{ .RelPermalink }}</a></h2>
  789. {{ end }}
  790. {{ end }}
  791. {{ partial "footer.html" . }}
  792. :wq
  793. ```
  794. 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.
  795. ### Update the Default Single Template to Use the Partials
  796. ```
  797. $ vi themes/zafta/layouts/_default/single.html
  798. {{ partial "header.html" . }}
  799. <h1>{{ .Title }}</h1>
  800. {{ .Content }}
  801. {{ partial "footer.html" . }}
  802. :wq
  803. ```
  804. 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.
  805. ## Add “Date Published” to Posts
  806. 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.
  807. ### Add “Date Published” to the Template
  808. We'll start by updating the template used to render the posts. The template code will look like:
  809. ```
  810. {{ .Date.Format "Mon, Jan 2, 2006" }}
  811. ```
  812. Posts use the default single template, so we'll change that file.
  813. ```
  814. $ vi themes/zafta/layouts/_default/single.html
  815. {{ partial "header.html" . }}
  816. <h1>{{ .Title }}</h1>
  817. <h2>{{ .Date.Format "Mon, Jan 2, 2006" }}</h2>
  818. {{ .Content }}
  819. {{ partial "footer.html" . }}
  820. :wq
  821. ```
  822. 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.
  823. 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.
  824. The "if" solution works for sites that have just a couple of content types. It aligns with the principle of "code for today," too.
  825. 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.
  826. Let's restore the default single template before we forget.
  827. ```
  828. $ mkdir themes/zafta/layouts/post
  829. $ vi themes/zafta/layouts/_default/single.html
  830. {{ partial "header.html" . }}
  831. <h1>{{ .Title }}</h1>
  832. {{ .Content }}
  833. {{ partial "footer.html" . }}
  834. :wq
  835. ```
  836. 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.
  837. ```
  838. $ vi themes/zafta/layouts/post/single.html
  839. {{ partial "header.html" . }}
  840. <h1>{{ .Title }}</h1>
  841. <h2>{{ .Date.Format "Mon, Jan 2, 2006" }}</h2>
  842. {{ .Content }}
  843. {{ partial "footer.html" . }}
  844. :wq
  845. ```
  846. 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.
  847. ### Don't Repeat Yourself
  848. 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.