-
-
Notifications
You must be signed in to change notification settings - Fork 485
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can I process a front matter item like collections processes tags? #1611
Comments
Interesting... Nunjucks or LiquidJS? |
Yeah, custom filter should work: https://github.com/pdehaan/11ty-1611 eleventyConfig.addFilter("toArray", value => {
if (Array.isArray(value)) {
return value;
}
return [value];
}); Or, if you want a one-liner: eleventyConfig.addFilter("toArray", value => Array.isArray(value) ? value : [value]); <p>Convert the tag to an array using custom <code>toArray</code> filter:</p>
<ol>
{% for t in (tag | toArray) %}
<li>{{ t }}</li>
{% endfor %}
</ol> |
You must be using Nunjucks, because LiquidJS seems to "just work"(tm) and return a single "single" value instead of splitting by character. But for future travellers, if you are using liquidjs, you'll need to use the <p>But the <code>toArray</code> filter should still work too:</p>
<ol>
{% assign tags = tag | toArray %}
{% for t in tags %}
<li>{{ t }}</li>
{% endfor %}
</ol> |
I think I've saw Eleventy converting it to an array either way under the hood. |
The filter works perfectly. I would not have thought to write a custom filter, because I'm new to 11ty. So this resolves the immediate issue, and hopefully will help me solve other problems in the future. Thanks for your help. |
11ty collections can process tags whether a post has one
tag: foo
or whether it has more than onetag: [foo, bar]
. I have a front matter variable that might have one value, or more than one, i.e., it might bevar: baz
or it might bevar: [baz, bat]
. Can I use a{% for %}
loop to process it either way? In other words, treatvar: baz
as if var was an array with one item? I tried this:It works when there's more than one value. But when there's only one, in the form
var: baz
, it processes the value as an array of letters (which makes sense, but it's not what I want). If it can't be done, I can just useso not the end of the world. But it'd be nice if I could write it without brackets when there's only 1 item.
The text was updated successfully, but these errors were encountered: