Okay... Pause for a moment, till I gather my thoughts...
First, I can't post any script I have upto now developed, but in all fairness, and to help the PHP community as a whole, I have been working on another approach, and on the theme of the Composite View pattern, will post the following 'proof of concept' script instead.
It's rough, no unit testing, etc, but it works so you can get the basic idea at least 
PHP Code:
interface IComposite {
public function getId();
public function getParent();
public function getTemplate();
}
interface IActionHandler {
}
abstract class BaseComposite implements IComposite {
public $children = array();
abstract public function __construct();
abstract public function attach( IComposite $composite );
abstract public function accept( Visitor $visitor );
public function getId() {
return $this -> controller -> id;
}
public function getParent() {
return $this -> controller -> parent;
}
public function getTemplate() {
return $this -> controller -> template;
}
}
abstract class BaseActionHandler implements IActionHandler {
public function __construct() {}
}
class PageActionHandler extends BaseActionHandler {
public $id;
public $parent;
public $template;
public function __construct() {
$this -> id = 'page';
$this -> parent = '';
}
public function execute( $request ) {
// you would do you own complex calculations here,
// work out your models, etc, keeping the models here with
// the controller, thus
//
// letting the visitor access those models (from the visitor), so
// it's the visitor that composes the template (view) with the
// model data, from the actual visitor it's self
//
// as this is an example, i'm just returning a basic template file
// so you can see how it all gells together ;)
return file_get_contents( 'page.tpl' );
}
}
class BodyActionHandler extends BaseActionHandler {
public $id;
public $parent;
public $template;
public function __construct() {
$this -> id = 'body';
$this -> parent = 'page';
}
public function execute( $request ) {
return file_get_contents( 'body.tpl' );
}
}
class FooterActionHandler extends BaseActionHandler {
public $id;
public $parent;
public $template;
public function __construct() {
$this -> id = 'footer';
$this -> parent = 'page';
}
public function execute( $request ) {
return file_get_contents( 'footer.tpl' );
}
}
class MenuActionHandler extends BaseActionHandler {
public $id;
public $parent;
public $template;
public function __construct() {
$this -> id = 'menu';
$this -> parent = 'body';
}
public function execute( $request ) {
return file_get_contents( 'menu.tpl' );
}
}
class Composite extends BaseComposite {
public function __construct( IActionHandler $controller ) {
$this -> controller = $controller;
}
public function attach( IComposite $composite ) {
$this -> children[] = $composite;
}
public function accept( Visitor $visitor ) {
$visitor -> visit( $this );
}
}
class Visitor {
private $request;
public function __construct( $request ) {
$this -> request = $request;
}
public function visit( IComposite $composite ) {
$this -> template[$composite -> getId()] = $composite -> controller -> execute( $this -> request );
foreach( $composite -> children as $child ) {
$this -> visit( $child );
$this -> template[$composite -> getId()] = ereg_replace( '<'.$child -> getId().' />', $this -> template[$child -> getId()], $this -> template[$composite -> getid()] );
}
}
}
class Request {} // your request,
$visitor = new Visitor( new Request() );
$c = new Composite( new PageActionHandler() );
$body = new Composite( new BodyActionHandler() );
$body -> attach( new Composite( new MenuActionHandler() ) );
$c -> attach( $body );
$c -> attach( new Composite( new FooterActionHandler() ) );
$c -> accept( $visitor );
echo( $visitor -> template['page'] );
And the crappy template files,
page.tpl
Code:
<html><body>
<p>This is the PAGE template</p>
<p><body /></p>
<p><footer /></p>
</body></html><!-- a shi**y template but it'll do :p -->
body.tpl
Code:
This is the BODY template<p><menu /></p>
menu.tpl
Code:
And finally, this is the MENU, belonging to the BODY template
footer.tpl
Code:
This is the FOOTER template
And, the resultant output generated,
Code:
<html><body>
<p>This is the PAGE template</p>
<p>This is the BODY template<p>And finally, this is the MENU, belonging to the BODY template</p></p>
<p>This is the FOOTER template</p>
</body></html><!-- a shi**y template but it'll do :p -->
Umm...
The Visitor in question would require a means to gain access to the Model data from the Controller it's self [MVC], and in one form or another, prior to the foreach() { ... } loop, insert the Model data to the template.
For this example, I just returned a basic file_get_contents() but in fact, it's the template to use based on the logic that the Controller has. The View [MVC] is the Visitor it's self, and for most purposes, you only need the one Visitor, so let's keep it simple.
Questions, Comments, Etc Etc I'm all open to hear from you. As to the problem of MatID, I would need to see more script, bar just the Interfaces,
Dr Livingston
Bookmarks