5 Custom Pages and Customized Content with the Go Template Language

 

This chapter covers:

  • Building custom pages in Hugo.
  • Rendering content and accessing variables and functions in the Go Template language.
  • Accessing Hugo’s configuration and front matter in code.
  • Reading from the filesystem using Hugo.
  • Creating custom shortcodes
  • Create reusable page templates called archetypes

One strength of Hugo is a clear separation of concerns between content (markdown) and presentation (HTML/CSS/JavaScript). While creating content we rarely have to deal with HTML. Team members who are not well versed in HTML/CSS can be successful with Hugo as a content management system using features we discussed so far in this book. A lot more power can be unlocked in going one layer deeper into the layouts and HTML generation. In chapter 2 we have stepped out of the theme to create the home page in pure HTML. In doing so, we eschewed the role of the content creator and went into the web developer mode while mostly supplying content for the other pages. In this chapter we will get first-hand experience with Hugo’s template language, its rendering mechanism as we improve the home page of the Acme Corporation website and add some features which are based on the content which a static HTML page cannot provide.

The pure HTML index page we currently have for the Acme Corporation website has intermingling of content and layout. This approach has a huge set of problems:

5.1  Separating data and design

5.1.1  Accessing the Go Template language

5.1.2  Existence checks

5.1.3  Using site variables for defaults

5.1.4  Creating variables for simplification

5.1.5  Using standard library functions to reduce the code size

5.1.6  Using the content switch via the with conditional for simplifying the checks further

5.1.7   Add some processing of content

5.1.8  Adding markdown content

5.2  Using Hugo generated properties to add content

5.2.1  Adding the menu

5.2.2  Adding recent blog posts

5.3  Playing with structured data

5.3.1  Using Front Matter for structured data

5.3.2  Parsing files for data

5.4  Enhancing life with the go template language

5.4.1  Template code in shortcodes

sitemap