2019-10-21 04:22:28 -04:00
|
|
|
---
|
2023-07-29 05:15:54 -04:00
|
|
|
title: File variables
|
2023-05-22 10:43:12 -04:00
|
|
|
description: "Use File variables to access file-related values for each page that is backed by a file."
|
2023-07-29 05:15:54 -04:00
|
|
|
categories: [variables and parameters]
|
2019-10-21 04:22:28 -04:00
|
|
|
keywords: [files]
|
|
|
|
menu:
|
|
|
|
docs:
|
2023-05-22 10:43:12 -04:00
|
|
|
parent: variables
|
2019-10-21 04:22:28 -04:00
|
|
|
weight: 40
|
2023-05-22 10:43:12 -04:00
|
|
|
toc: true
|
2019-10-21 04:22:28 -04:00
|
|
|
weight: 40
|
|
|
|
aliases: [/variables/file-variables/]
|
|
|
|
---
|
2023-05-22 10:43:12 -04:00
|
|
|
## Variables
|
2019-10-21 04:22:28 -04:00
|
|
|
|
2023-05-22 10:43:12 -04:00
|
|
|
{{% note %}}
|
|
|
|
The path separators (slash or backslash) in `.File.Path`, `.File.Dir`, and `.File.Filename` depend on the operating system.
|
2019-10-21 04:22:28 -04:00
|
|
|
{{% /note %}}
|
|
|
|
|
|
|
|
.File.Path
|
2023-05-22 10:43:12 -04:00
|
|
|
: (`string`) The file path, relative to the `content` directory.
|
2019-10-21 04:22:28 -04:00
|
|
|
|
2023-05-22 10:43:12 -04:00
|
|
|
.File.Dir
|
|
|
|
: (`string`) The file path, excluding the file name, relative to the `content` directory.
|
2019-10-21 04:22:28 -04:00
|
|
|
|
2023-05-22 10:43:12 -04:00
|
|
|
.File.LogicalName
|
|
|
|
: (`string`) The file name.
|
2020-06-16 08:18:51 -04:00
|
|
|
|
2019-10-21 04:22:28 -04:00
|
|
|
.File.BaseFileName
|
2023-05-22 10:43:12 -04:00
|
|
|
: (`string`) The file name, excluding the extension.
|
|
|
|
|
|
|
|
.File.TranslationBaseName
|
|
|
|
: (`string`) The file name, excluding the extension and language identifier.
|
2019-10-21 04:22:28 -04:00
|
|
|
|
|
|
|
.File.Ext
|
2023-05-22 10:43:12 -04:00
|
|
|
: (`string`) The file extension.
|
2019-10-21 04:22:28 -04:00
|
|
|
|
|
|
|
.File.Lang
|
2023-05-22 10:43:12 -04:00
|
|
|
: (`string`) The language associated with the given file.
|
2019-10-21 04:22:28 -04:00
|
|
|
|
2023-05-22 10:43:12 -04:00
|
|
|
|
|
|
|
.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.
|
2019-10-21 04:22:28 -04:00
|
|
|
|
2020-05-06 06:12:21 -04:00
|
|
|
.File.UniqueID
|
2023-05-22 10:43:12 -04:00
|
|
|
: (`string`) The MD5 hash of `.File.Path`.
|
|
|
|
|
|
|
|
## 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:
|
2020-05-06 06:12:21 -04:00
|
|
|
|
2023-05-22 10:43:12 -04:00
|
|
|
```go-html-template
|
|
|
|
{{ with .File }}
|
|
|
|
{{ .ContentBaseName }}
|
|
|
|
{{ end }}
|
|
|
|
```
|