Advanced substitution

For more complex substitutions the %{<cmd>(arg1,arg2,...):<options>}% can be used. <cmd> is one of the commands listed below with the argument that command takes and the optional :<options> can be used to apply some text processing to the string before it's inserted into the text being transformed.

Substitution commands

Commands can be run to get more advanced substitutions, all commands take arguments. When most commands are run the arguments are run though the template engine to replace any substitutions that might be needed later. For example to show a customer record and all addresses that belong to that customer you can have a template for the customer that looks like this.

Example 11.1. Customer template example

Name %name%<br> <ol>%{query("select line1,city from address where cust_no=%cust_no%","<li>%line1% %city%</li>")}%</ol>

In this example the %cust_no% in the SQL argument of the query template will be replaced with the cust_no primary key from the record being shown before the query is run. The line1 and city used in the template will come from records in the query. Only columns returned by the query is available inside the second template. If you try to add %cust_no% to the inner template an error will result.


It's possible to put any template code inside an argument that is expanded before run, but since the argument runs to the end of the string expansions inside an argument must have it's argument string characters escaped.

query(sql,template)

Execute the SQL query and expand the template for each row. SQL is expanded with the current row data but template is left alone.

editLink(tablename,pkvalue,linktext)

Generate a link to edit a record in tablename. pkvalue is the primary key for the record to edit, linktext will be shown in the link. If pkvalue is left empty it's extracted from values in the current row.

deleteLink(tablename,pkvalue,linktext)

Generate a link to delete a record in tablename. pkvalue is the primary key for the record to delete, linktext will be shown to the user. If pkvalue is left empty it will be retrieved from the current row.

addLink(tablename,basetable,linktext)

Generate a link to add a new record, if basetable is set it should be the name of a table with values in current row, the primary key will be transported to the new record so it's linked to basetable.

editField(table,field)

Insert an edit widget for table.field. If there is a value in the current row for this it will be inserted as default, otherwise it will be blank.

translate(tablename,fieldname,key,language_no)

Translate the row with the primary key "key" in "tablename"."fieldname" to language_no.

describe(tablename,pkvalue)

Generate a one line description for the current record of tablename if no pkvalue is set, or use pkvalue to get a record otherwise. The description is stored in the tables.struct file.

address(address_no,cust_no)

Display a formatted address based on the country of the address. cust_no is optional and if exists will add an "Attn: name" line after the company name of the address. The output of this is formated to html, if you need to use it as plain text add the "text" option after this command.

Options

There is currently only one option, "text" that will convert html code inside the expanded text into pure text. <br> will be converted to new line and everything else will simply be removed.