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

data
{
  "val2" : 2,
  "val5" : 5
}
template
val2 = {d.val2:ifGT(3):show('high')}val5 = {d.val5:ifGT(3):show('high')}val2 = {d.val2:ifGT(3):show('high'):elseShow('low')}
Carbone Merge Icon
result
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:

data
{
  "val1" : 1,
  "val2" : 2,
  "val3" : 3
}
template
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)}
Carbone Merge Icon
result
val1 = Aval2 = Bval3 = C

Syntax alternative: {d.val1:ifEQ(1):show(A):or(d.val1):ifEQ(2):show(B):elseShow(C)}

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.

data
[
  { "name": "John", "age": 20 },
  { "name": "Eva", "age": 18 },
  { "name": "Bob", "age": 25 },
  { "name": "Charly", "age": 30 }
]
template
People{d[i].name}: {d[i, age > 19].age}{d[i+1].name}
Carbone Merge Icon
result
PeopleJohn: 20Eva: Bob: 25Charly: 30

: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

null:ifEmpty('D'oh!') // "D'oh!"
[]:ifEmpty('D'oh!') // "D'oh!"
{}:ifEmpty('D'oh!') // "D'oh!"
'':ifEmpty('D'oh!') // "D'oh!"
0:ifEmpty('D'oh!') // 0
'homer':ifEmpty('D'oh!') // "homer"
[23]:ifEmpty('D'oh!') // [23]
{'id':3}:ifEmpty('D'oh!') // {"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

100:ifEqual(100, 'bingo') // "bingo"
100:ifEqual(101, 'bingo') // 100
'homer':ifEqual('homer', 'bingo') // "bingo"
'homer':ifEqual('bart', 'bingo') // "homer"
'':ifEqual('', 'bingo') // "bingo"
null:ifEqual(100, 'bingo') // null
null:ifEqual(null, 'bingo') // "bingo"
0:ifEqual(100, 'bingo') // 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

'your beautiful eyes':ifContain('beauti', 'bingo') // "bingo"
'your beautiful eyes':ifContain('leg', 'bingo') // "your beautiful eyes"
'your beautiful eyes':ifContain('eyes', 'bingo') // "bingo"
'':ifContain('eyes', 'bingo') // ""
'your beautiful eyes':ifContain('', 'bingo') // "bingo"
[100,120,20]:ifContain(120, 'bingo') // "bingo"
[100,120,20]:ifContain(99, 'bingo') // [100,120,20]
['your','eyes']:ifContain('eyes', 'bingo') // "bingo"
[]:ifContain('eyes', 'bingo') // []