Ticket #446 (closed Feature Request: wontfix)
Template View
| Reported by: | allain | Owned by: | - No owner - |
|---|---|---|---|
| Priority: | minor | Milestone: | 2.2 |
| Component: | Libraries | Version: | SVN HEAD |
| Keywords: | template view | Cc: |
Description
Let me start by saying I like the Template_Controller class. I've been using it for a bit now. I just find using it a little verbose, and on the conceptual level I didn't like the Controller being modified to support something that just "feels" like it should be in the view.
The following code is my attempt at an alternative way of doing things, which I think is a little cleaner.
<?php
class Template_View_Core extends View_Core {
public function __construct($view_file, $template_file='templates/default') {
parent::__construct($template_file);
$this->content_view = new View($view_file);
}
public function __set($field_name, $value) {
$this->data['content_view']->$field_name = $value;
return parent::__set($field_name, $value);
}
}
?>
And you would use it in the controller like this:
public function welcome() {
$view = new Template_View('welcome');
$view->message = 'Hello World';
$view->title = 'Welcome Page';
$view->render(TRUE);
}
With the following in application/views/templates/default.php:
<html> <head> <title><?=$title?></title> </head> <body> <h1><?=$title?></h1> <?=$content_view?> </body> </html>
And this in application/views/welcome.php:
<p>We've provided this message for you: <strong><?=$message?></strong></p>
If this has merit I think it could be extended to allow for multiple subviews (named of course) without too much extra work.
