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:
To drop all paragraphs conditionally, follow each Carbone expressions with a drop tag on the same paragraph, such as:
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.
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:
To drop the address1 and city if values are empty, it is possible to add expression, such as:
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!