The script in Example 13-1 works as follows:
-
Include the
xtpl.p
template library. -
In the function
show_HTML_receipt( )
, associate the script with the template shown in Example 13-2:$xtpl= new XTemplate ("example.shipping.3.xtpl");
This creates a new template object called
$xtpl
. -
Query the customer table and assign the returned
$row
to an element of the template namedCUSTOMER
. Also assign theorderID
session variable to an element of the template namedORDER_ID
. Uppercase strings are used to distinguish template elements from variables in the script, but this isn't essential. After assigning the data, parse themain.customer
template data (we discuss the structure of the template later in this section). -
Retrieve and assign each item in the order to the template elements
QTY
,WINE
,PRICE
, andTOTAL
. Now check that this data is correctly formed and associated by parsing it. The following lines perform these functions:// Assign the qty, wine details, price, and total item // cost to the template $xtpl->assign("QTY", $row["qty"]); $xtpl->assign("WINE", $wineDetail); $xtpl->assign("PRICE", sprintf("%-.2f", $row["price"])); $xtpl->assign("TOTAL", sprintf("%-.2f", $itemsPrice)); // Parse a template row of items $xtpl->parse("main.items.row");
We explain how this relates to the template later.
-
Assign the overall order total to the template and check the overall structure of the data. The final check includes parsing the
items
and the overallmain
output, and then outputting the data with the following code:$xtpl->assign("ORDER_TOTAL", sprintf("%-.2f", $orderTotalPrice)); // parse all items $xtpl->parse("main.items"); // parse the whole document $xtpl->parse("main"); // output the templated data $xtpl->out("main");