Debian Codegraph @

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

raw
1 ---
2 title: File
3 description: For pages backed by a file, returns file information for the given page.
4 categories: []
5 keywords: []
6 params:
7 functions_and_methods:
8 returnType: hugolib.fileInfo
9 signatures: [PAGE.File]
10 ---
11
12 By default, not all pages are backed by a file, including top-level [section pages](g), [taxonomy pages](g), and [term pages](g). By definition, you cannot retrieve file information when the file does not exist.
13
14 To back one of the pages above with a file, create an `_index.md` file in the corresponding directory. For example:
15
16 ```tree
17 content/
18 └── books/
19 ├── _index.md <-- the top-slevel section page
20 ├── book-1.md
21 └── book-2.md
22 ```
23
24 > [!NOTE]
25 > Code defensively by verifying file existence as shown in the examples below.
26
27 ## Methods
28
29 Use these methods on the `File` object.
30
31 > [!NOTE]
32 > The path separators (slash or backslash) in `Path`, `Dir`, and `Filename` depend on the operating system.
33
34 `BaseFileName`
35 : (`string`) The file name, excluding the extension.
36
37 ```go-html-template
38 {{ with .File }}
39 {{ .BaseFileName }}
40 {{ end }}
41 ```
42
43 `ContentBaseName`
44 : (`string`) If the page is a branch or leaf bundle, the name of the containing directory, else the `TranslationBaseName`.
45
46 ```go-html-template
47 {{ with .File }}
48 {{ .ContentBaseName }}
49 {{ end }}
50 ```
51
52 `Dir`
53 : (`string`) The file path, excluding the file name, relative to the `content` directory.
54
55 ```go-html-template
56 {{ with .File }}
57 {{ .Dir }}
58 {{ end }}
59 ```
60
61 `Ext`
62 : (`string`) The file extension.
63
64 ```go-html-template
65 {{ with .File }}
66 {{ .Ext }}
67 {{ end }}
68 ```
69
70 `Filename`
71 : (`string`) The absolute file path.
72
73 ```go-html-template
74 {{ with .File }}
75 {{ .Filename }}
76 {{ end }}
77 ```
78
79 `IsContentAdapter`
80 : (`bool`) Reports whether the file is a [content adapter][].
81
82 ```go-html-template
83 {{ with .File }}
84 {{ .IsContentAdapter }}
85 {{ end }}
86 ```
87
88 `LogicalName`
89 : (`string`) The file name.
90
91 ```go-html-template
92 {{ with .File }}
93 {{ .LogicalName }}
94 {{ end }}
95 ```
96
97 `Path`
98 : (`string`) The file path, relative to the `content` directory.
99
100 ```go-html-template
101 {{ with .File }}
102 {{ .Path }}
103 {{ end }}
104 ```
105
106 `Section`
107 : (`string`) The name of the top-level section in which the file resides.
108
109 ```go-html-template
110 {{ with .File }}
111 {{ .Section }}
112 {{ end }}
113 ```
114
115 `TranslationBaseName`
116 : (`string`) The file name, excluding the extension and language identifier.
117
118 ```go-html-template
119 {{ with .File }}
120 {{ .TranslationBaseName }}
121 {{ end }}
122 ```
123
124 `UniqueID`
125 : (`string`) The MD5 hash of `.File.Path`.
126
127 ```go-html-template
128 {{ with .File }}
129 {{ .UniqueID }}
130 {{ end }}
131 ```
132
133 ## Examples
134
135 Consider this content structure in a multilingual project:
136
137 ```tree
138 content/
139 ├── news/
140 │ ├── b/
141 │ │ ├── index.de.md <-- leaf bundle
142 │ │ └── index.en.md <-- leaf bundle
143 │ ├── a.de.md <-- regular content
144 │ ├── a.en.md <-- regular content
145 │ ├── _index.de.md <-- branch bundle
146 │ └── _index.en.md <-- branch bundle
147 ├── _index.de.md
148 └── _index.en.md
149 ```
150
151 With the English language site:
152
153 &nbsp;|regular content|leaf bundle|branch bundle
154 :--|:--|:--|:--
155 BaseFileName|a.en|index.en|_index.en
156 ContentBaseName|a|b|news
157 Dir|news/|news/b/|news/
158 Ext|md|md|md
159 Filename|/home/user/...|/home/user/...|/home/user/...
160 IsContentAdapter|false|false|false
161 LogicalName|a.en.md|index.en.md|_index.en.md
162 Path|news/a.en.md|news/b/index.en.md|news/_index.en.md
163 Section|news|news|news
164 TranslationBaseName|a|index|_index
165 UniqueID|15be14b...|186868f...|7d9159d...
166
167 ## Defensive coding
168
169 Some of the pages on a site may not be backed by a file. For example:
170
171 - Top-level section pages
172 - Taxonomy pages
173 - Term pages
174
175 Without a backing file, Hugo will throw an error if you attempt to access a `.File` property. To code defensively, first check for file existence:
176
177 ```go-html-template
178 {{ with .File }}
179 {{ .ContentBaseName }}
180 {{ end }}
181 ```
182
183 [content adapter]: /content-management/content-adapters/