Hide a line if an element is empty
Hide a line if an element is empty
When creating a template, some value may be optional and it creates an empty line the generated document.
It is possible to remove a line based on condition, here is an example using the following JSON dataset:
{
"firstname": "John",
"lastname": "Wick",
"address1": "221B Baker Street",
"address2": "",
"city": "London"
}
First solution
Use the drop formatter to delete the paragraph if the value is empty.
The expression {d.address2:ifEM:drop(p)} removes the address2 paragraph, such as:
![DOCX template edited with MS Word](https://storage.crisp.chat/users/helpdesk/website/f355e91d7615e000/screenshot-2022-09-09-at-18342_zp4smi.png)
To drop all paragraphs conditionally, follow each Carbone expressions with a drop tag on the same paragraph, such as:
![DOCX template edited with MS Word](https://storage.crisp.chat/users/helpdesk/website/f355e91d7615e000/screenshot-2022-09-09-at-18352_1p6nnjn.png)
Learn more about :drop formatter on the documentation
Second Solution
Use the hideBegin/hideEnd formatters to remove the paragraph if the value is empty.
The following template removes the address2 paragraph. Break-lines must be used between the hideBegin and hideEnd.
![ODT Template with a condition wrapping the line {d.address2}](https://storage.crisp.chat/users/helpdesk/website/f355e91d7615e000/screenshot-2022-02-15-at-16011_1kfl70m.png)
In order to drop the line, the expression {d.address2:ifEM:hideBegin} is placed just before the value and the {d.address2:hideEnd} just after the break-line.
Here is the result:
![Generated PDF document](https://storage.crisp.chat/users/helpdesk/website/f355e91d7615e000/screenshot-2022-02-15-at-15523_y23bgo.png)
To drop the address1 and city if values are empty, it is possible to add expression, such as:
![ODT Template - Optional lines: address1 address2 city](https://storage.crisp.chat/users/helpdesk/website/f355e91d7615e000/screenshot-2022-02-15-at-15592_17ybwz7.png)
When using showBegin/showEnd, we recommend to use only break-lines shift + enter, learn more on the following article
Updated on: 09/09/2022
Thank you!