Useful File Variables 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.