Table of Contents
Easily Log to the Console in Eleventy with Nunjucks
If you're working with Eleventy and Nunjucks, you may sometimes need to log values to the console for debugging purposes. There's a neat trick that allows you to log values more easily from within your Nunjucks templates.
Here's how you can do it:
- Open your Eleventy configuration file.
- Add the following code to the file:
This code adds a new filter called 'log' to Nunjucks. Filters in Nunjucks allow you to modify or transform values in your templates.eleventyConfig.addFilter('log', value => { console.log(value) });
- Now, in any of your Nunjucks templates, you can use this filter like this:
This will log the value of `someVariable` to the console.{{ someVariable | log }}
For example, if you have a variable called `person` with an object containing name and age properties, you can log it like this:
{{ person | log }}
And you'll see the entire `person` object printed in the console. Just pipe the value you want to log through the 'log' filter, and it'll be displayed on the page automatically.
This can be especially handy when you're working with complex data structures or debugging tricky template logic.
So, give it a try and make your debugging experience a little smoother with this simple console.log trick for Eleventy and Nunjucks!
October Featured Tools
From live presentations to social posts, email graphics, GIFs, and videos, Projector helps you create and share visuals for all your channels.
ExploreEasily create high converting product images fast and at scale. No design skills needed! Create store ready, high quality eCommerce images in just 3 steps.
Explore