{define:name:expression}

Description

Stores a named expression (a variable) that you read back later with the var command. The expression is evaluated where define runs, and the stored value lasts for the whole page render - so define lets you compute something once and reuse it many times, or pass a value from one view into another (for example email templates). define itself prints nothing; it only stores. Within one expression, commands run left to right, so define a name before the var that reads it; and because the store is page-wide, a var can also read a value defined earlier elsewhere on the page. Redefining the same name overwrites it. An empty name is ignored. The companion command is var, which both reads a defined value and, in its two-argument form, defines one.

Parameters

name required

The variable name to store under. The same name is used later by var to read the value. An empty name is ignored (nothing is stored).

expression required

The value to store. Any nested AA expression here is evaluated first, so the already-computed result is what gets stored.

Examples

test{define:greeting:Hello, world}{var:greeting}
ExpectedHello, world
ActualHello, world
Store a value under a name, then print it with var. The most basic use.
test[{define:greeting:Hello}]
Expected[]
Actual[]
define only stores; it returns an empty string. The brackets show there is no output where define sits.
virtual[{var:msg}{define:msg:set}]
Expected(empty if msg is unset; page-dependent)
Actual[set]
Reading a name before it is defined yields whatever the page-global store holds at that point - empty if nothing set it yet. Because define values persist for the whole page render, a read-before-define result is page-dependent, not guaranteed empty. The reliable rule: define before you read.
test{define:total:{count:a-b-c-d}}You have {var:total} items, see {var:total} again
ExpectedYou have 4 items, see 4 again
ActualYou have 4 items, see 4 again
Store a computed result once (here a count), then reuse it without recomputing. The inner expression is evaluated when define runs, so var just replays the stored value.
test{define:x:first}{define:x:second}{var:x}
Expectedsecond
Actualsecond
Defining the same name twice keeps the last value. var reads whatever was stored last.
test{define:price:42}{math:{var:price}*2}
Expected84
Actual84
A stored value can be read by var anywhere, including inside another command such as math.