There are many ways to pass database information into smarty templates. But in all cases you must explicitly do *something* to get the data.
1. If you just need to get a single record you can use the {load_record} tag.
e.g.
- Code: Select all
{load_record table="fundraiser_information" id="10" var="info"}
{$info->display('slogan')}
...
The {load_record} tag must include a "table" attribute to specify the table from which the record should be loaded, and a "var" attribute to specify the name of the variable where the resulting record will be stored. You may also pass additional parameters named after columns in the table to specify query parameters. In the above example it loaded a record from the fundraiser_information table whose "id" column = 10, and stored the record in the $info variable. Hence the $info variable would store a Dataface_Record object.
2. If you want to have a little more control over what you load, you could load the data in a controller, and the pass the data to your template.
e.g. in actions/my_action.php
- Code: Select all
class actions_my_action {
function handle(&$params){
$res = mysql_query("select * from fundraiser_information", df_db());
$info = array();
while ( $row = mysql_fetch_assoc($res) ) $info[] = $row;
@mysql_free_result($res);
df_display( array('fundraiser_information' => & $info), 'my_template.html');
}
}
and in templates/my_template.html:
- Code: Select all
{foreach from=$fundraiser_information item=info}
{$info.slogan}
{/foreach}
...
Notice in this example that each record is an array rather than a Dataface_Record object. This is because we obtained the records using mysql_query(), and why we use the notation {$info.slogan} rather than {$info->display('slogan')}.
If we had used df_get_records_array() to obtain our records instead, then they would have been Dataface_Record objects.
You would access this page using the url index.php?-action=my_action
3. If already have a record accessible in your template you can use that record to obtain related records (e.g. using the getRelatedRecordObjects() method). You can also define custom fields in the delegate class that returns an array of records.... I'll save that for another tutorial...
-Steve