Formats/Templates
| From: | Stefan Wasilewski | Date: | Wed, 03 Mar 1999 16:09:30 +0000 |
| Subject: | Formats/Templates | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-4460@lists.php.net to get a copy of this message | ||
Greetings,
My apologies for not lurking longer before posting, and for the length
of the post. I am interested in something similar to perl 'formats' for
php. I think these would be especially powerful when applied to
generating rows in html tables.
For those of you not familiar with perl's formats, they work like this:
(my apologies for alignment problems)
# a report on the /etc/passwd file
format STDOUT_TOP =
Passwd File
Name Login Office Uid Gid Home
------------------------------------------------------------------
.
format STDOUT =
@<<<<<<<<<<<<<<<<<< @|||||||
@<<<<<<@>>>> @>>>>
@<<<<<<<<<<<<<<<<<
$name, $login, $office,$uid,$gid, $home
.
(stolen from the perlform(1p) man page)
And they're usually used within a while() loop, like:
while ( $result = run_query() ) {
$name = $result->name;
$login = $result->login;
...
write;
}
Which would print the format with the first '@<<<<<<<' interpolated
as a
left justified $name, etc. It's also (one of the keys here) possible to
run a function/subroutine call to return one of the variables. So we
could have a &disk_space_used($name) as the last variable.
I think it would be very powerful to write a 'format' in php in (almost)
plain html that would automatically assign variables from a SQL query
and iterate over the resulting records. I'm not sure of syntax here,
but something like this:
<?php
query_format USERS_TOP =
<table>
<tr><th>Name</th><th>Age</th></tr>
.
query_format USERS =
<tr><td>%name</td><td>get_age_from_birtday(%birthdate)</td></tr>
.
query_format USERS_BOT =
</table>
.
$result = run_sql_query("SELECT name, birthdate FROM users");
write_formatted_query("USERS", $result, $number_of_records_per_table");
?>
I'm sure I'm missing quite a lot here, but I think this is part of what
makes perl such a good reporting language, and I think it's fits into
html better than most places. Isn't easy yet powerful creation of html
tables from query results enough of a generic need that it merits
examination of a special case feature aimed at it?
I may very well try to implement this myself, as an addition to the php
core language or (possibly?) an add-on, but I'd love to hear about your
feelings on the idea. I'm especially interested in knowing if someone
else has already done this in some other form.
Thanks for your time,
-Stefan
Stefan Wasilewski
Triton Network Systems, Inc.
smw@triton-network.com
--
PHP Development Mailing List http://www.php.net/
To unsubscribe send an empty message to php-dev-unsubscribe@lists.php.net
For help: php-dev-help@lists.php.net