[SOLVED] Get Draft Front Matter without Generating Page?

Hey, I was wondering if there is a way to get content from pages where draft = true without actually generating the page in the build.

Use case:

I have a directory of pages each with a .Title and some are ready to be published, others not. However, I would like to list all the pages linking to the ones where draft = false and just putting the .Title when draft = true. This would prevent people from navigating to the (still in draft) page if they can figure out the URL – which should be pretty simple.

Some code (not tested - but in theory it should work (I might be missing some .Params or something, but the intent should be clear)

// Directory
...
β”‚
β”œβ”€β”€ content/
β”‚   β”œβ”€β”€ post-1.md    // title = "Hello Hugo" author="Mr. X" draft = false
β”‚   β”œβ”€β”€ post-2.md    // title = "Another Title" author="Mr. Y" draft = true
β”‚   β”œβ”€β”€ post-3.md    // title = "Yet Another Title" author="Mrs. Z" draft = false
β”‚   └── _index.md    // draft = false
...

This is really where the question is (I am 100% sure this is not correct :stuck_out_tongue:)

// layout/_default/list.html

{{ range .Pages }}
    <div>
    {{ if eq .draft true }}
        {{ .Title }}
    {{ else }}
        <a href="{{ .Permalink }}">{{ .Title }}</a>
    {{ end }}
    <span>By: {{ .author }}</span>
    </div>
{{ end }}

// Desired Output Directory

...
β”‚
β”œβ”€β”€ pages/
β”‚   β”œβ”€β”€ post-1.html
β”‚   β”œβ”€β”€ post-3.html
β”‚   └── index.html
...
// Desired Output in /pages/index.html

<div>
     <a hrfe=".../posts/post-1.html">Hello Hugo</a>
     <span>By: Mr. X</span>
</div>
<div>
     <span>Another Title</span>
     <span>By: Mr. Y</span>
</div>
<div>
    <a hrfe=".../posts/post-1.html">Yet Another Title</a>
     <span>By: Mrs. Z</span>
</div>

I think that this could be very useful for book and documentation websites which want to indicate that particular content is in the works, but is not ready for public consumption. I would be interested if people have other use cases.

-hs

I don’t think that’s going to work that way. Drafts are either published (ie using --buildDrafts), or not. This means when it’s published, it has a .Permalink, and therefore browse-able.

You can get the draft state of a .Page with .Params.draft, but again, the above point stands.

You could look into headless bundles: https://gohugo.io/content-management/page-bundles/#headless-bundle

Basically, have a headless bundle to contain all your non-browse-able content. These will not have a .Permalink, but they will also not be part of .Site.Pages. You can use .Resources methods to get their content etc.: https://gohugo.io/content-management/page-resources/

Once you are ready to have them published, just move out of the headless bundle.

Hmmm, interesting – that could work in another context. I’m building this for Forestry, so I don’t think that it would fit within their [current] editing workflow.

I feel that this could be fairly useful and since Hugo is already going through all the pages to check if draft = true or not, I could imagine that it could easily store all front matter / content, and then filters out all the drafts when it writes to public/

readFile + transform.Unmarshal is the way to go (for now)
(i’m not sure if .Content / .RawContent is available, but at least for my application right now, that is not necessary)

1 Like