While writing validation code for an 11ty shortcode, I
discovered something odd:
if you add a shortcode with no parameter to a nunjucks
template, the engine modifies the call,
adding a parameter set to an empty string.
In other words,
{% foo %}
is equivalent to
{% foo "" %}
.
Liquid templates behave differently.
As far as I can tell
{% foo %}
is equivalent to
{% foo undefined %}
.
This might be related to an eleventy bug from 2019. Hard to tell, because there’s not much info in the issue. But take a look at the commit that resolved it (5cd19f6). There’s a new check for empty arguments inserted at lines 79-81; if there are no arguments, it adds one and sets it to an empty string. There’s also a comment about a nunjucks bug above the inserted code.
I think adding an argument to a function call is the wrong behavior. I’ve filed an issue (3788) with eleventy, and created an eleventy project demonstating the issue.
I’ve also attempted to fix the problem with a
PR (pull request).
The
PR
changes the nunjucks template engine to explicitly
send undefined
when no parameter is specified, mimicking
the behavior of liquid.
However, I don’t know if that will reintroduce the nunjucks
bug to the template engine.
I’m guessing it will, in which case the PR will likely be
rejected.
I’ll update this post if I hear from the eleventy maintainers.