Check if an object is empty in Nunjucks
<p>So I'm using Nunjucks as the templating engine in my Node.js application.</p>

<p>I have an object we'll call <code>var</code> which may or may not be empty.</p>

<p>When it is empty, if I do
<code>{{ var | dump }}</code> Nunjucks properly shows that it is an empty object, displaying <code>{}</code>.</p>

<p>The problem is, I can't find any way to check if the object is empty using Nunjuck's <code>{% if condition %}</code> statement. I have tried <code>var.length</code>, <code>var | length</code>, <code>var | first</code>, and just plain <code>var</code> for the condition, but none of them work, they all just evaluate to true (or false), regardless of whether or not <code>var</code> is empty. Does anyone know how to solve this?</p>

<p>EDIT: using <code>{% if var | dump != '{}' %}</code> does work, but seems like a really hacky solution...</p>

<p>EDIT 2: I ended up just creating a custom <code>empty</code> filter for objects which does what I need:</p>

<pre><code>env.addFilter('empty', function(object) {
    return Object.keys(object).length === 0;
});
</code></pre>