Is there any way to require meta data in DocPad with eco?

106 views Asked by At

At the moment, I'm setting up a new DocPad-project and I explicitly require certain meta data to be set. If not set, I want DocPad to give me some sort of warning, alas I can't find any hints online on how to set it up.

My file default.html.eco prints the title by accessing it from the document, like so:

<%= @document.title %>

However, when it's not set, the output is just empty.

I would like DocPad to warn me about the missing data. Is there a setting I'm missing?

1

There are 1 answers

0
Jesse Bilsten On

You can handle this a number of ways.

1. Conditional

<% if @document.title?: %>
    <%= @document.title %>

2. Postfix Conditional

<%= @document.title if @document.title? %>

3. Console

If you would like to receive a warning you can call console.log("whatever you want here.") and it will be output by node.js into your terminal.

<% if @document.title?: %>
    <%= @document.title %>
<% else: %>
    <% console.log("No document title in " + @document) %>

4. Query/Collection level

If you would like to not render documents without titles you could handle that at the query level in /docpad.coffee

pages: ->
  @getCollection('documents').findAllLive({title: $exists: true}, [pageOrder:1,title:1])

This will check to make sure the document has a title before putting it into the pages collection.

5. Prototype the document object

You could rewrite the document object I suppose as well to overload the attribute calls and have it output a console warning in the event that that attribute doesn't exist, but that'd be much more low level...