I am getting stated using DocxTemplater with Node.
I am trying to figure out how to remove an empty line if the element is empty (i.e '' or null) - I can't seem to find a similar case on SO or elsewhere!
My Placeholders on the Docx look like this:
{#data}
{#input}
{Line1}
{Line2}
{Line3}
{/input}
{/data}
Any my Array looks like this:
data: {
input: {
Line1: 'TEXT1',
Line2: '',
Line3: 'TEXT3'
},
}
When I create a document I would like to remove the line in which 'Line2' is as its empty.
So instead of this:
TEXT1
TEXT3
I would like
TEXT1
TEXT3
UPDATE
Changed my code to the below as per the advice, however haven't had any success. Any more pointers? Thanks in advance!
{#data}
{#input}
{#Line1}{Line1}{/Line1}
{#hasLine2}{Line2}{/hasLine2}
{#Line3}{Line3}{/Line3}
{/input}
{/data}
And
data: {
input: {
Line1: 'TEXT1',
Line2: '',
Line3: 'TEXT3',
hasLine2: false
},
}
I'm the creator of docxtemplater
You can do like this :
{#data}
{#input}
{#Line1}
{Line1}
{/Line1}
{#hasLine2}
{Line2}
{/hasLine2}
{#Line3}
{Line3}
{/Line3}
{/input}
{/data}
and you in your code :
new Docxtemplater(zip, {paragraphLoop:true})
With the paragraphLoop option, if the start of the loop and the end of the loop are on separate paragraphs, then you won't get any extra space when your sections are empty.
This is documented here : https://docxtemplater.com/docs/configuration/#paragraphloop