Getting a content file's name and path in Hugo
Hugo’s .File variable exposes information about the content file behind a page. It is handy when a template needs the filename, directory, language, or a stable identifier derived from the path.
The important limitation is that .File is only available when the page has a content file attached to it. Do not assume every page object has one.
Fields on .File
.File.Pathis the path relative to the content directory, such asarticles/post.md..File.LogicalNameis the content filename, such aspost.md..File.TranslationBaseNameis the filename without its extension, such aspost..File.ContentBaseNameis the translation base name, or the directory name for a leaf bundle..File.BaseFileNameis the filename without its extension, such aspost..File.Extis the extension, such asmd..File.Langis the language when Hugo’s multilingual features are enabled, such asen..File.Diris the content file’s relative directory, such asarticles/abc/def/..File.UniqueIDis an MD5 checksum of the content file’s path.
For most templates, .File.Path and .File.Dir are the useful starting points. Use .File only when the template genuinely cares about storage on disk; page-level values are usually a better fit for content semantics.
Enjoyed reading this?
If you found this article helpful, consider supporting my writing. Every little bit helps keep this blog going.
Related articles
- Customizing Hugo’s RSS Feed
Adjust Hugo RSS titles, descriptions, content, and section filtering while keeping the generated feed valid and easy to discover.
- Organizing Hugo Content With Tags and Categories
Organize Hugo content with a small, deliberate set of tags, categories, or custom taxonomies that lead to useful archive pages.
- A More Useful 404 Page in Hugo
Turn Hugo's 404 page into a useful route back to the homepage, search, or content visitors may have wanted.