You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
3.3 KiB

4 years ago
  1. +++
  2. title = "Custom Layouts"
  3. description = "Customize layouts without modifying theme source."
  4. categories = ["customizing"]
  5. tags = ["layout", "templating", "style"]
  6. features = ["code highlighter", "snippets", "related content"]
  7. [[copyright]]
  8. owner = "Josh Habdas"
  9. date = "2019"
  10. license = "agpl-3.0-or-later"
  11. +++
  12. After Dark uses block templates to facilitate the creation of unique page layouts anywhere on your site. Use them to add [Snippets](../snippets) to pages in a section, selectively apply [Custom Styles](../custom-styles) or add an about section to the homepage.
  13. ## How it works
  14. Given the following `block` with optional default value:
  15. ```go-html-template
  16. <title>{{ block "title" }}Site Title{{ end }}</title>
  17. ```
  18. Inheriting templates may omit the block and keep the default value or `define` the block to change its value, as shown here:
  19. ```go-html-template
  20. {{ define "title" }}Page Title | Site Title{{ end }}
  21. ```
  22. Combined with {{< external href="https://gohugo.io/templates/lookup-order/" text="Lookup Order in Hugo" />}} blocks become valuable in designating editable regions within, and improving reuse of, existing template files.
  23. ## Applied in context
  24. Here's the template used to display an individual page in After Dark:
  25. {{< highlight go-html-template >}}
  26. {{< include "themes/after-dark/layouts/_default/single.html" >}}
  27. {{< /highlight >}}
  28. There's not much to it. Most of the code is inherited from another template, giving a clear picture of the page structure and making modifications trivial.
  29. ## Creating your own
  30. Imagine you're creating an Audiobooks section for your site and want two new custom layouts: one to list audio clips and another to describe them.
  31. To achieve this using block templates first create an `audiobook` section with a single page to describe a clip:
  32. ```sh
  33. $ hugo new audiobook/the-power-of-now.md
  34. ```
  35. Resulting in the following `content` tree structure:
  36. ```
  37. ├── assets
  38. ├── content
  39. │   └── audiobook
  40. │   └── the-power-of-now.md
  41. ├── layouts
  42. ```
  43. If already serving your site the Audiobooks section and page will appear immediately using the default block templates. To customize from default add a `list.html` and `single.html` to a `layouts/audiobook` directory in your site.
  44. If the files don't exist yet, copy them from theme defaults:
  45. ```sh
  46. $ mkdir -p layouts/audiobook
  47. $ cp themes/after-dark/layouts/_default/list.html layouts/audiobook
  48. $ cp themes/after-dark/layouts/_default/single.html layouts/audiobook
  49. ```
  50. Resulting in the following `layouts` tree structure:
  51. ```
  52. ├── content
  53. ├── layouts
  54. │   └── audiobook
  55. │   ├── list.html
  56. │ └── single.html
  57. ├── static
  58. ```
  59. Adjust `list.html` and `single.html` layouts and use [Custom Styles](../custom-styles) to achieve the desired result. Reference the following resources for help:
  60. - {{< external "https://gohugo.io/templates/" />}} for templating functions and logic
  61. - {{< external "https://devdocs.io" />}} for a comprehensive HTML and CSS reference
  62. - {{< external "https://internetingishard.com" />}} learn HTML & CSS for free
  63. - {{< external "https://inclusive-components.design" />}} for design pattern ideas
  64. - {{< external "https://diveintohtml5.info" >}} background behind HTML5
  65. - {{< external "https://gsnedders.html5.org/outliner/" />}} test your HTML document outline