Search code examples
docpad

How to know in which environment we are from a collection definition?


I would like something like the following pseudo-code :

collections:
    posts: function (database) {
        var match = env === 'debug' {layout: 'post', published: true} ? {layout: 'post'};
        return database.findAllLive(match, {date:-1});
    }

Solution

  • You'll want to use this.docpad.getEnvironments() (JavaScript) or @docpad.getEnvironments() (coffeescript) which returns an array on environments we are currently using. Based on your code intentions you then want to check if debug is inside of it.

    In JavaScript as you're using:

    collections: {
        posts: function (database) {
            var match = this.docpad.getEnvironments().indexOf('debug') != -1 then {layout: 'post', published: true} ? {layout: 'post'};
            return database.findAllLive(match, {date:-1});
        }
    }
    

    In the standard CoffeeScript

    collections:
        posts: (database) ->
            match =
                if 'debug' in @docpad.getEnvironments()
                    {layout: 'post', published: true}
                else
                    {layout: 'post'}
            database.findAllLive(match, {date:-1})
    

    It's also worth mentioning, that you may also be interested in docpad.getDebugging() which will return a boolean reflecting the inclusion of the -d --debug CLI flag.