Skip to content

Instantly share code, notes, and snippets.

@wdalmut
Created June 20, 2012 14:05
Show Gist options
  • Save wdalmut/2960064 to your computer and use it in GitHub Desktop.
Save wdalmut/2960064 to your computer and use it in GitHub Desktop.
Simple View... Based on idea that I found on the web...
.project
.buildpath
.settings
<?php
require_once 'SView.php';
$view = new SView();
$view->users = array(
array(
"name" => "Mike"
),
array(
"name" => "John"
)
);
echo $view->render("users.phtml");
<?php
class SView {
private $_path;
private $_data = array();
private $_dataView = array();
public function __set($key, $value)
{
$this->_data[$key] = $value;
}
public function __get($key)
{
if(isset($this->_dataView[$key])) {
return $this->_dataView[$key];
}
else if(isset($this->_data[$key])) {
return $this->_data[$key];
}
else {
return false;
}
}
public function setViewPath($path)
{
if (!is_dir($path)) {
throw new Exception("View path {$path} must be a directory");
}
$this->_path = $path;
}
public function render($filename, $data = false)
{
if($data) {
$this->_dataView = $data;
}
if(!$this->_path) {
$this->setViewPath(dirname(__FILE__));
}
$filename = $this->_path . "/" . $filename ;
if (!file_exists($filename)) {
throw new Exception("Unable to get view at path: {$filename}");
}
$rendered = "";
ob_start();
require($filename);
$rendered = ob_get_contents();
ob_end_clean();
return $rendered;
}
}
<ul>
<li>
<strong>Users list</strong>
<ul>
<?php foreach($this->users AS $user): ?>
<li><?php echo $user["name"] ?></li>
<?php endforeach; ?>
</ul>
</li>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment