Looking for a simple template with "optional" fields

I'm looking for a simple template with the following property: The
resulting page should look something like this (say):

<div>
This is some text that never changes.
[[[$some variable that can be set from the calling PHP code]]]
This is some more text that never changes.
</div>

The crux is that if the calling code never initializes the variable,
then the entire <div> should not be displayed.

The use-case that I see is this: I have a program that mostly needs to
display long(ish) lists to the clients, but the lists can be
customized. Sometime I want to display the first five columns for each
row, sometime the last four columns, other times only the odd-numbered
columns, etc.

What I want, is to have only the following in my PHP code:

$qry = "SELECT [[[whatever fields I'm interested in]]] FROM mytable
WHERE...";
$res = db_query($qry);
while ($row = db_fetch_assoc($res)) {
$template->add_params($row);
$template->print();
}


In the template I will have something like this:

{<td>[[[$col1-name]]]</td>}
{<td>[[[$col2-name]]]</td>}
....

and only the <td>'s whose column is actually included in $row will be
displayed.


It seems quite trivial to write such a template (I really don't need
much logic there at all), probably a week worth of work. But it will
be even better if I can just use something out there instead.

Thanks,

-- Shai
Shai Halevi [ Fr, 18 Januar 2008 14:31 ] [ ID #1911471 ]

Re: Looking for a simple template with "optional" fields

Shai Halevi wrote:

> The use-case that I see is this: I have a program that mostly needs to
> display long(ish) lists to the clients, but the lists can be
> customized. Sometime I want to display the first five columns for each
> row, sometime the last four columns, other times only the odd-numbered
> columns, etc.
>
> What I want, is to have only the following in my PHP code:
>
> $qry = "SELECT [[[whatever fields I'm interested in]]] FROM mytable
> WHERE...";
> $res = db_query($qry);
> while ($row = db_fetch_assoc($res)) {
> $template->add_params($row);
> $template->print();
> }
>

You need a simple way to turn on/off columns that you want to display, you
could store the column names in an array, works quite well.

$template->showcolumns($columnsarray);

and then your query could work like

$qry="SELECT ".implode(',',$this->columnarray)." FROM yourtable WHERE ...";

The number of columns is then based on the size of the array and is quite
trivial to implement, I don't think you would be spending more than some ten
fifteen minutes to do it.


--

//Aho
Shion [ Fr, 18 Januar 2008 19:13 ] [ ID #1911472 ]
PHP » alt.php » Looking for a simple template with "optional" fields

Vorheriges Thema: Ideas how to achieve this?
Nächstes Thema: Send an email automatically? is it possible?