Articles on: Tutorials

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

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

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}

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

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

When using showBegin/showEnd, we recommend to use only break-lines shift + enter, learn more on the following article

Updated on: 09/09/2022

Was this article helpful?

Share your feedback

Cancel

Thank you!