mozilla / nunjucks

A powerful templating engine with inheritance, asynchronous control, and more (jinja2 inspired)
https://mozilla.github.io/nunjucks/
BSD 2-Clause "Simplified" License
8.48k stars 635 forks source link

3.2.3. Custom Async Tag / CallExtensionAsync not working #1416

Open BigJk opened 1 year ago

BigJk commented 1 year ago

It seems like this problem came up a few times in the past. I'm using Version 3.2.3. of Nunjucks and I'm trying to use a custom async tag to fetch some content, unfortunately I can't get it to work even with the bare minimum.

Simple Example:

function TestExtension() {
    this.tags = ['test'];

    this.parse = function (parser, nodes, lexer) {
        let tok = parser.nextToken();

        let args = parser.parseSignature(null, true);
        parser.advanceAfterBlockEnd(tok.value);

        let body = parser.parseUntilBlocks('endtest');
        parser.advanceAfterBlockEnd();

        return new nodes.CallExtensionAsync(this, 'run', args, [body]);
    };

    this.run = function (context, arg1, content, callback) {
        setTimeout(100, () => {
            callback(null, nunjucks.runtime.SafeString('Hello world?'));
        });
    };
}

let env = new nunjucks.Environment();
env.addExtension('TestExtension', new TestExtension());

If I use any of my custom tags using CallExtensionAsync the result of renderString is empty. Is there anything I could be doing wrong or is there any chance to get the problem fixed if it's a bug in the codebase?

ogonkov commented 1 year ago

It seems like this is a bug in Nunjucks, i have unfinished PR with fix somewhere

BigJk commented 1 year ago

Thanks. Using your PR indeed fixes is. Would be nice if this could be merged at some point