Curly Bracket Mysteries, the Mystery of T and --- vs. +++

The Mystery of those Curly Brackets and Other Hard To Find Objects

So you’ve made the decision that you will execute your Static Web Project using Hugo and you’ve spawned your new site, selected a Theme and added some content pages. Now you want to start looking at the partials that control how your pages are rendered and the first thing you come across are Double Curly Brackets, {{ }} or {{- }} or {{- -}} or {{ -}} and you are banging your head against the wall trying to determine what is the difference between these various notations. It is not immediately clear what you need to search for to find the answer in the documentation.

Well as your “Humble Trail Blazer” I hope I can save you some trouble and point you to Go Package Template Documentation.

Specifically you will find there that:

“Package template implements data-driven templates for generating textual output. …

Templates are executed by applying them to a data structure. Annotations in the template refer to elements of the data structure (typically a field of a struct or a key in a map) to control execution and derive values to be displayed. Execution of the template walks the structure and sets the cursor, represented by a period ‘.’ and called “dot”, to the value at the current location in the structure as execution proceeds.

The input text for a template is UTF-8-encoded text in any format. “Actions”–data evaluations or control structures–are delimited by “{{” and “}}"; all text outside actions is copied to the output unchanged. Except for raw strings, actions may not span newlines, although comments can.”

That document goes on to further clarify the situation with:

By default, all text between actions is copied verbatim when the template is executed. …

However, to aid in formatting template source code, if an action’s left delimiter (by default “{{") is followed immediately by a minus sign and white space, all trailing white space is trimmed from the immediately preceding text. Similarly, if the right delimiter ("}}") is preceded by white space and a minus sign, all leading white space is trimmed from the immediately following text. In these trim markers, the white space must be present: “{{- 3}}” is like “{{3}}” but trims the immediately preceding text, while “{{-3}}” parses as an action containing the number -3.

Another mystery I came across in trying to understand the template code was the use of the symbol T as in {{ T “post_nav_prev” }}. Turns out this Translates a piece of content based on your i18n configuration files.

In the i18n directory of the Theme directories there are a number of .yaml files that define various tags ids such as “post_nav_prev” and give the translation for that tag.

# Post nav
- id: post_nav_prev
  translation: "Previous"

The construct {{T “post_nav_prev”}} placed in a layout file will result in the appropriate set of characters being substituted based on language settings. In English this is “Previous”. More information on this and other language functions can be found by reviewing the I18N Documentation.

Another convention that had me searching and not finding is why is the Front Matter for a page is sometimes delimited by - - - and sometimes delimited by +++. The secret to this is that - - - is if you are using TOML syntax and +++ is if your are using YAML syntax.

All simple concepts if only you know where to look. It took your “Humble Trail Blazer” a few hours to find these answers. Hopefully, you have stumbled on this in less time and can proceed with your efforts to understand the more subltle aspects of your project without being held up by this. Namaste