2

I have a Nunjucks shortcode in an Eleventy project. It's added to configuration in eleventy.js with the Eleventy addNunjucksAsyncShortcode method. My shortcode has gotten extremely messy with complex HTML string literals, and I'd like to refactor it into using a Nunjucks template along with the logic in my shortcode Javascript file.

Is there any way I can include and render a Nunjucks template in my async shortcode, or to call a Nunjucks template with the async shortcode config (which I could then use to include methods from my Javascript logic file)? It seems like the Nunjucks object is a singleton, and I get errors trying to use it.

webstackdev
  • 850
  • 15
  • 23
  • 1
    Is it possible to refactor your code to use [Nunjuck macros](https://mozilla.github.io/nunjucks/templating.html#macro) instead? – person_v1.32 May 18 '22 at 22:38
  • 1
    @Mendhak I ended up exploring a couple of approaches, and staying with string literals. You can use Nunjucks to programmatically compile templates, but you lose the data cascade and there's a lot of context 11ty provides the instance to recreate. You can use 11ty programmatically to compile a template but it's the same problem as with using Nunjucks directly. I ended up doing that a lot with Jest for tests, where I can feed dummy data just for the template. I ended up just writing good tests for the shortcodes - not ideal since no code completeion in string literals. – webstackdev Jan 18 '23 at 19:52

0 Answers0