Ronalds Vilciņš

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

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.

← Back to all articles