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