hugo/docs/content/templates/variables.md
Alexandre Bourget ec33732fbe Add multilingual support in Hugo
Implements:
* support to render:
  * content/post/whatever.en.md to /en/2015/12/22/whatever/index.html
  * content/post/whatever.fr.md to /fr/2015/12/22/whatever/index.html
* gets enabled when `Multilingual:` is specified in config.
* support having language switchers in templates, that know
  where the translated page is (with .Page.Translations)
  (when you're on /en/about/, you can have a "Francais" link pointing to
   /fr/a-propos/)
  * all translations are in the `.Page.Translations` map, including the current one.
* easily tweak themes to support Multilingual mode
* renders in a single swift, no need for two config files.

Adds a couple of variables useful for multilingual sites

Adds documentation (content/multilingual.md)

Added language prefixing for all URL generation/permalinking see in the
code base.

Implements i18n. Leverages the great github.com/nicksnyder/go-i18n lib.. thanks Nick.
* Adds "i18n" and "T" template functions..
2016-09-06 18:32:15 +03:00

12 KiB

aliases lastmod date linktitle menu next prev title weight toc
/doc/variables/
/layout/variables/
2015-12-08 2013-07-01 Variables
main
parent
layout
/templates/content /templates/functions Template Variables 20 true

Hugo makes a set of values available to the templates. Go templates are context based. The following are available in the context for the templates.

Page Variables

The following is a list of most of the accessible variables which can be defined for a piece of content. Many of these will be defined in the front matter, content or derived from file location.

See also: Scratch for page-scoped writable variables.

.Title The title for the content.
.Content The content itself, defined below the front matter.
.Summary A generated summary of the content for easily showing a snippet in a summary view. Note that the breakpoint can be set manually by inserting <!--more--> at the appropriate place in the content page. See Summaries for more details.
.Truncated A boolean, true if the .Summary is truncated. Useful for showing a "Read more..." link only if necessary. See Summaries for more details.
.Description The description for the content.
.Keywords The meta keywords for this content.
.Date The date the content is associated with.
.Lastmod The date the content was last modified.
.PublishDate The date the content is published on.
.ExpiryDate The date where the content is scheduled to expire on.
.Type The content type (e.g. post).
.Section The section this content belongs to.
.Permalink The Permanent link for this page.
.RelPermalink The Relative permanent link for this page.
.LinkTitle Access when creating links to this content. Will use linktitle if set in front matter, else title.
.RSSLink Link to the taxonomies' RSS link.
.TableOfContents The rendered table of contents for this content.
.Prev Pointer to the previous content (based on pub date).
.Next Pointer to the following content (based on pub date).
.PrevInSection Pointer to the previous content within the same section (based on pub date). For example, {{if .PrevInSection}}{{.PrevInSection.Permalink}}{{end}}.
.NextInSection Pointer to the following content within the same section (based on pub date)
.FuzzyWordCount The approximate number of words in the content.
.WordCount The number of words in the content.
.ReadingTime The estimated time it takes to read the content in minutes.
.Weight Assigned weight (in the front matter) to this content, used in sorting.
.RawContent Raw Markdown content without the metadata header. Useful with remarkjs.com
.UniqueID The MD5-checksum of the page's filename
.Draft A boolean, true if the content is marked as a draft in the front matter.
.IsNode Always false for pages.
.IsPage Always true for page.
.Site See [Site Variables]({{< relref "#site-variables" >}}) below.
.Hugo See [Hugo Variables]({{< relref "#hugo-variables" >}}) below.
.Translations A map to other pages with the same filename, but with a different language-extension (like post.fr.md). Populated only if Multilingual is enabled in your site config. .Lang Taken from the language extension notation. Populated only if Multilingual is enabled for your site config.

Page Params

Any other value defined in the front matter, including taxonomies, will be made available under .Params. For example, the tags and categories taxonomies are accessed with:

  • .Params.tags
  • .Params.categories

All Params are only accessible using all lowercase characters.

This is particularly useful for the introduction of user defined fields in content files. For example, a Hugo website on book reviews could have in the front matter of /content/review/book01.md

---
...
affiliatelink: "http://www.my-book-link.here"
recommendedby: "my Mother"
---

Which would then be accessible to a template at /themes/yourtheme/layouts/review/single.html through .Params.affiliatelink and .Params.recommendedby, respectively. Two common situations where these could be introduced are as a value of a certain attribute (like href="" below) or by itself to be displayed. Sample syntaxes include:

<h3><a href={{ printf "%s" $.Params.affiliatelink }}>Buy this book</a></h3>
<p>It was recommended by {{ .Params.recommendedby }}.</p>

which would render

<h3><a href="http://www.my-book-link.here">Buy this book</a></h3>
<p>It was recommended by my Mother.</p>

See also: [Archetypes]({{% ref "content/archetypes.md" %}}) for consistency of Params across pieces of content.

Param method

In Hugo you can declare params both for the site and the individual page. A common use case is to have a general value for the site and a more specific value for some of the pages (i.e. an image).

With the Param method the most specific value will be selected for you, and it is safe to use it in any template (it's defined on both Page and Node):

$.Param "image"

Node Variables

In Hugo, a node is any page not rendered directly by a content file. This includes taxonomies, lists and the homepage.

See also: Scratch for global node variables.

.Title The title for the content.
.Date The date the content is published on.
.Lastmod The date the content was last modified.
.Permalink The Permanent link for this node
.URL The relative URL for this node.
.Ref(ref) Returns the permalink for ref. See [cross-references]({{% ref "extras/crossreferences.md" %}}). Does not handle in-page fragments correctly.
.RelRef(ref) Returns the relative permalink for ref. See [cross-references]({{% ref "extras/crossreferences.md" %}}). Does not handle in-page fragments correctly.
.RSSLink Link to the taxonomies' RSS link.
.Data The data specific to this type of node.
.IsHome True if the node is the site home page.
.IsNode Always true for nodes.
.IsPage Always false for nodes.
.Site See [Site Variables]({{< relref "#site-variables" >}}) below.
.Hugo See [Hugo Variables]({{< relref "#hugo-variables" >}}) below.

Taxonomy Terms Node Variables

Taxonomy Terms pages are of the type "node" and have the following additional variables. These are available in layouts/_defaults/terms.html for example.

.Data.Singular The singular name of the taxonomy
.Data.Plural The plural name of the taxonomy
.Data.Pages the list of pages in this taxonomy
.Data.Terms The taxonomy itself
.Data.Terms.Alphabetical The Terms alphabetized
.Data.Terms.ByCount The Terms ordered by popularity

The last two can also be reversed: .Data.Terms.Alphabetical.Reverse, .Data.Terms.ByCount.Reverse.

Taxonomies elsewhere

The .Site.Taxonomies variable holds all taxonomies defines site-wide. It is a map of the taxonomy name to a list of its values. For example: "tags" -> ["tag1", "tag2", "tag3"]. Each value, though, is not a string but rather a Taxonomy variable.

The Taxonomy variable

The Taxonomy variable, available as .Site.Taxonomies.tags for example, contains the list of tags (values) and, for each of those, their corresponding content pages.

Site Variables

Also available is .Site which has the following:

.Site.BaseURL The base URL for the site as defined in the site configuration file.
.Site.RSSLink The URL for the site RSS.
.Site.Taxonomies The taxonomies for the entire site. Replaces the now-obsolete .Site.Indexes since v0.11. Also see section Taxonomies elsewhere.
.Site.Pages Array of all content ordered by Date, newest first. Replaces the now-deprecated .Site.Recent starting v0.13. This array contains only the pages in the current language.
.Site.AllPages Array of all pages regardless of their translation.
.Site.Params A container holding the values from the params section of your site configuration file. For example, a TOML config file might look like this:

baseurl = "http://yoursite.example.com/"

[params]
  description = "Tesla's Awesome Hugo Site"
  author = "Nikola Tesla"

.Site.Sections Top level directories of the site.
.Site.Files All of the source files of the site.
.Site.Menus All of the menus in the site.
.Site.Title A string representing the title of the site.
.Site.Author A map of the authors as defined in the site configuration.
.Site.LanguageCode A string representing the language as defined in the site configuration. This is mostly used to populate the RSS feeds with the right language code.
.Site.DisqusShortname A string representing the shortname of the Disqus shortcode as defined in the site configuration.
.Site.GoogleAnalytics A string representing your tracking code for Google Analytics as defined in the site configuration.
.Site.Copyright A string representing the copyright of your web site as defined in the site configuration.
.Site.LastChange A string representing the date/time of the most recent change to your site, based on the [date variable]({{< ref "content/front-matter.md#required-variables" >}}) in the front matter of your content pages.
.Site.Permalinks A string to override the default permalink format. Defined in the site configuration.
.Site.BuildDrafts A boolean (Default: false) to indicate whether to build drafts. Defined in the site configuration.
.Site.Data Custom data, see Data Files.
.Site.Multilingual Whether the site supports internationalization of the content. With this mode enabled, all your posts' URLs will be prefixed with the language (ex: /en/2016/01/01/my-post)
.Site.CurrentLanguage This indicates which language you are currently rendering the website for. When using Multilingual mode, will render the site in this language. You can then run hugo again with a second config file, with the other languages. When using i18n and T template functions, it will use the i18n/*.yaml files (in either /themes/[yourtheme]/i18n or the /i18n, translations in the latter having precedence).
.Site.LanguagePrefix When Multilingual is enabled, this will hold /{{ .Site.CurrentLanguage}}, otherwise will be an empty string. Using this to prefix taxonomies or other hard-coded links ensures your keep your theme compatible with Multilingual configurations. .Site.Languages An ordered list of languages when Multilingual is enabled. Used in your templates to iterate through and create links to different languages.

File Variables

The .File variable gives you additional information of a page.

Note: .File is only accessible on Pages but not on Nodes (like the homepage and list pages).

Available are the following attributes:

.File.Path The original relative path of the page, e.g. content/posts/foo.md
.File.LogicalName The name of the content file that represents a page, e.g. foo.md
.File.BaseFileName The filename without extension, e.g. foo
.File.Ext or .File.Extension The file extension of the content file, e.g. md
.File.Dir Given the path content/posts/dir1/dir2/, the relative directory path of the content file will be returned, e.g. posts/dir1/dir2/

Hugo Variables

Also available is .Hugo which has the following:

.Hugo.Generator Meta tag for the version of Hugo that generated the site. Highly recommended to be included by default in all theme headers so we can start to track the usage and popularity of Hugo. Unlike other variables it outputs a complete HTML tag, e.g. <meta name="generator" content="Hugo 0.15" />
.Hugo.Version The current version of the Hugo binary you are using e.g. 0.13-DEV
.Hugo.CommitHash The git commit hash of the current Hugo binary e.g. 0e8bed9ccffba0df554728b46c5bbf6d78ae5247
.Hugo.BuildDate The compile date of the current Hugo binary formatted with RFC 3339 e.g. 2002-10-02T10:00:00-05:00