[eluser]7bit[/eluser]
The idea with the "intelligent" view came when reading a discusson somewhere (don't remember which forum) about the MVC pattern in general and its use in most of the web frameworks. Someone said: In *true* MVC the controller doesn't push data into the view, insted the view has an observer relationship with the model and pulls it's data out of it whenever the model changes, and it does this without interacting with the controller. The controller should only deal with user *input*. The conclusion someone could draw from this discussion would be that most frameworks that do it like CI aren't true MVC but something else. (not necessary something wrong but just something else)
Since it is in most cases not possible or practicable to notify the view directly about changes in the model the closest one can get to this pattern is to simply regard every page request as notification for the view that the model *may* have changed and it is time to pull the current state and display it.
It was just my attempt to see whether those OOP gurus in this discussion were right and MVC is really a different thing or whether one could just move some code around and have a "better" MVC without changing the way the framework works. I'm not sure which way it is really better, i was just thinking about multiple controllers loading the same (part of the) view and having all relevant logic for this part of the view in one place together with the view. This *may* make parts of the application more modular.
I have some free time and I'm just experimenting. Yesterday evening I spontaneously decided to write my own ultra-small bare bones mini MVC framework. Heavyly inspired by CI and Kohana i hacked together just the bare minimum to achieve the routing of URIs to controllers, some autoloading of classes, the cascading, etc., just a few hundred lines of code. Nothing special, and then while I pondered about the mechanisms that drive the views I had an idea: The whole problem with view partials and repeating view-related code in the controller or in different views comes from the fact that all those views work *procedural*. Why not implement override polymorphism in view templates? Just make all views classes that can inherit from each other and let them extend existing views, override parts of the parent view with their own version and so on. PHP is a full blown *template* language and it's object oriented, so why not use it to its full potential when it comes to *templates*?
Consider the following view template for my (not yet existing) template engine:
Code:
<?class V_index extends Polytemplate{function part_0(){?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html >
<head>
<title>Conforming XHTML 1.0 Strict Template</title>
</head>
<body>
<?}function part_1(){?>
<div>part_1: Hello world!</div>
<?}function part_2(){?>
<div>part_2: <?=$this->data->foo?></div>
<?}function part_3(){?>
<div>part_3: <?=$this->data->bar?></div>
<?}function part_4(){?>
<div>part_5: End.</div>
<?}function part_999(){?>
</body>
</html>
<?}}?>
it inherits a constructor from Polytemplate:
Code:
<?php
class Polytemplate{
function __construct($data = null){
$this->data = $data;
$parts = get_class_methods($this);
sort($parts);
foreach ($parts as $key => $method){
if (substr($method, 0, 5) == 'part_'){
$this->$method();
}
}
}
}
?>
All methods in the view that begin with 'part_' will be called in alphabetic order (therefore their names contain numbers) when the class is instantiated, the resulting html will go to the browser or the output buffer in the correct order.
Now consider this:
Code:
<?class V_index2 extends V_index{
// this class will
// * inherit header, footer and part 4
// * decorate part1
// * remove parts 2 and 3
// * add a new part 2a
function part_1(){?>
<div style="color: green;">
<?parent::part_1()?>
</div>
<?}function part_2(){?>
<?}function part_3(){?>
<?}function part_2a(){?>
<div style="color: red;">
<b><?=$this->data->bar?> <?=$this->data->foo?></b>
</div>
<?}}?>
This really works! This is fun! Imagine what you could do with it! I have to play with this for a while, but I think it is a great improvement over the procedural style used in CodeIgniter and many other frameworks.