Widow Maker, I'm one step ahead of you, I didn't post the class I derived from it
. I have another class, called TemplatePage, which implements a basic template structure, like so:
PHP Code:
class TemplatePage extends Page {
var $values;
var $template;
//$title is the title of the page
//$template is html file with <php*> tags
function TemplatePage($title, $template) {
$this->template = file_get_contents($template);
$values = array();
$this->title = $title;
$this->set("<phptitle>", $title);
}
//erases everything already there, use only the first time or not at all
function set($key, $val) {
$this->values[$key] = $val;
}
//appends to the value, preserving anything thats already been added
function append($key, $val) {
$this->values[$key] .= $val;
}
//an error has occured
function panic($message) {
$this->set("<phperror>", $message);
//doesnt dump the Page buffer, because it doesn't know where to put it
$this->show();
@die();
}
//write the page to the template
//clears the page
//$show = true|false, decides whether to show it after writing or not
function writePage($key, $show = false) {
$this->append($key, $this->page);
unset($this->page);
if ($show) {
$this->show();
}
}
//show function
function show() {
foreach ($this->values as $key => $val) {
//slip the variables into the file, if something isnt defined it wont affect it
$this->template = ereg_replace($key, $val, $this->template);
}
echo ($this->template);
}
}
Just so everyone knows, I've got a third class, derived from this one, which does page caching, but I'm still deciding on the best method(serialization, database, files, etc) to use to cache it, so that class is relatively incomplete. What I was trying to achieve was something like this: I can include a template, and then create things like a nav menu section USING the calls to startdiv(), etc. then add it to the template in the same method i would add static html, using append() or set(). Example code:
PHP Code:
$template = new TemplatePage("Test", "main.tpl.html");
$template->startDiv();
$template->add("content here.");
$template->endDiv();
$template->writePage("<phpbody>", true);
main.tpl.html:
HTML Code:
<html>
<head>
<title>
<phptitle>
</title>
<link rel="stylesheet" href="<phpstyle>" type="text/css" />
</head>
<body>
<div>
<phperror>
</div>
<phpbody>
</body>
</html>
Basically, I split the two classes like that to keep the HTML generating code seperate from the template usage code. I don't use the first class exclusively, like you said--its a headache to maintain, but I DO use it to create specific elements of the page that aren't a part of the template. By doing it this way, I can create more generic templates(allowing me to reuse them) and add calls like those illustrated above in included files to add things like navigation.
Bookmarks