Skip to content
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

Using Liquid filters in Nunjucks templates #989

Open
pdehaan opened this issue Mar 2, 2020 · 2 comments
Open

Using Liquid filters in Nunjucks templates #989

pdehaan opened this issue Mar 2, 2020 · 2 comments

Comments

@pdehaan
Copy link
Contributor

pdehaan commented Mar 2, 2020

"Not really a question, more of a comment(tm)…"

[email protected] 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.

Probably Almost 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.

// Where liquidjs is v9.9.0+, per https://github.com/harttle/liquidjs/issues/188
const { Liquid } = require("liquidjs");

// console.log(require("liquidjs/package.json").version); // 9.9.0

const liquidOptions = {
  extname: ".liquid",
  strictFilters: true,
  root: ["_includes"]
};
const liquidEngine = new Liquid(liquidOptions);
const liquidFilters = liquidEngine.filters.impls;

module.exports = function(eleventyConfig) {
  // eleventyConfig.setLibrary("liquid", liquidEngine);

  eleventyConfig.addNunjucksFilter("uniq", liquidFilters.uniq);

  return {
    dir: {
      input: "src",
      output: "www"
    }
  };
};

And now, in your Nunjucks templates, behold!

---
# src/test.njk
title: Liquid filters in Nunjucks templates
---

{{ [1, 2, 4, 4, 4, 3, 2, 1, 7, 3] | uniq | sort }}
{# Output: 1,2,3,4,7 #}

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.

@pdehaan
Copy link
Contributor Author

pdehaan commented Mar 3, 2020

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 }}.

@zachleat
Copy link
Member

zachleat commented Dec 5, 2022

I think the Render plugin solves this use case in a safer/less brittle way—what do you think about resolving this old one?

https://www.11ty.dev/docs/plugins/render/

@zachleat zachleat added the feature: 🪢 render plugin The Render plugin label Dec 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants