open-xml-templating/docxtemplater

custom filters in segment not resolved with renderAsync

MarinBozinovic opened this issue · 1 comments

There seems to be an issue with resolving custom filters within segments when using renderAsync

Environment

  • Version of docxtemplater : 3.37.12
  • Used docxtemplater-modules : docxtemplater-subtemplate-module 3.14.3
  • Runner : Node.JS

How to reproduce my problem :

My template is the following :

image

With the following parser :

const myParser = (tag) => {
    const _tag = tag
        .replace(/^\.$/, 'this')
        .replace(/(’|‘)/g, '\'')
        .replace(/(“|”)/g, '"');
    const expr = expressions.compile(_tag);
    return {
        get: async (scope, data) => {
            const isSubsection =
                data &&
                data.meta &&
                data.meta.part &&
                data.meta.part.module === 'pro-xml-templating/subsection-module';

            const scopeList = data.scopeList;
            const num = data.num;
            let obj = {};
            for (let i = 0, len = num + 1; i < len; i++) {
                obj = Object.assign(obj, scopeList[I]);
            }

            let value;
            if (_tag === '.') {
                value = scope;
            } else {
                value = expr(scope, obj);
            }

            if (value && (isSubsection)) {
                await value.renderAsync(scope);
            }
            return value;
        },
    };
};

where the custom filter is:

filters.fromContext = (value, contextKey) => {
	return 'GOOD';
};

Expected (works well with render):
image

Result:
image

It might actually have something to do with your parser function.

Could you try with the following instead :

const expressionParser = require("docxtemplater/expressions.js");

expressionParser.filters.fromContext = /* 
...
...
*/


const myParser = (tag) => {
    const _tag = tag
        .replace(/^\.$/, 'this')
        .replace(/(’|‘)/g, '\'')
        .replace(/(“|”)/g, '"');

	const myGet = expressionParser.apply(expressionParser, [_tag]).get;
    return {
        get: async (scope, data) => {
            const isSubsection =
                data &&
                data.meta &&
                data.meta.part &&
                data.meta.part.module === 'pro-xml-templating/subsection-module';

            let value = myGet.apply(myGet, arguments);

            if (value && (isSubsection)) {
                await value.renderAsync(scope);
            }
            return value;
        },
    };
};

If that doesn't work, please send me your full code, template as docx format, and the data you're using. Please prefer using email support (click on "Contact Us" on https://docxtemplater.com/).