The Planarly Template()
function is modelled on the c++ 20 std::format
and the Python str.format
functions. It takes a format
string (or table of format
strings) containing fields, a table containing the arguments (args
) to be interpolated into format string, and a header
table that uses position to map named fields to cells in the argument table.
Format string placeholders
A format string uses the field {}
to mark where an argument should be placed in the string. Empty fields, {}
, positional fields, {1}
, and named fields, {y}
can be used. The different types can be mixed in the same format string, but empty fields cannot come after any positional or named placeholders. Note: Due to the way empty fields are mapped to arguments, they should be avoided when calling Template()
with multiple format strings.
The following examples of calling Template()
with the different field types will all use the same pre-defined values:
There are three format fields in the first row, format1
, format2
and format3
. Note the first and last cells also have the labels fmts
and Zfmts
respectively so we can pass all three format strings as a table to Template()
.
Below the format strings is a table containing the arguments to interpolate into the format strings. At the bottom is the header table, which maps field names to column positions in the arguments table.
Using empty fields in format strings
If the format string contains empty fields, {}
, Planarly will fill in values working left to right for each row of the arguments.
If there are more empty fields than columns in the arguments, then an error will occur. As you can see above you can pass the format string to Template()
directly as an argument. Since we are not using named fields we do not need to pass in the #header
argument.
Using positional fields in format strings
Positional fields specify the column in the arguments to be substituted using the column number within a pair of brackets: {1}
. Planarly uses zero indexing, so the first column, as shown below, is referenced with {0}
, the second with {1}
, etc.
Like empty fields, positional fields do not require the header
argument.
Using named fields in format strings
Named fields use the values of the #header
argument to map field names to argument columns, {foo}
. #header
is expected to be a 1xN
row vector. Below the named fields {n}
and {x}
are used from #header
to pull out names and numbers from #args
.
Calling Template()
with multiple format strings