Zend Framework style View object
Below you'll find a Zend Framework styled View object in a very minimal format. I originally wrote this object in 2004 for use in the original Arizona PHP Guru website.
This object is a few years old, it's just one of many I have sitting around, and has been chosen to demonstrate the source view options I've configured.
Using this object is very easy. First you implement something similar to the following code in your PHP application:
Then you'd implement something like the following inside of your views/common/sidebar.phtml template.
And finally, the object itself:
<?php
function draw_sidebar() {
require_once('View.php');
$view = new View('common/sidebar'); // Let's load a sidebar element
$view->elements = array(
'Home' => '/home',
'Images' => '/images',
'Blog' => '/blog',
'Reviews' => '/blog/reviews'
);
return $view->render();
}
?>
function draw_sidebar() {
require_once('View.php');
$view = new View('common/sidebar'); // Let's load a sidebar element
$view->elements = array(
'Home' => '/home',
'Images' => '/images',
'Blog' => '/blog',
'Reviews' => '/blog/reviews'
);
return $view->render();
}
?>
<div id="sidebar">
<ul>
<?php foreach($this->elements as $text => $link): ?>
<li><a href="<?php echo $link; ?>"><?php echo $text; ?></a></li>
<?php endforeach; ?>
</ul>
</div>
<ul>
<?php foreach($this->elements as $text => $link): ?>
<li><a href="<?php echo $link; ?>"><?php echo $text; ?></a></li>
<?php endforeach; ?>
</ul>
</div>
<?php
/**
* Zend style view object
* --------------------------
* Usage:
* $view = new View('view/path');
* $view->var = 'foo';
* echo $view->render();
*/
if(!defined('VIEW_PATH')) { define('VIEW_PATH', dirname(__FILE__) . '/views'); }
class View {
public $controller, $view, $filename, $status;
function __construct($view) {
$this->view = $view;
$this->filename = VIEW_PATH . DIRECTORY_SEPARATOR . $this->controller . DIRECTORY_SEPARATOR . $this->view . '.phtml';
if(!file_exists($this->filename)) {
throw new Exception('Unable to locate view at "' . $this->filename . '"');
$this->status = false;
} else {
$this->status = true;
}
}
function render($return=true) {
ob_start();
require_once($this->filename);
$result = ob_get_clean();
if($return) {
return $result;
} else {
echo $result;
}
}
}
?>
/**
* Zend style view object
* --------------------------
* Usage:
* $view = new View('view/path');
* $view->var = 'foo';
* echo $view->render();
*/
if(!defined('VIEW_PATH')) { define('VIEW_PATH', dirname(__FILE__) . '/views'); }
class View {
public $controller, $view, $filename, $status;
function __construct($view) {
$this->view = $view;
$this->filename = VIEW_PATH . DIRECTORY_SEPARATOR . $this->controller . DIRECTORY_SEPARATOR . $this->view . '.phtml';
if(!file_exists($this->filename)) {
throw new Exception('Unable to locate view at "' . $this->filename . '"');
$this->status = false;
} else {
$this->status = true;
}
}
function render($return=true) {
ob_start();
require_once($this->filename);
$result = ob_get_clean();
if($return) {
return $result;
} else {
echo $result;
}
}
}
?>
Category:
PHP
Download: