Template(#format, #args, #header)

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:

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/b91b80c3-1d55-47da-9579-0ce22f89e696/Screenshot_2021-06-18_at_17.11.29.png

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.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/d165bd7d-178f-4642-a484-4cc99a5dfe8e/Screenshot_2021-06-21_at_10.23.10.png

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.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/2e512a8b-39cf-4014-87cf-cd6d05b05ca3/Screenshot_2021-06-21_at_10.30.03.png

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.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/a4f577f9-2986-48f1-b4ca-9f4112b3c977/Screenshot_2021-06-21_at_10.37.48.png

Calling Template() with multiple format strings