| 1 |
--- |
| 2 |
title: Path |
| 3 |
description: Returns the logical path of the given page. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
returnType: string |
| 9 |
signatures: [PAGE.Path] |
| 10 |
--- |
| 11 |
|
| 12 |
The `Path` method on a `Page` object returns the logical path of the given page, regardless of whether the page is backed by a file. |
| 13 |
|
| 14 |
{{% glossary-term "logical path" %}} |
| 15 |
|
| 16 |
```go-html-template |
| 17 |
{{ .Path }} → /posts/post-1 |
| 18 |
``` |
| 19 |
|
| 20 |
The value returned by the `Path` method on a `Page` object is independent of content format, language, and URL modifiers such as the `slug` and `url` front matter fields. |
| 21 |
|
| 22 |
## Examples |
| 23 |
|
| 24 |
### Monolingual project |
| 25 |
|
| 26 |
Note that the logical path is independent of content format and URL modifiers. |
| 27 |
|
| 28 |
File path|Front matter slug|Logical path |
| 29 |
:--|:--|:-- |
| 30 |
`content/_index.md`||`/` |
| 31 |
`content/posts/_index.md`||`/posts` |
| 32 |
`content/posts/post-1.md`|`foo`|`/posts/post-1` |
| 33 |
`content/posts/post-2.html`|`bar`|`/posts/post-2` |
| 34 |
|
| 35 |
### Multilingual site |
| 36 |
|
| 37 |
Note that the logical path is independent of content format, language identifiers, and URL modifiers. |
| 38 |
|
| 39 |
File path|Front matter slug|Logical path |
| 40 |
:--|:--|:-- |
| 41 |
`content/_index.en.md`||`/` |
| 42 |
`content/_index.de.md`||`/` |
| 43 |
`content/posts/_index.en.md`||`/posts` |
| 44 |
`content/posts/_index.de.md`||`/posts` |
| 45 |
`content/posts/posts-1.en.md`|`foo`|`/posts/post-1` |
| 46 |
`content/posts/posts-1.de.md`|`foo`|`/posts/post-1` |
| 47 |
`content/posts/posts-2.en.html`|`bar`|`/posts/post-2` |
| 48 |
`content/posts/posts-2.de.html`|`bar`|`/posts/post-2` |
| 49 |
|
| 50 |
### Pages not backed by a file |
| 51 |
|
| 52 |
The `Path` method on a `Page` object returns a value regardless of whether the page is backed by a file. |
| 53 |
|
| 54 |
```tree |
| 55 |
content/ |
| 56 |
└── posts/ |
| 57 |
└── post-1.md <-- front matter: tags = ['hugo'] |
| 58 |
``` |
| 59 |
|
| 60 |
When you build the site: |
| 61 |
|
| 62 |
```tree |
| 63 |
public/ |
| 64 |
├── posts/ |
| 65 |
│ ├── post-1/ |
| 66 |
│ │ └── index.html .Page.Path = /posts/post-1 |
| 67 |
│ └── index.html .Page.Path = /posts |
| 68 |
├── tags/ |
| 69 |
│ ├── hugo/ |
| 70 |
│ │ └── index.html .Page.Path = /tags/hugo |
| 71 |
│ └── index.html .Page.Path = /tags |
| 72 |
└── index.html .Page.Path = / |
| 73 |
``` |
| 74 |
|
| 75 |
## Finding pages |
| 76 |
|
| 77 |
These methods, functions, and shortcodes use the logical path to find the given page: |
| 78 |
|
| 79 |
Methods|Functions|Shortcodes |
| 80 |
:--|:--|:-- |
| 81 |
[`Site.GetPage`][]|[`urls.Ref`][]|[`ref`][] |
| 82 |
[`Page.GetPage`][]|[`urls.RelRef`][]|[`relref`][] |
| 83 |
[`Page.Ref`][]| | |
| 84 |
[`Page.RelRef`][]| | |
| 85 |
[`Shortcode.Ref`][]| | |
| 86 |
[`Shortcode.RelRef`][]| | |
| 87 |
|
| 88 |
> [!NOTE] |
| 89 |
> Specify the logical path when using any of these methods, functions, or shortcodes. If you include a file extension or language identifier, Hugo will strip these values before finding the page in the logical tree. |
| 90 |
|
| 91 |
## Logical tree |
| 92 |
|
| 93 |
Just as file paths form a file tree, logical paths form a logical tree. |
| 94 |
|
| 95 |
A file tree: |
| 96 |
|
| 97 |
```tree |
| 98 |
content/ |
| 99 |
└── s1/ |
| 100 |
├── p1/ |
| 101 |
│ └── index.md |
| 102 |
└── p2.md |
| 103 |
``` |
| 104 |
|
| 105 |
The same content represented as a logical tree: |
| 106 |
|
| 107 |
```tree |
| 108 |
content/ |
| 109 |
└── s1/ |
| 110 |
├── p1 |
| 111 |
└── p2 |
| 112 |
``` |
| 113 |
|
| 114 |
A key difference between these trees is the relative path from p1 to p2: |
| 115 |
|
| 116 |
- In the file tree, the relative path from p1 to p2 is `../p2.md` |
| 117 |
- In the logical tree, the relative path is `p2` |
| 118 |
|
| 119 |
> [!NOTE] |
| 120 |
> Remember to use the logical path when using any of the methods, functions, or shortcodes listed in the previous section. If you include a file extension or language identifier, Hugo will strip these values before finding the page in the logical tree. |
| 121 |
|
| 122 |
[`Page.GetPage`]: /methods/page/getpage/ |
| 123 |
[`Page.Ref`]: /methods/page/ref/ |
| 124 |
[`Page.RelRef`]: /methods/page/relref/ |
| 125 |
[`Shortcode.Ref`]: /methods/shortcode/ref/ |
| 126 |
[`Shortcode.RelRef`]: /methods/shortcode/relref/ |
| 127 |
[`Site.GetPage`]: /methods/site/getpage/ |
| 128 |
[`ref`]: /shortcodes/ref/ |
| 129 |
[`relref`]: /shortcodes/relref/ |
| 130 |
[`urls.Ref`]: /functions/urls/ref/ |
| 131 |
[`urls.RelRef`]: /functions/urls/relref/ |