PHP

The application logic

Example 13-1 is the application logic that produces the order receipt. The script logic is identical to that of the shipping.2 script discussed in Chapter 12. The different features of this script are the omission of any code to produce output, and the inclusion of the fragments of code that assign database values to presentation elements, parse these elements, and call functions in the template class library.

The script in Example 13-1 works as follows:

  1. Include the xtpl.p template library.

  2. 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.

  3. Query the customer table and assign the returned $row to an element of the template named CUSTOMER. Also assign the orderID session variable to an element of the template named ORDER_ID. Uppercase strings are used to distinguish template elements from variables in the script, but this isn't essential. After assigning the data, parse the main.customer template data (we discuss the structure of the template later in this section).

  4. Retrieve and assign each item in the order to the template elements QTY, WINE, PRICE, and TOTAL. 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.

  5. 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 overall main 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");