mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-20 21:31:32 +02:00
Merge commit 'f96384a3b596f9bc0a3a035970b09b2c601f0ccb'
This commit is contained in:
@@ -2,20 +2,14 @@
|
||||
title: Variables and Params
|
||||
linktitle: Variables Overview
|
||||
description: Page-, file-, taxonomy-, and site-level variables and parameters available in templates.
|
||||
date: 2017-02-01
|
||||
publishdate: 2017-02-01
|
||||
lastmod: 2017-02-01
|
||||
categories: [variables and params]
|
||||
keywords: [variables,params,values,globals]
|
||||
draft: false
|
||||
menu:
|
||||
docs:
|
||||
parent: "variables"
|
||||
parent: variables
|
||||
weight: 1
|
||||
weight: 01 #rem
|
||||
sections_weight: 01
|
||||
weight: 01
|
||||
aliases: [/templates/variables/]
|
||||
toc: false
|
||||
---
|
||||
|
||||
Hugo's templates are context aware and make a large number of values available to you as you're creating views for your website.
|
||||
|
@@ -1,54 +1,102 @@
|
||||
---
|
||||
title: File Variables
|
||||
linktitle:
|
||||
description: "You can access filesystem-related data for a content file in the `.File` variable."
|
||||
date: 2017-02-01
|
||||
publishdate: 2017-02-01
|
||||
lastmod: 2017-02-01
|
||||
description: "Use File variables to access file-related values for each page that is backed by a file."
|
||||
categories: [variables and params]
|
||||
keywords: [files]
|
||||
draft: false
|
||||
menu:
|
||||
docs:
|
||||
parent: "variables"
|
||||
parent: variables
|
||||
weight: 40
|
||||
toc: true
|
||||
weight: 40
|
||||
sections_weight: 40
|
||||
aliases: [/variables/file-variables/]
|
||||
toc: false
|
||||
---
|
||||
## Variables
|
||||
|
||||
{{% note "Rendering Local Files" %}}
|
||||
For information on creating shortcodes and templates that tap into Hugo's file-related feature set, see [Local File Templates](/templates/files/).
|
||||
{{% note %}}
|
||||
The path separators (slash or backslash) in `.File.Path`, `.File.Dir`, and `.File.Filename` depend on the operating system.
|
||||
{{% /note %}}
|
||||
|
||||
The `.File` object contains the following fields:
|
||||
|
||||
.File.Path
|
||||
: the original relative path of the page, relative to the content dir (e.g., `posts/foo.en.md`)
|
||||
|
||||
.File.LogicalName
|
||||
: the name of the content file that represents a page (e.g., `foo.en.md`)
|
||||
|
||||
.File.TranslationBaseName
|
||||
: the filename without extension or optional language identifier (e.g., `foo`)
|
||||
|
||||
.File.ContentBaseName
|
||||
: is either a TranslationBaseName or name of containing folder if file is a leaf bundle.
|
||||
|
||||
.File.BaseFileName
|
||||
: the filename without extension (e.g., `foo.en`)
|
||||
|
||||
.File.Ext
|
||||
: the file extension of the content file (e.g., `md`).
|
||||
|
||||
.File.Lang
|
||||
: the language associated with the given file if Hugo's [Multilingual features][multilingual] are enabled (e.g., `en`)
|
||||
: (`string`) The file path, relative to the `content` directory.
|
||||
|
||||
.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/`). Note that the path separator (`\` or `/`) could be dependent on the operating system.
|
||||
: (`string`) The file path, excluding the file name, relative to the `content` directory.
|
||||
|
||||
.File.LogicalName
|
||||
: (`string`) The file name.
|
||||
|
||||
.File.BaseFileName
|
||||
: (`string`) The file name, excluding the extension.
|
||||
|
||||
.File.TranslationBaseName
|
||||
: (`string`) The file name, excluding the extension and language identifier.
|
||||
|
||||
.File.Ext
|
||||
: (`string`) The file extension.
|
||||
|
||||
.File.Lang
|
||||
: (`string`) The language associated with the given file.
|
||||
|
||||
|
||||
.File.ContentBaseName
|
||||
: (`string`) If the page is a branch or leaf bundle, the name of the containing directory, else the `.TranslationBaseName`.
|
||||
|
||||
.File.Filename
|
||||
: (`string`) The absolute file path.
|
||||
|
||||
.File.UniqueID
|
||||
: the MD5-checksum of the content file's path.
|
||||
: (`string`) The MD5 hash of `.File.Path`.
|
||||
|
||||
[Multilingual]: /content-management/multilingual/
|
||||
## Examples
|
||||
|
||||
```text
|
||||
content/
|
||||
├── news/
|
||||
│ ├── b/
|
||||
│ │ ├── index.de.md <-- leaf bundle
|
||||
│ │ └── index.en.md <-- leaf bundle
|
||||
│ ├── a.de.md <-- regular content
|
||||
│ ├── a.en.md <-- regular content
|
||||
│ ├── _index.de.md <-- branch bundle
|
||||
│ └── _index.en.md <-- branch bundle
|
||||
├── _index.de.md
|
||||
└── _index.en.md
|
||||
```
|
||||
|
||||
With the content structure above, the `.File` objects for the English pages contain the following properties:
|
||||
|
||||
|regular content|leaf bundle|branch bundle
|
||||
:--|:--|:--|:--
|
||||
Path|news/a.en.md|news/b/index.en.md|news/_index.en.md
|
||||
Dir|news/|news/b/|news/
|
||||
LogicalName|a.en.md|index.en.md|_index.en.md
|
||||
BaseFileName|a.en|index.en|_index.en
|
||||
TranslationBaseName|a|index|_index
|
||||
Ext|md|md|md
|
||||
Lang|en|en|en
|
||||
ContentBaseName|a|b|news
|
||||
Filename|/home/user/...|/home/user/...|/home/user/...
|
||||
UniqueID|15be14b...|186868f...|7d9159d...
|
||||
|
||||
## Defensive coding
|
||||
|
||||
Some of the pages on a site may not be backed by a file. For example:
|
||||
|
||||
- Top level section pages
|
||||
- Taxonomy pages
|
||||
- Term pages
|
||||
|
||||
Without a backing file, Hugo will throw a warning if you attempt to access a `.File` property. For example:
|
||||
|
||||
```text
|
||||
WARN .File.ContentBaseName on zero object. Wrap it in if or with...
|
||||
```
|
||||
|
||||
To code defensively:
|
||||
|
||||
```go-html-template
|
||||
{{ with .File }}
|
||||
{{ .ContentBaseName }}
|
||||
{{ end }}
|
||||
```
|
||||
|
@@ -2,24 +2,17 @@
|
||||
title: Git Info Variables
|
||||
linktitle: Git Variables
|
||||
description: Get the last Git revision information for every content file.
|
||||
date: 2017-03-12
|
||||
publishdate: 2017-03-12
|
||||
lastmod: 2017-03-12
|
||||
categories: [variables and params]
|
||||
keywords: [git]
|
||||
draft: false
|
||||
menu:
|
||||
docs:
|
||||
parent: "variables"
|
||||
parent: variables
|
||||
weight: 70
|
||||
weight: 70
|
||||
sections_weight: 70
|
||||
aliases: [/extras/gitinfo/]
|
||||
toc: false
|
||||
wip: false
|
||||
---
|
||||
|
||||
{{% note "`.GitInfo` Performance Considerations" %}}
|
||||
{{% note %}}
|
||||
Hugo's Git integrations should be fairly performant but *can* increase your build time. This will depend on the size of your Git history.
|
||||
{{% /note %}}
|
||||
|
||||
|
@@ -1,127 +1,92 @@
|
||||
---
|
||||
title: Menu Entry Properties
|
||||
linktitle: Menu Entry Properties
|
||||
description: A menu entry in a menu-template has specific variables and functions to make menu management easier.
|
||||
date: 2017-03-12
|
||||
publishdate: 2017-03-12
|
||||
lastmod: 2017-03-12
|
||||
title: Menu Variables
|
||||
description: Use these variables and methods in your menu templates.
|
||||
categories: [variables and params]
|
||||
keywords: [menus]
|
||||
draft: false
|
||||
menu:
|
||||
docs:
|
||||
title: "variables defined by a menu entry"
|
||||
parent: "variables"
|
||||
parent: variables
|
||||
weight: 50
|
||||
weight: 50
|
||||
sections_weight: 50
|
||||
aliases: [/variables/menu/]
|
||||
toc: false
|
||||
---
|
||||
|
||||
A **menu entry** has the following properties available that can be used in a
|
||||
[menu template][menu-template].
|
||||
## Variables
|
||||
|
||||
## Menu Entry Variables
|
||||
|
||||
.Menu
|
||||
: _string_ <br />
|
||||
Name of the **menu** that contains this **menu entry**.
|
||||
|
||||
.URL
|
||||
: _string_ <br />
|
||||
URL that the menu entry points to. The `url` key, if set for the menu entry,
|
||||
sets this value. If that key is not set, and if the menu entry is set in a page
|
||||
front-matter, this value defaults to the page's `.RelPermalink`.
|
||||
|
||||
.Page
|
||||
: _\*Page_ <br />
|
||||
Reference to the [page object][page-object] associated with the menu entry. This
|
||||
will be non-nil if the menu entry is set via a page's front-matter and not via
|
||||
the site config.
|
||||
|
||||
.PageRef
|
||||
: _string_ <br /> Can be set if defined in site config and the menu entry refers to a Page. [site.GetPage](/functions/getpage/) will be used to do the page lookup. If this is set, you don't need to set the `URL`.
|
||||
|
||||
.Name
|
||||
: _string_ <br />
|
||||
Name of the menu entry. The `name` key, if set for the menu entry, sets
|
||||
this value. If that key is not set, and if the menu entry is set in a page
|
||||
front-matter, this value defaults to the page's `.LinkTitle`.
|
||||
|
||||
.Identifier
|
||||
: _string_ <br />
|
||||
Value of the `identifier` key if set for the menu entry. This value must be
|
||||
unique for each menu entry. **It is necessary to set a unique identifier
|
||||
manually if two or more menu entries have the same `.Name`.**
|
||||
|
||||
.Pre
|
||||
: _template.HTML_ <br />
|
||||
Value of the `pre` key if set for the menu entry. This value typically contains
|
||||
a string representing HTML.
|
||||
|
||||
.Post
|
||||
: _template.HTML_ <br />
|
||||
Value of the `post` key if set for the menu entry. This value typically contains
|
||||
a string representing HTML.
|
||||
|
||||
.Weight
|
||||
: _int_ <br />
|
||||
Value of the `weight` key if set for the menu entry. By default the entries in
|
||||
a menu are sorted ascending by their `weight`. If that key is not set, and if
|
||||
the menu entry is set in a page front-matter, this value defaults to the page's
|
||||
`.Weight`.
|
||||
|
||||
.Parent
|
||||
: _string_ <br />
|
||||
Name (or Identifier if present) of this menu entry's parent **menu entry**. The
|
||||
`parent` key, if set for the menu entry, sets this value. If this key is set,
|
||||
this menu entry nests under that parent entry, else it nests directly under the
|
||||
`.Menu`.
|
||||
After [defining menu entries], access their properties in [menu templates] with these variables.
|
||||
|
||||
.Children
|
||||
: _Menu_ <br />
|
||||
This value is auto-populated by Hugo. It is a collection of children menu
|
||||
entries, if any, under the current menu entry.
|
||||
: (`menu`) A collection of child menu entries, if any, under the current menu entry.
|
||||
|
||||
## Menu Entry Functions
|
||||
|
||||
Menus also have the following functions available:
|
||||
|
||||
.HasChildren
|
||||
: _boolean_ <br />
|
||||
Returns `true` if `.Children` is non-nil.
|
||||
.Identifier
|
||||
: (`string`) The `identifier` property of the menu entry. If you define the menu entry [automatically], the page's `.Section`.
|
||||
|
||||
.KeyName
|
||||
: _string_ <br />
|
||||
Returns the `.Identifier` if present, else returns the `.Name`.
|
||||
: (`string`) The `identifier` property of the menu entry, else the `name` property.
|
||||
|
||||
.IsEqual
|
||||
: _boolean_ <br />
|
||||
Returns `true` if the two compared menu entries represent the same menu entry.
|
||||
.Menu
|
||||
: (`string`) The identifier of the menu that contains the menu entry.
|
||||
|
||||
.IsSameResource
|
||||
: _boolean_ <br />
|
||||
Returns `true` if the two compared menu entries have the same `.URL`.
|
||||
.Name
|
||||
: (`string`) The `name` property of the menu entry.
|
||||
|
||||
- If you define the menu entry [automatically], the page's `.LinkTitle`, else the page's `.Title`.
|
||||
- If you define the menu [in front matter] or [in site configuration], falls back to the page's `.LinkTitle`, then to the page's `.Title`.
|
||||
|
||||
.Page
|
||||
: (`page`) A reference to the page associated with the menu entry.
|
||||
|
||||
<!-- This provides no value when rendering menu. Omitting to avoid confusion.
|
||||
.PageRef
|
||||
: (`string`) The `pageRef` property of the menu entry.
|
||||
-->
|
||||
|
||||
.Params
|
||||
: (`map`) The `params` property of the menu entry.
|
||||
|
||||
.Parent
|
||||
: (`string`) The `parent` property of the menu entry.
|
||||
|
||||
.Post
|
||||
: (`template.HTML`) The `post` property of the menu entry.
|
||||
|
||||
.Pre
|
||||
: (`template.HTML`) The `pre` property of the menu entry.
|
||||
|
||||
.Title
|
||||
: _string_ <br />
|
||||
Link title, meant to be used in the `title` attribute of a menu entry's
|
||||
`<a>`-tags. Returns the menu entry's `title` key if set. Else, if the menu
|
||||
entry was created through a page's front-matter, it returns the page's
|
||||
`.LinkTitle`. Else, it just returns an empty string.
|
||||
: (`string`) The `title` property of the menu entry.
|
||||
|
||||
## Other Menu-related Functions
|
||||
- If you define the menu entry [automatically], the page's `.LinkTitle`, else the page's `.Title`.
|
||||
- If you define the menu [in front matter] or [in site configuration], falls back to the page's `.LinkTitle`, then to the page's `.Title`.
|
||||
|
||||
Additionally, here are some relevant methods available to menus on a page:
|
||||
.URL
|
||||
: (`string`) The `.RelPermalink` of the page associated with the menu entry. For menu entries pointing to external resources, the `url` property of the menu entry.
|
||||
|
||||
.IsMenuCurrent
|
||||
: _(menu string, menuEntry *MenuEntry ) boolean_ <br />
|
||||
See [`.IsMenuCurrent` method](/functions/ismenucurrent/).
|
||||
.Weight
|
||||
: (`int`) The `weight` property of the menu entry.
|
||||
|
||||
.HasMenuCurrent
|
||||
: _(menu string, menuEntry *MenuEntry) boolean_ <br />
|
||||
See [`.HasMenuCurrent` method](/functions/hasmenucurrent/).
|
||||
- If you define the menu entry [automatically], the page's `.Weight`.
|
||||
- If you define the menu [in front matter] or [in site configuration], falls back to the page's `.Weight`.
|
||||
|
||||
[menu-template]: /templates/menu-templates/
|
||||
[page-object]: /variables/page/
|
||||
## Methods
|
||||
|
||||
.HasChildren
|
||||
: (`bool`) Returns `true` if `.Children` is non-nil.
|
||||
|
||||
.IsEqual
|
||||
: (`bool`) Returns `true` if the compared menu entries represent the same menu entry.
|
||||
|
||||
.IsSameResource
|
||||
: (`bool`) Returns `true` if the compared menu entries point to the same resource.
|
||||
|
||||
.Page.HasMenuCurrent
|
||||
: (`bool`) Use this method to determine ancestors of the active menu entry. See [details](/functions/hasmenucurrent/).
|
||||
|
||||
.Page.IsMenuCurrent
|
||||
: (`bool`) Use this method to determine the active menu entry. See [details](/functions/ismenucurrent/).
|
||||
|
||||
[automatically]: /content-management/menus/#define-automatically
|
||||
[defining menu entries]: /content-management/menus/#overview
|
||||
[in front matter]: /content-management/menus/#define-in-front-matter
|
||||
[in site configuration]: /content-management/menus/#define-in-site-configuration
|
||||
[menu templates]: /templates/menu-templates/
|
||||
|
@@ -1,29 +1,18 @@
|
||||
---
|
||||
title: Page Variables
|
||||
linktitle:
|
||||
description: Page-level variables are defined in a content file's front matter, derived from the content's file location, or extracted from the content body itself.
|
||||
date: 2017-02-01
|
||||
publishdate: 2017-02-01
|
||||
categories: [variables and params]
|
||||
keywords: [pages]
|
||||
draft: false
|
||||
menu:
|
||||
docs:
|
||||
title: "variables defined by a page"
|
||||
parent: "variables"
|
||||
parent: variables
|
||||
weight: 20
|
||||
weight: 20
|
||||
sections_weight: 20
|
||||
aliases: []
|
||||
toc: true
|
||||
---
|
||||
|
||||
The following is a list of page-level variables. Many of these will be defined in the front matter, derived from file location, or extracted from the content itself.
|
||||
|
||||
{{% note "`.Scratch`" %}}
|
||||
See [`.Scratch`](/functions/scratch/) for page-scoped, writable variables.
|
||||
{{% /note %}}
|
||||
|
||||
## Page Variables
|
||||
|
||||
.AlternativeOutputFormats
|
||||
@@ -33,7 +22,7 @@ See [`.Scratch`](/functions/scratch/) for page-scoped, writable variables.
|
||||
: aliases of this page
|
||||
|
||||
.Ancestors
|
||||
: get the ancestors of each page, simplify [breadcrumb navigation]({{< relref "content-management/sections#example-breadcrumb-navigation" >}}) implementation complexity
|
||||
: get the ancestors of each page, simplify [breadcrumb navigation](/content-management/sections#example-breadcrumb-navigation) implementation complexity
|
||||
|
||||
.BundleType
|
||||
: the [bundle] type: `leaf`, `branch`, or an empty string if the page is not a bundle.
|
||||
@@ -59,6 +48,9 @@ See [`.Scratch`](/functions/scratch/) for page-scoped, writable variables.
|
||||
.File
|
||||
: filesystem-related data for this content file. See also [File Variables].
|
||||
|
||||
.Fragments
|
||||
: Fragments returns the fragments for this page. See [Page Fragments](#page-fragments).
|
||||
|
||||
.FuzzyWordCount
|
||||
: the approximate number of words in the content.
|
||||
|
||||
@@ -98,17 +90,17 @@ See also `.ExpiryDate`, `.Date`, `.PublishDate`, and [`.GitInfo`][gitinfo].
|
||||
: access when creating links to the content. If set, Hugo will use the `linktitle` from the front matter before `title`.
|
||||
|
||||
.Next
|
||||
: Points up to the next [regular page](/variables/site/#site-pages) (sorted by Hugo's [default sort](/templates/lists#default-weight--date--linktitle--filepath)). Example: `{{with .Next}}{{.Permalink}}{{end}}`. Calling `.Next` from the first page returns `nil`.
|
||||
: Points up to the next [regular page](/variables/site/#site-pages) (sorted by Hugo's [default sort](/templates/lists#default-weight--date--linktitle--filepath)). Example: `{{ with .Next }}{{ .Permalink }}{{ end }}`. Calling `.Next` from the first page returns `nil`.
|
||||
|
||||
.NextInSection
|
||||
: Points up to the next [regular page](/variables/site/#site-pages) below the same top level section (e.g. in `/blog`)). Pages are sorted by Hugo's [default sort](/templates/lists#default-weight--date--linktitle--filepath). Example: `{{with .NextInSection}}{{.Permalink}}{{end}}`. Calling `.NextInSection` from the first page returns `nil`.
|
||||
: Points up to the next [regular page](/variables/site/#site-pages) below the same top level section (e.g. in `/blog`)). Pages are sorted by Hugo's [default sort](/templates/lists#default-weight--date--linktitle--filepath). Example: `{{ with .NextInSection }}{{ .Permalink }}{{ end }}`. Calling `.NextInSection` from the first page returns `nil`.
|
||||
|
||||
.OutputFormats
|
||||
: contains all formats, including the current format, for a given page. Can be combined the with [`.Get` function](/functions/get/) to grab a specific format. (See [Output Formats](/templates/output-formats/).)
|
||||
|
||||
.Pages
|
||||
: a collection of associated pages. This value will be `nil` within
|
||||
the context of regular content pages. See [`.Pages`]({{< relref "page.md#pages" >}}).
|
||||
the context of regular content pages. See [`.Pages`](#pages).
|
||||
|
||||
.Permalink
|
||||
: the Permanent link for this page; see [Permalinks](/content-management/urls/)
|
||||
@@ -120,10 +112,10 @@ See also `.ExpiryDate`, `.Date`, `.PublishDate`, and [`.GitInfo`][gitinfo].
|
||||
: the slice of strings that results from splitting .Plain into words, as defined in Go's [strings.Fields](https://pkg.go.dev/strings#Fields).
|
||||
|
||||
.Prev
|
||||
: Points down to the previous [regular page](/variables/site/#site-pages) (sorted by Hugo's [default sort](/templates/lists#default-weight--date--linktitle--filepath)). Example: `{{if .Prev}}{{.Prev.Permalink}}{{end}}`. Calling `.Prev` from the last page returns `nil`.
|
||||
: Points down to the previous [regular page](/variables/site/#site-pages) (sorted by Hugo's [default sort](/templates/lists#default-weight--date--linktitle--filepath)). Example: `{{ if .Prev }}{{ .Prev.Permalink }}{{ end }}`. Calling `.Prev` from the last page returns `nil`.
|
||||
|
||||
.PrevInSection
|
||||
: Points down to the previous [regular page](/variables/site/#site-pages) below the same top level section (e.g. `/blog`). Pages are sorted by Hugo's [default sort](/templates/lists#default-weight--date--linktitle--filepath). Example: `{{if .PrevInSection}}{{.PrevInSection.Permalink}}{{end}}`. Calling `.PrevInSection` from the last page returns `nil`.
|
||||
: Points down to the previous [regular page](/variables/site/#site-pages) below the same top level section (e.g. `/blog`). Pages are sorted by Hugo's [default sort](/templates/lists#default-weight--date--linktitle--filepath). Example: `{{ if .PrevInSection }}{{ .PrevInSection.Permalink }}{{ end }}`. Calling `.PrevInSection` from the last page returns `nil`.
|
||||
|
||||
.PublishDate
|
||||
: the date on which the content was or will be published; `.Publishdate` pulls from the `publishdate` field in a content's front matter. See also `.ExpiryDate`, `.Date`, and `.Lastmod`.
|
||||
@@ -184,6 +176,14 @@ https://remarkjs.com)
|
||||
.WordCount
|
||||
: the number of words in the content.
|
||||
|
||||
## Writable Page-scoped Variables
|
||||
|
||||
[.Scratch][scratch]
|
||||
: returns a Scratch to store and manipulate data. In contrast to the [`.Store`][store] method, this scratch is reset on server rebuilds.
|
||||
|
||||
[.Store][store]
|
||||
: returns a Scratch to store and manipulate data. In contrast to the [`.Scratch`][scratch] method, this scratch is not reset on server rebuilds.
|
||||
|
||||
## Section Variables and Methods
|
||||
|
||||
Also see [Sections](/content-management/sections/).
|
||||
@@ -199,53 +199,92 @@ aliased form `.Pages`.
|
||||
|
||||
{{< getcontent path="readfiles/pages-vs-site-pages.md" >}}
|
||||
|
||||
## Page Fragments
|
||||
|
||||
{{< new-in "0.111.0" >}}
|
||||
|
||||
The `.Fragments` method returns a list of fragments for the current page.
|
||||
|
||||
.Headings
|
||||
: A recursive list of headings for the current page. Can be used to generate a table of contents.
|
||||
|
||||
{{< todo >}}add .Headings toc example{{< /todo >}}
|
||||
|
||||
.Identifiers
|
||||
: A sorted list of identifiers for the current page. Can be used to check if a page contains a specific identifier or if a page contains duplicate identifiers:
|
||||
|
||||
```go-html-template
|
||||
{{ if .Fragments.Identifiers.Contains "my-identifier" }}
|
||||
<p>Page contains identifier "my-identifier"</p>
|
||||
{{ end }}
|
||||
|
||||
{{ if gt (.Fragments.Identifiers.Count "my-identifier") 1 }}
|
||||
<p>Page contains duplicate "my-identifier" fragments</p>
|
||||
{{ end }}
|
||||
```
|
||||
|
||||
.HeadingsMap
|
||||
: Holds a map of headings for the current page. Can be used to start the table of contents from a specific heading.
|
||||
|
||||
Also see the [Go Doc](https://pkg.go.dev/github.com/gohugoio/hugo@v0.111.0/markup/tableofcontents#Fragments) for the return type.
|
||||
|
||||
### Fragments in hooks and shortcodes
|
||||
|
||||
`.Fragments` are safe to call from render hooks, even on the page you're on (`.Page.Fragments`). For shortcodes we recommend that all `.Fragments` usage is nested inside the `{{</**/>}}` shortcode delimiter (`{{%/**/%}}` takes part in the ToC creation so it's easy to end up in a situation where you bite yourself in the tail).
|
||||
|
||||
|
||||
## The global page function
|
||||
|
||||
{{< new-in "0.111.1" >}}
|
||||
|
||||
Hugo almost always passes a `Page` as the data context into the top level template (e.g. `single.html`) (the one exception is the multihost sitemap template). This means that you can access the current page with the `.` variable in the template.
|
||||
|
||||
But when you're deeply nested inside `.Render`, partial etc., accessing that `Page` object isn't always practical or possible.
|
||||
|
||||
For this reason, Hugo provides a global `page` function that you can use to access the current page from anywhere in any template.
|
||||
|
||||
```go-html-template
|
||||
{{ page.Title }}
|
||||
```
|
||||
|
||||
There are one caveat with this, and this isn't new, but it's worth mentioning here: There are situations in Hugo where you may see a cached value, e.g. when using `partialCached` or in a shortcode.
|
||||
|
||||
## Page-level Params
|
||||
|
||||
Any other value defined in the front matter in a content file, including taxonomies, will be made available as part of the `.Params` variable.
|
||||
|
||||
```yml
|
||||
---
|
||||
title: My First Post
|
||||
date: 2017-02-20T15:26:23-06:00
|
||||
{{< code-toggle file="content/example.md" fm=true copy=false >}}
|
||||
title: Example
|
||||
categories: [one]
|
||||
tags: [two,three,four]
|
||||
```
|
||||
{{< /code-toggle >}}
|
||||
|
||||
With the above front matter, the `tags` and `categories` taxonomies are accessible via the following:
|
||||
|
||||
* `.Params.tags`
|
||||
* `.Params.categories`
|
||||
|
||||
{{% note "Casing of Params" %}}
|
||||
Page-level `.Params` are *only* accessible in lowercase.
|
||||
{{% /note %}}
|
||||
The `.Params` variable is particularly useful for the introduction of user-defined front matter fields in content files. For example, a Hugo website on book reviews could have the following front matter:
|
||||
|
||||
The `.Params` variable is particularly useful for the introduction of user-defined front matter fields in content files. For example, a Hugo website on book reviews could have the following front matter in `/content/review/book01.md`:
|
||||
|
||||
```yml
|
||||
---
|
||||
...
|
||||
{{< code-toggle file="content/example.md" fm=true copy=false >}}
|
||||
title: Example
|
||||
affiliatelink: "http://www.my-book-link.here"
|
||||
recommendedby: "My Mother"
|
||||
...
|
||||
---
|
||||
{{< /code-toggle >}}
|
||||
|
||||
These fields would then be accessible to via `.Params.affiliatelink` and `.Params.recommendedby`.
|
||||
|
||||
```go-html-template
|
||||
<h3><a href="{{ .Params.affiliatelink }}">Buy this book</a></h3>
|
||||
<p>It was recommended by {{ .Params.recommendedby }}.</p>
|
||||
```
|
||||
|
||||
These fields would then be accessible to the `/themes/yourtheme/layouts/review/single.html` template through `.Params.affiliatelink` and `.Params.recommendedby`, respectively.
|
||||
This template would render as follows:
|
||||
|
||||
Two common situations where this type of front matter field could be introduced is as a value of a certain attribute like `href=""` or by itself to be displayed as text to the website's visitors.
|
||||
|
||||
{{< code file="/themes/yourtheme/layouts/review/single.html" >}}
|
||||
<h3><a href={{ printf "%s" $.Params.affiliatelink }}>Buy this book</a></h3>
|
||||
<p>It was recommended by {{ .Params.recommendedby }}.</p>
|
||||
{{< /code >}}
|
||||
|
||||
This template would render as follows, assuming you've set [`uglyURLs`](/content-management/urls/) to `false` in your [site `config`](/getting-started/configuration/):
|
||||
|
||||
{{< output file="yourbaseurl/review/book01/index.html" >}}
|
||||
```html
|
||||
<h3><a href="http://www.my-book-link.here">Buy this book</a></h3>
|
||||
<p>It was recommended by my Mother.</p>
|
||||
{{< /output >}}
|
||||
```
|
||||
|
||||
{{% note %}}
|
||||
See [Archetypes](/content-management/archetypes/) for consistency of `Params` across pieces of content.
|
||||
@@ -265,37 +304,22 @@ The `.Param` method provides a way to resolve a single value according to it's d
|
||||
|
||||
When front matter contains nested fields like the following:
|
||||
|
||||
```yml
|
||||
---
|
||||
{{< code-toggle file="content/example.md" fm=true copy=false >}}
|
||||
title: Example
|
||||
author:
|
||||
given_name: John
|
||||
family_name: Feminella
|
||||
display_name: John Feminella
|
||||
---
|
||||
```
|
||||
{{< /code-toggle >}}
|
||||
|
||||
`.Param` can access these fields by concatenating the field names together with a dot:
|
||||
|
||||
```go-html-template
|
||||
{{ $.Param "author.display_name" }}
|
||||
```
|
||||
|
||||
If your front matter contains a top-level key that is ambiguous with a nested key, as in the following case:
|
||||
|
||||
```yml
|
||||
---
|
||||
favorites.flavor: vanilla
|
||||
favorites:
|
||||
flavor: chocolate
|
||||
---
|
||||
```
|
||||
|
||||
The top-level key will be preferred. Therefore, the following method, when applied to the previous example, will print `vanilla` and not `chocolate`:
|
||||
|
||||
```txt
|
||||
{{ $.Param "favorites.flavor" }}
|
||||
=> vanilla
|
||||
```
|
||||
|
||||
[gitinfo]: /variables/git/
|
||||
[File Variables]: /variables/files/
|
||||
[bundle]: {{< relref "content-management/page-bundles" >}}
|
||||
[bundle]: /content-management/page-bundles
|
||||
[scratch]: /functions/scratch
|
||||
[store]: /functions/store
|
||||
|
@@ -1,18 +1,13 @@
|
||||
---
|
||||
title: Pages Methods
|
||||
linktitle:
|
||||
description: Pages is the core page collection in Hugo and has many useful methods.
|
||||
date: 2019-10-20
|
||||
categories: [variables and params]
|
||||
keywords: [pages]
|
||||
draft: false
|
||||
menu:
|
||||
docs:
|
||||
title: "methods defined on a page collection"
|
||||
parent: "variables"
|
||||
parent: variables
|
||||
weight: 21
|
||||
weight: 21
|
||||
sections_weight: 20
|
||||
aliases: [/pages]
|
||||
toc: true
|
||||
---
|
||||
@@ -23,8 +18,8 @@ Also see [List templates](/templates/lists) for an overview of sort methods.
|
||||
|
||||
`.Next` and `.Prev` on `Pages` work similar to the methods with the same names on `.Page`, but are more flexible (and slightly slower) as they can be used on any page collection.
|
||||
|
||||
`.Next` points **up** to the next page relative to the page sent in as the argument. Example: `{{with .Site.RegularPages.Next . }}{{.RelPermalink}}{{end}}`. Calling `.Next` with the first page in the collection returns `nil`.
|
||||
`.Next` points **up** to the next page relative to the page sent in as the argument. Example: `{{ with .Site.RegularPages.Next . }}{{ .RelPermalink }}{{ end }}`. Calling `.Next` with the first page in the collection returns `nil`.
|
||||
|
||||
## .Prev PAGE
|
||||
|
||||
`.Prev` points **down** to the previous page relative to the page sent in as the argument. Example: `{{with .Site.RegularPages.Prev . }}{{.RelPermalink}}{{end}}`. Calling `.Prev` with the last page in the collection returns `nil`.
|
||||
`.Prev` points **down** to the previous page relative to the page sent in as the argument. Example: `{{ with .Site.RegularPages.Prev . }}{{ .RelPermalink }}{{ end }}`. Calling `.Prev` with the last page in the collection returns `nil`.
|
||||
|
@@ -1,19 +1,13 @@
|
||||
---
|
||||
title: Shortcode Variables
|
||||
linktitle: Shortcode Variables
|
||||
description: Shortcodes can access page variables and also have their own specific built-in variables.
|
||||
date: 2017-03-12
|
||||
publishdate: 2017-03-12
|
||||
categories: [variables and params]
|
||||
keywords: [shortcodes]
|
||||
menu:
|
||||
docs:
|
||||
parent: "variables"
|
||||
parent: variables
|
||||
weight: 20
|
||||
weight: 20
|
||||
sections_weight: 20
|
||||
aliases: []
|
||||
toc: false
|
||||
---
|
||||
|
||||
[Shortcodes][shortcodes] have access to parameters delimited in the shortcode declaration via [`.Get`][getfunction], page- and site-level variables, and also the following shortcode-specific fields:
|
||||
@@ -39,9 +33,13 @@ toc: false
|
||||
.Inner
|
||||
: represents the content between the opening and closing shortcode tags when a [closing shortcode][markdownshortcode] is used
|
||||
|
||||
[getfunction]: /functions/get/
|
||||
[markdownshortcode]: /content-management/shortcodes/#shortcodes-with-markdown
|
||||
[shortcodes]: /templates/shortcode-templates/
|
||||
.Scratch
|
||||
: returns a writable [`Scratch`][scratch] to store and manipulate data which will be attached to the shortcode context. This scratch is reset on server rebuilds.
|
||||
|
||||
.InnerDeindent {{< new-in "0.100.0" >}}
|
||||
: Gets the `.Inner` with any indentation removed. This is what's used in the built-in `{{</* highlight */>}}` shortcode.
|
||||
|
||||
[getfunction]: /functions/get/
|
||||
[markdownshortcode]: /content-management/shortcodes/#shortcodes-with-markdown
|
||||
[shortcodes]: /templates/shortcode-templates/
|
||||
[scratch]: /functions/scratch
|
||||
|
@@ -1,19 +1,13 @@
|
||||
---
|
||||
title: Site Variables
|
||||
linktitle: Site Variables
|
||||
description: Many, but not all, site-wide variables are defined in your site's configuration. However, Hugo provides a number of built-in variables for convenient access to global values in your templates.
|
||||
date: 2017-02-01
|
||||
publishdate: 2017-02-01
|
||||
lastmod: 2017-02-01
|
||||
categories: [variables and params]
|
||||
keywords: [global,site]
|
||||
draft: false
|
||||
menu:
|
||||
docs:
|
||||
parent: "variables"
|
||||
parent: variables
|
||||
weight: 10
|
||||
weight: 10
|
||||
sections_weight: 10
|
||||
aliases: [/variables/site-variables/]
|
||||
toc: true
|
||||
---
|
||||
@@ -84,16 +78,16 @@ All the methods below, e.g. `.Site.RegularPages` can also be reached via the glo
|
||||
: all the menus in the site.
|
||||
|
||||
.Site.Pages
|
||||
: array of all content ordered by Date with the newest first. This array contains only the pages in the current language. See [`.Site.Pages`]({{< relref "site.md#site-pages" >}}).
|
||||
: array of all content ordered by Date with the newest first. This array contains only the pages in the current language. See [`.Site.Pages`](#site-pages).
|
||||
|
||||
.Site.RegularPages
|
||||
: a shortcut to the *regular* page collection. `.Site.RegularPages` is equivalent to `where .Site.Pages "Kind" "page"`. See [`.Site.Pages`]({{< relref "site.md#site-pages" >}}).
|
||||
: a shortcut to the *regular* page collection. `.Site.RegularPages` is equivalent to `where .Site.Pages "Kind" "page"`. See [`.Site.Pages`](#site-pages).
|
||||
|
||||
.Site.Sections
|
||||
: top-level directories of the site.
|
||||
|
||||
.Site.Taxonomies
|
||||
: the [taxonomies](/taxonomies/usage/) for the entire site. Also see section [Use `.Site.Taxonomies` Outside of Taxonomy Templates](/variables/taxonomy/#use-sitetaxonomies-outside-of-taxonomy-templates).
|
||||
: the [taxonomies](/content-management/taxonomies/) for the entire site. Also see section [Access taxonomy data from any template](/variables/taxonomy/#access-taxonomy-data-from-any-template).
|
||||
|
||||
.Site.Title
|
||||
: a string representing the title of the site.
|
||||
@@ -117,7 +111,7 @@ baseURL = "https://yoursite.example.com/"
|
||||
You can use `.Site.Params` in a [partial template](/templates/partials/) to call the default site description:
|
||||
|
||||
{{< code file="layouts/partials/head.html" >}}
|
||||
<meta name="description" content="{{if .IsHome}}{{ $.Site.Params.description }}{{else}}{{.Description}}{{end}}" />
|
||||
<meta name="description" content="{{ if .IsHome }}{{ $.Site.Params.description }}{{ else }}{{ .Description }}{{ end }}" />
|
||||
{{< /code >}}
|
||||
|
||||
## The `.Site.Pages` Variable {#site-pages}
|
||||
|
@@ -2,19 +2,13 @@
|
||||
title: Sitemap Variables
|
||||
linktitle: Sitemap Variables
|
||||
description:
|
||||
date: 2017-03-12
|
||||
publishdate: 2017-03-12
|
||||
categories: [variables and params]
|
||||
keywords: [sitemap]
|
||||
draft: false
|
||||
menu:
|
||||
docs:
|
||||
parent: "variables"
|
||||
parent: variables
|
||||
weight: 80
|
||||
weight: 80
|
||||
sections_weight: 80
|
||||
aliases: []
|
||||
toc: false
|
||||
---
|
||||
|
||||
A sitemap is a `Page` and therefore has all the [page variables][pagevars] available to use sitemap templates. They also have the following sitemap-specific variables available to them:
|
||||
|
@@ -1,16 +1,14 @@
|
||||
---
|
||||
title: Taxonomy Variables
|
||||
linktitle:
|
||||
description: Hugo's taxonomy system exposes variables to taxonomy and term templates.
|
||||
categories: [variables and params]
|
||||
keywords: [taxonomy,term]
|
||||
menu:
|
||||
docs:
|
||||
parent: "variables"
|
||||
parent: variables
|
||||
weight: 30
|
||||
toc: true
|
||||
weight: 30
|
||||
aliases: []
|
||||
---
|
||||
|
||||
## Taxonomy templates
|
||||
@@ -124,7 +122,7 @@ For example, to render the entire taxonomy data structure as a nested unordered
|
||||
<ul>
|
||||
{{ range $weightedPages }}
|
||||
<li>
|
||||
<a href="{{ .RelPermalink}}"> {{ .LinkTitle }}</a>
|
||||
<a href="{{ .RelPermalink }}"> {{ .LinkTitle }}</a>
|
||||
</li>
|
||||
{{ end }}
|
||||
</ul>
|
||||
|
Reference in New Issue
Block a user