Converting a small static site from WP to Hugo, and loving it so far
I ran into a small problem when doing the homepage (first page) though. I would like to grab its content from content/home.md, and use the standard layouts/_default/single.md template. I canât really figure out the proper way to do this though?
Currently, no. There are variations of this, but nothing more elegant.
This has been discussed before, but I guess noone has thought about it too hard. But adding a âfindOnePageâ helper func wouldnât make it all that more elegant, as It would have to do the ranging behind the scenes.
My variation on this is to add the home page content to a directory e.g. content/home/index.md then you can filter on Type (matching the directory name)
{{range where .Data.Pages "Type" "home"}}
{{ partial "header" . }}
{{.Content }}
{{ partial "footer" . }}
{{ end }}
How did you solve the issue with partial content files being rendered as single files also?
Currently I am using something similar with:
{{ range where .Site.Pages "Section" "_index" }}
{{ if eq .RelPermalink "/_index/hero/" }}
{{ partial "hero.html" . }}
{{ end }}
{{ end }}
But all content under _index is always rendered as a page. Even moving to having no _default type does render an empty page. Should render 404 instead in my view.
This isnât solvable in Hugo <= 0.17, but has built-in support in the upcoming Hugo 0.18. Will be released for Christmas, but you can get it now by using one of the source alternatives.
Any docs on the built-in support for 0.18? Would love to get it ready, but digging into the docs in the repo didnât make the approach needed clear. Any help would be appreciated. Thanks.
How would one render the partial âservices.htmlâ only when the page _index_services.md is called?
As pages canât be identified via their permalink is the only option to use the linktitle or some sort of id set within the content file? I am not sure, if moving a theme used ID to the content section makes sense so.
Any other way to filter?
{{ range sort (where .Site.Pages "Section" "home" ) }}
{{ if .IsNode }}
{{ if **something to identify that we are in the context of _index_services.md ** }}
{{ partial "services.html" . }}
{{ end }}
{{ end }}
{{ end }}
Also played around with:
{{ .Site.GetPage "section" "404" }}
Which only returns the first index found. So canât be used for multiple index files.
The linked document seems to indicate that one can only add one content file per section/home page.
No mention on how to use content files per partial used within a home page for example.
Alright then I misinterpreted the PR. A few of the linked and closed issues were also talking about one page sites.
For anyone interested. This is my current workaround:
layouts/index.html:
{{ range sort (where .Site.Pages "Section" "_partials" ) }}
{{ if in .RelPermalink "/_partials/hero/"}}
{{ partial "hero.html" . }}
{{ end }}
{{ end }}
layout/partials/hero.html:
<header>
<div class="container">
<div class="intro-text">
<div class="intro-lead-in">{{ with .Title }}{{ . }}{{ end }}</div>
<div class="intro-heading">{{ with .Params.subtitle }}{{ . }}{{ end }}</div>
{{ with .Params.button }}
<a href="#anchor" class="page-scroll btn btn-xl">{{ . }}</a>
{{ end }}
<p>{{ .Content }}</p>
</div>
</div>
</header>
Content: content/_partials/hero.md
Unfortunately this duplicate the content also as page using the _default layout. Using layout file names such as _index_hero.md would hack around that, but the way I see this working is only by introducing a user editable ID/linktitle to check against or some other way. Editable theme important data doesnât sound perfect. If someone has a better approach, ping me.