5 Custom pages and customized content with the Go template language

 

This chapter covers

  • Customizing pages and shortcodes
  • 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
  • Creating reusable page templates called archetypes

One strength of Hugo is its clear separation of concerns between content (Markdown) and presentation (HTML/CSS/JavaScript). When we create content, we rarely have to deal with HTML. Team members who are not well versed in HTML or CSS can be successful with Hugo as a content management system using features we’ve discussed so far in this book. However, we can unlock a lot more power by digging deeper into layouts and HTML generation.

Chapter 2 has a section where we step 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 web developer mode, while primarily supplying content for the other pages. In this chapter, we will get first-hand experience with Hugo’s template language and its rendering mechanism as we improve the home page for the Acme Corporation website. In addition, we will add some features based on the content that a static HTML page cannot provide.

The pure HTML index page we currently have for the Acme Corporation website has an intermingling of content and layout. This approach has a massive 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 a context switch via the with conditional for simplifying further checks

5.1.7 Adding content processing

5.1.8 Adding Markdown content

5.2 Using external data to add content

sitemap