javascripttypescriptdocxtemplater

Is there a way to ignore loop and conditional placeholders that have no data with docxtemplater?


I'm building a backend around docxtemplater. It could occur that someone provides a template with placeholders that don't occur in the data. I made the following nullGetter so it leaves placeholders as they are.

private static nullGetter(part: Part) {
        if (!part.module) return `${this.startDelimiter}${part.value}${this.endDelimiter}`;
        return '';
    }

The problem is that this doenst work for conditional placeholders like <#condition> Some Text . If there is no data for condition it just leaves the line blanc. I have the same problem when i want to loop tablerows. If the loop doesn't occur in the data it just removes the table. I want it to return the document with the placeholders still in the text so the user can eassily see which placeholders are forgotten.

I tried making a custom module to handle this. But i'm not capable of doing this.


Solution

  • Finally found a solution by adjusting a combination of the values of part.subparsed.value in the custom parser for text with type content and doing this for type content in the nullgetter. Otherwise the delimiters were escaped incorrectly because I use < and > as delimiters. I escaped these because otherwise the file was not valid. But the parser escape < and > to &lt; and &gt;.