0

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?

Patrick
  • 397
  • 3
  • 3
  • I don't there is a plugin for that as I was also searching for it. I checked in DocPad API and I think it is possible to create such a plugin, but I just didn't have time to do it yet. – Lukasz Gornicki Oct 24 '14 at 15:53

1 Answers1

1

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...

Jesse Bilsten
  • 79
  • 1
  • 3