Homepage content from content/home.md

Converting a small static site from WP to Hugo, and loving it so far :smiley:

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?

Any pointers?

There was an earlier thread about this same question.

Currently it’s not as straightforward as we would like, but it’s very doable.

I would just access that page directly and pass the value into a {{ template “_default/single.md” .ContentPageSetAbove }}.

See this thread.

Cool, thanks! Something like this seems to work for the time being:

{{ range where .Data.Pages "Title" "Home" }}
  {{ template "_default/single.html" . }}
{{ end }}

Thank you for Hugo - it’s way cool.

I’m trying to figure out how to directly access a page and you reference that in your comment.

Would you mind showing how or pointing me to the docs on it? Thanks! I’ve searched quite a bit which is how I found this thread.

Thanks again!


Doesn’t the example in my comment work for you?

Yes, it did, but was thinking there might be an even more direct way to access a page. Thank you.

@scrogatl more direct or just "cheating?? Put this in layouts/ index.html and home.md in root of /content That was my solution.

<script type="text/javascript">
window.location = "{{ .Site.BaseUrl }}/home"

for those up against this and simlar problem of using markdown in layout files (e.g. the root index.html or partials) I have started this post.

Is it possible to use the layout defined by index.html (as opposed to another layout) and render content from a single markdown page?

EDIT: It’s as simple as,

{{ range where .Data.Pages "Title" "Home" }}
    {{ .Content }}
{{ end }}

Is there a more elegant way to accomplish this?

1 Like

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.

Just as an additional note, based on this thread I managed to get the contents of any posts tagged as _home for the home page, with this code:

{{ range .Data.Pages }}
  {{ if in .Params.tags "_home" }}
    <h1>{{ .Title }}</h1>

    {{ .Content }}
  {{ end }}
{{ end }}

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.

Nearing something okish.

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.

No, you don’t seem to have grasped it. It shouldn’t be too hard. See at the bottom here:


If not, I suggest you wait until it gets released.

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.

But never mind will try to figure it out myself.

That is true.

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:


{{ range sort (where .Site.Pages "Section" "_partials" ) }}
  {{ if in .RelPermalink "/_partials/hero/"}}
    {{ partial "hero.html" . }}
  {{ end }}
{{ end }}


  <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>


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.

And something like content/_index/hero.md?