Re: yet another template
| From: | Lux | Date: | Wed, 07 Nov 2001 18:14:58 +0000 |
| Subject: | Re: yet another template | ||
| Groups: | php.pear.dev | ||
| Request: | Send a blank email to pear-dev+get-2616@lists.php.net to get a copy of this message | ||
I have a template system I wrote that hopefully someone here might find useful, maybe even to take ideas from or incorporate into some standard template class. I don't have the latest copy with me to attach (it's at home), but here's a link to the documentation (it's a little out of date, I'm about to release a new version of this stuff):
http://www.simian.ca/index/docreader_view/class.Template
and it's part of a download package at:
http://freshmeat.net/projects/saf/
I did things a little differently than usual. Instead of manually saying $tpl->assign ('key', 'value') a bunch of times, you simply pass the template object an object or hash whose properties or pairs are used to fill out the template, like this:
while ($obj = $db->fetch ()) {
// assume $db->fetch () fetches as object or hash...
echo $tpl->fill ('some_template.tpl', $obj);
}
This makes the code a lot shorter. Templates look something like this:
<html>
<head>
<title>##title##</title>
</head>
<body>
<h1>##title##</h1>
[Include: menu.tpl]
##body##
</body>
</html>
There are various [Func: ...] hooks, and variable replacements happen on the ##keyword## occurrences.
The fill() method accepts either a template filename, or the template data itself, some object or hash, an optional database connection object (using an abstraction layer I wrote), and a 1 or 0 (0 by default) as sort of a "fast" switch.
The "fast" switch means just do the keyword replacements and forget about [Func: ...] hooks.
The database object is for an [SQL: ...] hook that automatically queries a database and returns its results with the template. This is the only control structure available, otherwise, you can use includes and multiple templates, and (although I don't encourage it) inline PHP.
I benchmarked this against a few others a while back, and it's fast too. I've released this along with a group of about 20-30 other classes, and the only trouble with contributing them to PEAR is that they are rather dependent on each other. I'm preparing another release within a week or two, so the stuff on freshmeat is a little outdated, but essentially the API is the same.
Feedback is welcome! Thanks,
Lux
--
Simian Systems
http://www.simian.ca/
Web Content Management