Open pdehaan opened 4 years ago
And for completeness, apparently you can access Nunjucks filters via require("nunjucks/src/filters")
and then use .addLiquidFilter()
as per usual:
const nunjucksFilters = require("nunjucks/src/filters");
module.exports = function(eleventyConfig) {
eleventyConfig.addLiquidFilter("random", nunjucksFilters.random);
return {
dir: {
input: "src",
output: "www"
}
};
};
# src/test.liquid
title: Nunjucks filters in Liquid templates
Your random number is {{ (0..20) | random }}.
I think the Render plugin solves this use case in a safer/less brittle way—what do you think about resolving this old one?
liquidjs@9.9.0 was released today and they now expose their filter implementations, so it looks like you can now use
eleventyConfig.addNunjucksFilter()
to add liquidjs filter implementations.ProbablyAlmost certainly not a good idea, and probably not worth documenting, but a neat little hack if you're a Nunjucks user who is envious of some of them sweet, sweet filters.And now, in your Nunjucks templates, behold!
Next I'll have to see if Nunjucks exposes filters in a way that they could be consumed by Liquid templates.
It's still a pretty fragile workflow, and not sure it'd be any better if somebody could repackage this into an Eleventy plugin where liquidjs filters get shimmed into nunjucks without all this hassle, but... I still figure this is a hacky solution and probably not worth the hassle of building something that would need to be maintained for more than 1 user.