Debian Codegraph @

docs/content/en/methods/page/Path.md

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