{table:id:cmd:r:c:val:param}

Description

Builds a small two-dimensional table in memory during one page render and reads values back out. The first argument names the table instance: every table: call sharing that id works on the same table. The second argument is the command - set stores a value at a row and column, get reads it back, addset adds a number to a cell, joinset appends to a cell with a delimiter, sum totals a column over all rows, and print renders the whole table as HTML. A cell can hold several named attributes (the 6th argument of set); get and sum take an output template where _#1 is the cell value or column total and _#attr pulls a named attribute. The table is not persisted - it exists only for the current render, and is never cached. Experimental: the author marks it as likely to be replaced by the array: command, so prefer array: for new templates.

Parameters

id required default (empty)

Names the table instance for this page render. Every {table:} call that uses the same id reads and writes the same in-memory table. The table lives only for the duration of one page render; it is not stored.

cmd required default (empty)

The operation to perform. One of: set (store a value), get (read a value back), addset (add a number to a cell), joinset (append to a cell with a delimiter), sum (total a column over all rows), print (render the whole table as an HTML table).

r optional default (empty)

Row key - any string identifies the row. Rows render in the order they are first written. Ignored by sum (which totals every row) and by print.

c optional default (empty)

Column key - any string identifies the column. Columns render in first-seen order. For get, c may be a dash-joined list (first-last) and the listed cells are concatenated. Ignored by print.

val optional default _#1 (for get and sum)

For set, addset and joinset this is the value to store or add. For get and sum it is an output template: _#1 is replaced by the cell value (get) or the column total (sum); _#attr pulls a named attribute of the cell (get). Defaults to _#1 for get and sum.

param optional default 1 (attribute name, for set)

Extra argument that depends on cmd. For set it is the attribute name to store the value under (default 1). For joinset it is the delimiter placed between joined values. Ignored by the other commands.

Examples

test{table:tE:set:0:tags:red}{table:tE:joinset:0:tags:green:, }{table:tE:get:0:tags}
Expectedred, green
Actualred, green
joinset appends value to the cell, separated by the 6th argument (here a comma and a space). Here set establishes the first tag, then joinset adds the second.
test{table:tD:set:0:total:10}{table:tD:addset:0:total:5}{table:tD:get:0:total}
Expected15
Actual15
addset adds a number to the running cell total (numeric add, not string append). Here set seeds the cell with 10, then addset adds 5.
test{table:tG:set:0:amt:100}{table:tG:set:1:amt:50}{table:tG:sum:x:amt:Sum is _#1 USD}
ExpectedSum is 150 USD
ActualSum is 150 USD
sum also accepts a value template with _#1 standing for the computed total. Avoid a colon inside the template - the colon would be read as the next argument separator.
test{table:tA:set:0:name:Alice}{table:tA:get:0:name}
ExpectedAlice
ActualAlice
set stores a value at row r, column c; get reads it back. The id (tA) names this table instance for the page render.
test{table:tB:set:0:first:Jane}{table:tB:set:0:last:Roe}{table:tB:get:0:first-last}
ExpectedJaneRoe
ActualJaneRoe
In get, the column argument may be a dash-joined list; the cells are concatenated in column order.
test{table:tC:set:0:price:42}{table:tC:get:0:price:[_#1]}
Expected[42]
Actual[42]
The value argument is a template; _#1 is replaced by the cell value. Useful to wrap or label the output.
test{table:tF:set:0:amt:100}{table:tF:set:1:amt:50}{table:tF:set:2:amt:25}{table:tF:sum:x:amt}
Expected175
Actual175
sum adds column c over every row. The row argument is ignored for sum. Here three rows hold 100, 50 and 25.
test{table:tH:set:0:m:5:value}{table:tH:set:0:m:kg:unit}{table:tH:get:0:m:_#value _#unit}
Expected5 kg
Actual5 kg
A cell can hold several named attributes (the 6th set arg). In get, an expression with _#attr placeholders pulls each named attribute.
virtual{table:tI:set:0:a:1}{table:tI:set:0:b:2}{table:tI:set:1:a:3}{table:tI:set:1:b:4}{table:tI:print}
Expected(HTML:
12
34
)
Actual
12
34
print emits the whole table as HTML rows and cells, in row order and first-seen column order. Marked illustrative because the browser renders the tags rather than showing them as text.