11ty / eleventy

A simpler site generator. Transforms a directory of templates (of varying types) into HTML.
https://www.11ty.dev/
MIT License
17.19k stars 494 forks source link

Idea: ability to mark a frontmatter attribute as required #269

Closed edwardhorsford closed 6 years ago

edwardhorsford commented 6 years ago

It would be handy if I could mark front matter as required. Eleventy would then fail if processing a page and that front matter were not set.

Some of my pages require bits of front matter to be present for things to work correctly. I've made a 'blank template' to duplicate when I create new pages which has placeholders for these. But if I add new attributes it could be easy to forget to add them to old templates. It's also easy to forget to update the placeholder content. I'd prefer it if there was a more obvious fail.

How it could work

Perhaps by setting frontmatter keys ending with * and a blank attribute. You could set this globally / in projects.json or on the file itself.

For example: for posts stored in /posts/

Post.json:

---
  "layout" : "post-template.njk",
  "permalink" : "/blog/{{page.fileSlug}}/",
  "primaryImage*" : ""
---

A-post-page.md:

---
  primaryImage:
---

Results in:

Error: required front matter not set on template 'A-post-page.md'

One challenge with the above is that right now I think the key in a-post-page.md would overwrite the post.json key. In my case I'd like the higher level one to essentially set a flag that subsequent ones need to follow.


Would this be useful to others?

zachleat commented 6 years ago

I like this! I wonder if it could be more generalized to use some kind of data schema a la https://json-schema.org/

zachleat commented 6 years ago

This repository is now using lodash style issue management for enhancements. This means enhancement issues will now be closed instead of leaving them open.

The enhancement backlog can be found here: https://github.com/11ty/eleventy/issues?utf8=%E2%9C%93&q=label%3Aneeds-votes+sort%3Areactions-%2B1-desc+

Don’t forget to upvote the top comment with 👍!

edwardhorsford commented 9 months ago

Not a full solution, but I partially worked around this by using eleventyComputed:

title: (data) => {
    if (!data.title){
      utils.warnQuiet(`eleventyComputed.js: item missing title. Page: ${data?.page?.inputPath}`)
    }
    return data.title
  },
  // Check that photoblog pages have a date
  date: (data) => {
    if (data.tags && data.tags.includes('_photoblog') && !data.date && !data.pagination){
      utils.warnQuiet(`eleventyComputed.js: item missing date. Page: ${data?.page?.inputPath}`)
    }
    return data.date
  },