Design

Aliases

Simplify Carbone tag with aliases
COMMUNITY FEATURE Available for:
Carbone Cloud
Carbone On-premise
Embedded Carbone JS
  v2.0+ 

Alias

Aliases can be used to simplify the maintenance of your report by avoiding repetition of code, or to insert tags where special characters such as square brackets are not allowed (for example, worksheet names).

Aliases can be defined anywhere in the document, at the end, or at the beginning. They are parsed and removed by Carbone before rendering.

data
{
  "name": "Cars",
  "wheels": 4
}
template
{#myAlias = d.wheels}{d.name} need {$myAlias} wheels!
Carbone Merge Icon
result
Cars need 4 wheels!

Aliases do not store values. They are simply shortcuts. More information

Parametrized Alias

Aliases accept an unlimited number of parameters between parentheses, like a function in many languages. Each parameter must start by $.

data
[
  { "name": "chicken", "weekday": 1 },
  { "name": "fish"   , "weekday": 2 }
]
template
{#mealOf($weekday) = d[weekday = $weekday]}Tuesday, we eat {$mealOf(2).name}.
Carbone Merge Icon
result
Tuesday, we eat fish.

More information

An alias cannot store a value. It is just a shortcut.

If you need to store a value, use the :set formatter:

{d.products[].price:aggSum:set(c.total)}
{c.value}

Aliases are replaced by Carbone with the actual tag before processing the template. They act as a preprocessor tool (similar to #define in C language).

For example:

Alias Template What Carbone Sees Result
d.obj {d.val:mul($alias.qty)} {d.val:mul(d.obj.qty)} OK
d.arr[i].obj {d.val:mul($alias.qty)} {d.val:mul(d.arr[i].obj.qty)} Not supported

The second case is not supported because Carbone does not support starting a loop with d.arr[i].obj inside a formatter.