Hugo’s Templates Lookup
Looking at the extensive list of examples in the docs might be a bit overwhelming to new Hugo users, but it is not that complicated once we break it down.
Layout files are generally in the pattern:
1 2 3 4 5 6
layoutDir / [directory] / filename . [lang] . [outputformat] . suffix
Let’s look at each page .Kind to see how the values of these parts are affected:
home
content/_index.md => example.com/index.html
-
layoutDir: By default, the value islayouts. You can configure this value, but for the examples we will assume this default value is used. -
[directory]: its value can be (with highest priority on top)
a. derived from.Type
- From the docs[^note3]
- So in the case of the homepage, the value will be either what is set on the frontmatter, or"page".b.
"": For the homepage, the layout file can be located just belowlayouts/, ie no[directory].
c."_default": default -
filename: its value can be (with highest priority on top)
a. derived fromlayoutset in the frontmatter.
b."index"
c. derived from the value of.Kind:"home"
d."list": default -
[lang]
a. language code, eg"en","fr", etc. -
[outputformat]
a. the output format name (eg"amp") [^note1] -
suffix
a. the output format suffix (eg"html") [^note1]
Items #1, #4, #5, #6 are the same for .Kinds below.
section
content/posts/_index.md => example.com/posts/
-
[directory]: its value can be
a. derived from.Type[^note3]
b. derived from.Kind:"section"
c."_default": default -
filename: its value can be
a. derived fromlayoutset in the frontmatter.
b. derived from its.Section, eg"posts". Not to be confused with 2.b or 3.c, which is the actual string"section"; this is the value of the top-level directory undercontent. See docs for details.
c. derived from.Kind:"section". Not to be confused with 3.b, this is the actual string"section".
d."list": default
See above for #1, #4, #5, #6
page
content/posts/post-one.md => example.com/posts/post-one/
content/posts/post-one/index.md => example.com/posts/post-one/
content/about.md => example.com/about/
content/about/index.md => example.com/about/
-
[directory]: its value can be
a. derived from.Type[^note3]
b."_default": default -
filename: its value can be
a. derived fromlayoutset in the frontmatter.
b."single": default
See above for #1, #4, #5, #6
taxonomyTerm
content/tags/_index.md => example.com/tags/
[taxonomies]
tag = "tags"
-
[directory]: its value can be
a. derived from.Type[^note3]
b."taxonomy": the actual string"taxonomy"
c. derived from its singular form (eg"tag"for above configuration)
d."_default": default -
filename: its value can be
a. derived fromlayoutset in the frontmatter.
b. derived from<singular> . terms(eg"tag.terms"for above config)
c."terms": the string"terms"
d."list": default
See above for #1, #4, #5, #6
taxonomy
content/tags/foo/_index.md => example.com/tags/foo/
[taxonomies]
tag = "tags"
-
[directory]: its value can be
a. derived from.Type[^note3]
b."taxonomy": the actual string"taxonomy"
c. derived from the singular form of its term (eg"tag"for above configuration)
d."_default": default -
filename: its value can be
a. derived fromlayoutset in the frontmatter.
b. derived from<singular>term (eg"tag"for above config)
c."taxonomy": the string"taxonomy"
d."list": default
See above for #1, #4, #5, #6
Some examples
Head over here for some basic examples. [GitHub]
Minimum example
Hugo can generate your entire site with just two template files:
_default/list.html_default/single.html
Multilingual example
Extra examples
-
Section
lorem/_index.mdhascustomtype.-
What layout will children pages / sections use?
- child pages
lorem/ipsum.mdwill have.Type=lorem[^note2] - child section
lorem/ipsum/_index.mdwill have.Type=lorem[^note2]
- child pages
-
How do I make children pages / sections also have
customtypetype?- Use
cascadein the frontmatter to make the child pages inherit the same value.
Alternatively, set thetypeof each content you wantcustomtypeto apply to.
- Use
-
-
Section
lorem/_index.mdhascustomlayout- child pages
lorem/ipsum.mdwill not usecustomlayout[^note2] - similar to the above, you can
cascadethelayoutfrontmatter so the child pages inherit the same value. Otherwise, you can set the value to each content individually.
- child pages
Notes:
[^note1]: See docs
[^note2]: unless cascaded from parent or otherwise configured.
[^note3]: From the docs: Is value of type if set in front matter, else it the name of the root section (e.g. “blog”). It will always have a value, so if not set, the value is “page”.
Suggestions / corrections welcome!
