Search code examples
hugohugo-content-organization

How does the routing in Hugo templates work?


I am trying to design my own theme starting from blank. The test site builds fine and is indeed empty but functional.

I however fail to understand how the routine in themes work. Specifically, my problem is that I do not see where the main skeleton for the final web page is - that one that will build <header>, <body>, etc. I expected to find there the logic of switching between views: what to parse when looking at the home page, what at a blog entry, what at a standalone page, etc.

To take the example of this starter theme, I see https://github.com/Vimux/blank/tree/master/layouts/index.html, but it is just the definition of one of the main blocks. There are other such definitions in https://github.com/Vimux/blank/tree/master/layouts/_default for instance.

Where do I code the logic/decision to use a specific template file? (one of the several with the definition of main)

I expect that some "routing" (not dynamic one - a routing that is then compiled into a set of pages with links to other pages) must be defined somewhere where you say that the page / is generated from <something> and a page /blog/hello-world is generated from <something else>.


Solution

  • Great question.

    The answer is rather suprising... especially when you are coming from Jekyll or a similar SSG.

    The logic/decision to use a specific template file is implicit.

    This means that a file in the root of the 'content' directory named '_index.md' uses the 'layouts/index.html' template, while a page with another name in the same directory uses the 'layouts/pages/single.html' template. When that layout file is not found/does not exist, it will fall back to the 'layouts/_default/single.html' template.

    More on this can be found at https://gohugo.io/templates/lookup-order/.