Re: [Phplib-users] Template - block name showing when no block returned
Brought to you by:
nhruby,
richardarcher
|
From: Layne W. <la...@if...> - 2002-04-18 16:37:53
|
> Thanks for the email. You're right, I had left "keep" in the
> initialisation statement (I've been blindly following the examples I
> found on day one). Removing it has fixed the problem. Is there any
> reason for leaving it in, other than for debugging?
I have found one application for it in production, but I only apply "keep"
for a short while before I resume "remove". There are probably other uses.
When I have a search that allows the user to sort the results by any number
of columns, I want to arrange the columns of my list table to match the
order of the sort columns. I don't want to be parsing blocks for each item
in a list, so I make one pass with "keep" to arrange the columns and then
loop through the results with "remove" to insert the data.
The following example is from a time-billing application we use internally.
My search results template contains:
---------------------------------------------------------------------
<table border="0" cellpadding="5" cellspacing="0">
<tr>
<!-- BEGIN employee_column_head -->
<th class="listHead" width="115">Employee</th>
<!-- END employee_column_head -->
<!-- BEGIN date_column_head -->
<th class="listHead" width="81">Date</th>
<!-- END date_column_head -->
<!-- BEGIN client_column_head -->
<th class="listHead" width="115">Client</th>
<!-- END client_column_head -->
<!-- BEGIN task_column_head -->
<th class="listHead" width="115">Task</th>
<!-- END task_column_head -->
<!-- BEGIN hours_column_head -->
<th class="listHead" width="81">Hours*</th>
<!-- END hours_column_head -->
</tr>
<!-- BEGIN ticket -->
<tr>
<!-- BEGIN employee_column -->
<td valign="top">{employee}</td>
<!-- END employee_column -->
<!-- BEGIN date_column -->
<td valign="top">{date_display}</td>
<!-- END date_column -->
<!-- BEGIN client_column -->
<td valign="top">{client}</td>
<!-- END client_column -->
<!-- BEGIN task_column -->
<td valign="top">{task}</td>
<!-- END task_column -->
<!-- BEGIN hours_column -->
<td align="center" valign="top">
<table border=0 cellpadding=0 cellspacing=0 width="71">
<tr>
<td align="left" width="38">{hours_worked} </td>
<td align="right" width="33">{hours_billed}</td>
</tr>
</table>
</td>
<!-- END hours_column -->
</tr>
<!-- END ticket -->
</table>
---------------------------------------------------------------------
My results script - after setting the search query, etc.
Note: I use 2 instances of Template which I have extended to allow one
argument to parse (target and varname are assumed to be the same) and adding
clear_var() - a shortcut for set_var("varname", "") that allows arrays of
varnames.
---------------------------------------------------------------------
$tpl->set_block("content", "ticket", "tickets");
$loop_tpl->set_var("DR", $DR);
$loop_tpl->set_var("ticket", $tpl->get_var("ticket"));
/* setup column order */
$tpl->set_block("results", "employee_column_head", "column_head_1");
$tpl->set_block("results", "date_column_head", "column_head_2");
$tpl->set_block("results", "client_column_head", "column_head_3");
$tpl->set_block("results", "task_column_head", "column_head_4");
$tpl->set_block("results", "hours_column_head", "column_head_5");
$loop_tpl->set_block("ticket", "employee_column", "column_1");
$loop_tpl->set_block("ticket", "date_column", "column_2");
$loop_tpl->set_block("ticket", "client_column", "column_3");
$loop_tpl->set_block("ticket", "task_column", "column_4");
$loop_tpl->set_block("ticket", "hours_column", "column_5");
$loop_tpl->set_var("arranged_ticket", $loop_tpl->get_var("ticket"));
$default_column_order = array("employee", "date", "client", "task",
"hours");
$column_order = array();
/* get requested sort order */
foreach((is_array($search_info["sort_order"]) and
count($search_info["sort_order"])) ? $search_info["sort_order"] :
explode(",", $default_sort_order) as $_sort_criteria)
{
$column_order[] = $_sort_criteria;
}
/* if not all five sort order columns were used, fill in the rest */
foreach($default_column_order as $_default_criteria) {
if(!in_array($_default_criteria, $column_order)) $column_order[] =
$_default_criteria; }
$loop_tpl->unknowns = "keep";
for($i = 0; $i < 5; $i++)
{
$tpl->parse("column_head_" . ($i + 1), $column_order[$i] . "_column_head");
$loop_tpl->parse("column_" . ($i + 1), $column_order[$i] . "_column");
}
$loop_tpl->parse("arranged_ticket");
$loop_tpl->set_var("ticket", $loop_tpl->get("arranged_ticket"));
$loop_tpl->unknowns = "remove";
$tpl->clear_var(array("employee_column_head", "date_column_head",
"client_column_head", "task_column_head", "hours_column_head"));
$loop_tpl->clear_var(array("unarranged_ticket", "employee_column",
"column_1", "date_column", "column_2", "client_column", "column_3",
"task_column", "column_4", "hours_column", "column_5"));
/* build list */
while($db->next_record())
{
$loop_tpl->set_var($db->Record);
if($db->Record["hours_worked"] == $db->Record["hours_billed"])
$loop_tpl->clear_var("hours_worked");
else $loop_tpl->set_var("hours_worked", "(".$db->Record[hours_worked].")");
$loop_tpl->parse("tickets", "ticket", true);
}
$tpl->set_var("tickets", $loop_tpl->get_var("tickets"));
---------------------------------------------------------------------
Layne Weathers
Ifworld Inc.
|