Created
June 20, 2012 14:05
-
-
Save wdalmut/2960064 to your computer and use it in GitHub Desktop.
Simple View... Based on idea that I found on the web...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.project | |
.buildpath | |
.settings |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'SView.php'; | |
$view = new SView(); | |
$view->users = array( | |
array( | |
"name" => "Mike" | |
), | |
array( | |
"name" => "John" | |
) | |
); | |
echo $view->render("users.phtml"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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