Widow Maker, I have finished my new template classes in a MVC pattern. I'm sorry to say that after using it, I'm starting to like MVC
.
Here's my code:
PHP Code:
controller.class.php:
class Controller {
var $template;
var $fragments;
function Controller() {
$this->fragments = array();
}
//creates a new template class and saves a pointer to it
function &createTemplate($style, $template) {
$temp = &new Template($style, $template);
$this->template = &$template;
return $temp;
}
//create a new Fragment and add stuff to it
function &createFragment($data) {
$frag = &new Fragment();
$frag->add($data);
$this->fragments[count($this->fragments)] = &$frag;
return $frag;
}
}
template.class.php:
//the template class
//the final output layer
//accepts mini-templates(fragments)
class Template {
var $template;
var $fragments;
//$style is filename of stylesheet
//$template is html file with <php*> tags
function Template($style, $template) {
$this->template = file_get_contents($template);
$this->fragments = array();
$this->set("style", $style);
}
//overwrites a fragment with a new one, containing $val
function &set($key, $val, &$control) {
unset($this->fragments[$key]);
$this->fragments["php".$key] = &$control->createFragment($val);
return $this->fragments["php".$key];
}
//set a Fragment(note: only one fragment can be in a space...to append more
//modify the fragment to include more HTML)
function setFragment($key, &$fragment) {
$this->fragments["php".$key] = &$fragment;
}
//an error has occured
function panic($message, &$control) {
$this->set("error", $message, &$control);
$this->show();
@die();
}
function show() {
foreach ($this->fragments as $key => $val) {
//get the contents of each fragment and send it to the template
$this->template = eregi_replace("<".$key.">", $val->show(), $this->template);
}
echo ($this->template);
}
}
//the mini-template class accepted by Template
class Fragment {
var $fragments;
var $page;
function Fragment() {
$this->fragments = array();
}
//function to add content directly to the page
function add($content) {
$this->page .= $content."\n";
}
//function to add a fragment
function addFragment(&$fragment) {
$this->fragments[count($this->fragments)] = &$fragment;
}
//returns the contents of $page
function show() {
$temp = $this->page;
foreach ($this->fragments as $val) {
$temp .= $val->show();
}
return $temp;
}
}
test.php(the code to use everything):
//set the style and template file
$style = "style.css";
$template = "main.tpl.html";
//require the class files
require_once("controller.class.php");
require_once("template.class.php");
$control = new Controller();
//create the template and test set()
$template = &$control->createTemplate($style, $template);
$fragmentA = &$template->set("title", "Test of MVC Template", &$control);
//create the fragment and test setFragment
$fragment = &$control->createFragment("Body Content.<br>");
$fragment->add("More Body Content.");
$template->setFragment("body", &$fragment);
//show everything
$template->show();
For new fragments, perhaps I want one that'll automatically put stuff in <div> tags, i just derive from Fragment and add a function to Controller to create that type of fragment. I'm also using my functions from the Page class that I posted earlier, which everyone said would be a headache to use, etc. for the subclasses of Fragment. Basically, DivFragment will have startDiv() and endDiv() in addition to add(), TableFragment will have all of the table functions, etc.
Personally, I'm happy with this, but if you see room for further improvement of the classes, tell me and I'll work on yet another revision(that would make four versions
).
Bookmarks