DocXtemplater multiple tags on a single line behavior

389 views Asked by At

I am replacing an old program for template-merging with docxtemplater and am trying to recreate the old programs prefix functionality. I want the line removed if all prefixed tags ({$tag}) on that line are undefined.

The issue being that if all the tags on that line are undefined docxtemplater still creates a blank line.

All the examples I have found online tend to reference inverted-sections or rawtags, which both seem to be designed for a single tag per line opposed to multiple tags side by side.

I have looked into using rawtags and writing a custom-parser / nullGetter. However I am still none the wiser to removing the blank line.

I am using:

const options = {
    paragraphLoop: true, 
    linebreaks: false,
    parser: function(tag) {
        return {
            get(scope, context) {
                console.log(tag);
                console.log(scope);
                console.log(context);
                if (tag[0] == "$") {
                    tag = tag.substr(1); // needs to then remove line break
                }
                return scope[tag];
            }
        }
    },
    nullGetter: function nullGetter(part, scopeManager) {
        if (!part.module) {
            return "";
        }
        if (part.module === "rawxml") {
            return "";
        }
        return "";
    }
};
doc = new Docxtemplater(zip, options);

The prefix in the program I am replacing acts as follows:

data: existingtag: EXISTINGTAG

Template.docx:

1 text above
{$existingtag}{$nonexistingtag}
text below

2 text above
{$existingtag}{$existingtag}
text below

3 text above
{$nonexistingtag}{$nonexistingtag}
text below

old program produced (What I want to produce)

1 text above
EXISTINGTAG
text below

2 text above
EXISTINGTAGEXISTINGTAG
text below

3 text above
text below

my docxtemplater produces (extra line in example 3):

1 text above
EXISTINGTAG
text below

2 text above
EXISTINGTAGEXISTINGTAG
text below

3 text above

text below
1

There are 1 answers

0
edi9999 On

I'm the creator of docxtemplater and I don't think that there is a way to do what you want to achieve without taking a lot of time to handle this case.

The problem is that the tags such as :

{xxx}{yyy}

have access only to the text that they are in, but they cannot have any effect ouside of that text, so it is not possible to remove a paragraph conditionnally.

There is one tag that has access to the whole paragraph, that is the raw xml tag, prefixed by a "@", like this :

{@raw}

It is used to add rawXML and if the raw value is an empty string, than that paragraph will be removed.

Edit : I have actually worked on a module back in the time to achieve quite similar functionnality, it is a paid module : https://docxtemplater.com/modules/paragraph-placeholder/