Design
Inline Conditions
How to conditionally print a text?
COMMUNITY FEATURE
Available for:
Carbone Cloud
Carbone On-premise
Embedded Carbone JS
v3.0+
:show(text) / :elseShow(text)
If the condition is true, :show prints the text between parentheses and the text is passed to the next formatter, bypassing all conditional formatters (ifEQ, show, ...).
Here are some useful examples
{
"val2" : 2,
"val5" : 5
}
val2 = {d.val2:ifGT(3):show('high')}val5 = {d.val5:ifGT(3):show('high')}val2 = {d.val2:ifGT(3):show('high'):elseShow('low')}
val2 = 2val5 = highval2 = low
Switch case
If the condition is not true, the value is passed to the next formatter. This allows you to easily create a switch-case structure like this:
{
"val1" : 1,
"val2" : 2,
"val3" : 3
}
val1 = {d.val1:ifEQ(1):show(A):ifEQ(2):show(B):elseShow(C)}val2 = {d.val2:ifEQ(1):show(A):ifEQ(2):show(B):elseShow(C)}val3 = {d.val3:ifEQ(1):show(A):ifEQ(2):show(B):elseShow(C)}
val1 = Aval2 = Bval3 = C
Syntax alternative: {d.val1:ifEQ(1):show(A):or(d.val1):ifEQ(2):show(B):elseShow(C)}
Get inspired by one of our real-life examples: Check-out or EditorJS JSON output from HTML WYSIWYG Tool to PDF document
With array filters
Array filter can be used to show or hide a Carbone tag. Carbone hides only one tag if the condition is not repeated for all tags in the loop.
[
{ "name": "John", "age": 20 },
{ "name": "Eva", "age": 18 },
{ "name": "Bob", "age": 25 },
{ "name": "Charly", "age": 30 }
]
People{d[i].name}: {d[i, age > 19].age}{d[i+1].name}
PeopleJohn: 20Eva: Bob: 25Charly: 30
Get inspired by one of our real-life examples: Planning with subtotals
:ifEmpty(textIfTrue, continueOnSuccess)
v0.12.5+
Test if data is empty (null, undefined, [], {}, ...).
The new formatter :ifEM:show should be used instead of this one.
| Params | Description | Type |
|---|---|---|
| textIfTrue | textIfTrue to print if JSON data is empty | String |
| continueOnSuccess | [optional], if true, next formatter will be called even if the condition is true | Boolean |
Examples
{d.value:ifEmpty('D'oh!')} // input: null → output: "D'oh!"
{d.value:ifEmpty('D'oh!')} // input: [] → output: "D'oh!"
{d.value:ifEmpty('D'oh!')} // input: {} → output: "D'oh!"
{d.value:ifEmpty('D'oh!')} // input: "" → output: "D'oh!"
{d.value:ifEmpty('D'oh!')} // input: 0 → output: 0
{d.value:ifEmpty('D'oh!')} // input: "homer" → output: "homer"
{d.value:ifEmpty('D'oh!')} // input: [23] → output: [23]
{d.value:ifEmpty('D'oh!')} // input: {"id":3} → output: {"id":3}
:ifEqual(value, textIfTrue, continueOnSuccess)
v0.13.0+
Test if a value equals a variable.
The new formatter :ifEQ:show should be used instead of this one.
| Params | Description | Type |
|---|---|---|
| value | value to test | String, Integer, Boolean |
| textIfTrue | message to print if the value equals JSON data | String |
| continueOnSuccess | [optional], if true, next formatter will be called even if the condition is true | Boolean |
Examples
{d.value:ifEqual(100, 'bingo')} // input: 100 → output: "bingo"
{d.value:ifEqual(101, 'bingo')} // input: 100 → output: 100
{d.value:ifEqual('homer', 'bingo')} // input: "homer" → output: "bingo"
{d.value:ifEqual('bart', 'bingo')} // input: "homer" → output: "homer"
{d.value:ifEqual('', 'bingo')} // input: "" → output: "bingo"
{d.value:ifEqual(100, 'bingo')} // input: null → output: null
{d.value:ifEqual(null, 'bingo')} // input: null → output: "bingo"
{d.value:ifEqual(100, 'bingo')} // input: 0 → output: 0
:ifContain(value, textIfTrue, continueOnSuccess)
v0.13.0+
Test if a string or an array contains a value.
The new formatter :ifIN:show should be used instead of this one.
| Params | Description | Type |
|---|---|---|
| value | value to search | String, Integer, Boolean |
| textIfTrue | message to print if JSON data contains the value | String |
| continueOnSuccess | [optional], if true, next formatter will be called even if the condition is true | Boolean |
Examples
{d.value:ifContain('beauti', 'bingo')} // input: "your beautiful eyes" → output: "bingo"
{d.value:ifContain('leg', 'bingo')} // input: "your beautiful eyes" → output: "your beautiful eyes"
{d.value:ifContain('eyes', 'bingo')} // input: "your beautiful eyes" → output: "bingo"
{d.value:ifContain('eyes', 'bingo')} // input: "" → output: ""
{d.value:ifContain('', 'bingo')} // input: "your beautiful eyes" → output: "bingo"
{d.value:ifContain(120, 'bingo')} // input: [100,120,20] → output: "bingo"
{d.value:ifContain(99, 'bingo')} // input: [100,120,20] → output: [100,120,20]
{d.value:ifContain('eyes', 'bingo')} // input: ["your","eyes"] → output: "bingo"
{d.value:ifContain('eyes', 'bingo')} // input: [] → output: []