Allows “loops” in the template to be defined, as might be required when display the contents of the database query result, row by row. For example a list of user names;
<list:list id="Users"> <table> <tr> <th>Username</th> <th>Email</th> <th>Full Name</th> </tr> <list:item> <tr> <td>{$Username}</td> <td>{$Email}</td> <td>{$Fullname}</td> </tr> </list:item> </table> </list:list>
You can use the list:default to contain some default content, should there be no data to iterate over in the list:
<list:list id="Users"> <ul> <list:item><li>{$Username} ($Email)</li></list:item> <list:default><li>No currently active users</li></list:default> </ul> </list:list>
The ‘code behind’ which populates these template fragments with data might look like:
require 'framework/common.inc.php'; require WACT_ROOT . '/template/template.inc.php'; define('DB_DATABASE', 'wack'); define('DB_HOST', 'localhost'); define('DB_USER', 'wack'); define('DB_PASSWORD', 'wacky'); // Make MySqlRecordSet et al. available require WACT_ROOT . '/db/mysql.inc.php'; // Load the template $Page =& new Template('/users.html'); // Find the runtime component $UserList =& $Page->findChild('Users'); // Get the users record set $UserSet = & NewRecordSet('SELECT * FROM users') // Register the record set as the data source for the runtime component $UserList->registerDataSet($UserSet); // Display the page $Page->display();
The list tag might form the basis for building more complex components which simplify the construction of HTML, for example a <calendar:Calendar/> tag which significantly reduces the amount of HTML required for constructing a calendar.
A List can be populated directly from a data set using the from attribute.
<list:list from="Users"> <ul> <list:item><li>{$Firstname} {$Lastname}</li></list:item> </ul> </list:list>
$Page->set('Users', $UserSet);
When is this “pull”-assignment preferable to registerDataSet() as shown above? The from attribute syntax helps eliminate a couple of lines of code from the view and for simple views can allow you to use a standard view instead of a custom view class.