11ty / eleventy

A simpler site generator. Transforms a directory of templates (of varying types) into HTML.

Home Page:https://www.11ty.dev/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Nunjucks: `{% setAsync %}` to handle Asynchronous Shortcodes nested inside.

zachleat opened this issue · comments

Failing test case:

{% set myVar %}
{% asyncShortcode %}
test content
{% endasyncShortcode %}
{% endset %}

Discovered via #2100 (comment)

Local PR that has some progress on a similar issue: #1749

Nunjucks Related Issues:
mozilla/nunjucks#815
mozilla/nunjucks#1362
mozilla/nunjucks#1363

I tested mozilla/nunjucks#1357 locally and it did not resolve the issue.

Related: macros

Note that the macro docs warn about using async stuff: https://mozilla.github.io/nunjucks/templating.html#macro

Important note: If you are using the asynchronous API, please be aware that you cannot do anything asynchronous inside macros. This is because macros are called like normal functions. In the future we may have a way to call a function asynchronously. If you do this now, the behavior is undefined.

This repo has a setAsync custom tag that I looked into but I don’t think it solved the issue: https://github.com/namshi/nunjucks-setasync

Do you need any more from my PR for this? Happy to do more work on it

I merged it! It is amazing, thank you @CodeFoodPixels!

1.0.0-beta.9 will have a new setAsync Nunjucks paired shortcode.

The first argument is a string.

{% setAsync "liquidOutput" %}
{% renderTemplate "md" %}
# Title
{% endrenderTemplate %}
{% endsetAsync %}