Content in data files

Hi all,
I’ve been reading posts about adding multiple markdown files to a single page.
For example:

Etc.
The reason for wanting this is usually for home pages or landing pages and the like.

What about if we allow markdown content in data files? Then we can iterate over data files like we normally do for everything else, we can grab the content and markdownify it, but the files are not rendered elsewhere in the site (rather hacky).

This seems an easy fix and much more elegant. WDYT?

P.s. this assumes that I am correct in thinking that you can’t have content in data files.

Phil

I suspect the discussions you link to are rather old and precedes the “everything is now a Page” Hugo release (Hugo 0.18?).

Home page and sections etc. can now have a content file attached to it – with both content and meta data (params etc.).

See:

Data files can also have content (see the markdownify template func), but you have to know where to look for it …

Thanks Bep. Can you point me to where I can find the content in the data files? AFAICS, there is no .Content variable, or anything obvious.

And yes, I understand that homepages can have content. The issue I’m trying to solve is to use multiple files as content for a single page.

E.g. inside my index.html template:

{{ range sort (.Site.Data.home) "weight" "asc" }}
<section>
  <h1>{{ .Title }}</h1>
  <h1>{{ .Content }}</h1>
</section>
{{end}}

I would have several sections from individual datafiles.

Thanks.

So, given /data/home.toml:

content1 = """

##This is a header for content 1

And some more **content**...
"""

content2 = """

##This is a header for content 2

And some more content...
"""

And then in your template:

{{ .Site.Data.home.content1 | markdownify }}

Note:

  1. The markdownify func behaves “better” in the next Hugo (aka current development version)
  2. There are also several open issues/plan to improve this “multiple content for one page” scenario. It will happen, eventually.

Yeah ok, that’s what I’m doing at the moment. I would have thought it made more sense to do this in the datafile (which would be more consistent with “normal” pages):

---
title: Hi there
---
This is my _markdown_ content.

Which then begs the question, why do we need data files when we could just use pages (with an option to prevent them being published maybe?). But I’ll leave that for another day.

Thanks!

A content file can only have 1 .Content, which, if I understand you correctly, is the foundation of your question? So if you want to put several “content sections” in the home page you can use a data file to pull in content for those sections.

Yes, a content file can only have one .Content. That is fine. But I don’t want to use content pages (like others have) to construct single pages (like a home page) because I don’t want to have to faff around stopping the content pages creating html pages in their own right.

Hence, data files are a better solution for me, but I just wish I could add .Content to data files without having to write them in the front-matter.

There’s an issue open on this idea:

1 Like