Mark Llobrera

Eleventy: The log Universal Filter

I’ve written about debugging in Eleventy before, in the context of .js files. But I’ve always wondered about the best ways to debug within Nunjucks templates.

I knew about the Nunjucks dump filter, but I was searching for something to write to the console while doing a --dryrun build of my site. So I wrote my own universal filter:

// Logging within Nunjucks files filter
eleventyConfig.addNunjucksFilter("njkLog", function (message) {
debug('njkLog: ', message);

Except, uh, this already exists in Eleventy as the universal filter log. So within a Nunjucks/Liquid/Handlebars template you could do:

{{ "This is a message" | log }}

or pass it a variable:

{{ myCollection | log }}