Search code examples
jekyllliquid

Want to cause a build error for bad input


Here is a my blog post:

---
last-medical-review: 2022-12-03
---
Some post

And then there is a layout that reads this front matter and renders the page. All pages need to have the medical review date specified. (And there are plenty of other rules.)

Is it possible to trigger some error conditionally if this front matter element is missing? I am hoping the build can fail as a fatal error.


Solution

  • Assuming there is a layout somewhere that does something like this:

    <p>Last medical review: {{ page.last-medical-review }}</p>
    

    then you can add the following to your _config.yml:

    liquid:
      strict_variables: true
    

    which will result in a build error like this if last-medical-review isn't defined in the front matter:

      Liquid Exception: Liquid error (line 7): undefined variable minima in /tmp/jekylltest/_layouts/post.html
                        ------------------------------------------------
          Jekyll 4.3.1   Please append `--trace` to the `serve` command 
                         for any additional information or backtrace. 
                        ------------------------------------------------