SHOW:
|
|
- or go back to the newest paste.
| 1 | define('DR', realpath(dirname(__FILE__)).DS);
| |
| 2 | ||
| 3 | class View | |
| 4 | {
| |
| 5 | # view file path | |
| 6 | private $file; | |
| 7 | ||
| 8 | # view local variables | |
| 9 | private $data = array(); | |
| 10 | ||
| 11 | # view rendered content | |
| 12 | private $content; | |
| 13 | ||
| 14 | # view global variables | |
| 15 | private static $_data = array(); | |
| 16 | ||
| 17 | private function __construct($name) | |
| 18 | {
| |
| 19 | $name = str_replace(array('/', '//', '\\'), DIRECTORY_SEPARATOR, $name);
| |
| 20 | $file = DR.'views'.DS.$name.EXT; | |
| 21 | ||
| 22 | if (file_exists($file)) | |
| 23 | {
| |
| 24 | $this->file = $file; | |
| 25 | } | |
| 26 | ||
| 27 | return true; | |
| 28 | } | |
| 29 | ||
| 30 | public static function factory($name = '') | |
| 31 | {
| |
| 32 | if (empty($name)) | |
| 33 | {
| |
| 34 | return false; | |
| 35 | } | |
| 36 | ||
| 37 | return new view($name); | |
| 38 | } | |
| 39 | ||
| 40 | public function __set($key = '', $value = null) | |
| 41 | {
| |
| 42 | if (empty($key)) | |
| 43 | {
| |
| 44 | return false; | |
| 45 | } | |
| 46 | ||
| 47 | $this->data[$key] = $value; | |
| 48 | ||
| 49 | return true; | |
| 50 | } | |
| 51 | ||
| 52 | public function __get($key = '') | |
| 53 | {
| |
| 54 | if (empty($key)) | |
| 55 | {
| |
| 56 | return false; | |
| 57 | } | |
| 58 | ||
| 59 | if (array_key_exists($key, $this->data)) | |
| 60 | {
| |
| 61 | return $this->data[$key]; | |
| 62 | } | |
| 63 | else | |
| 64 | {
| |
| 65 | return false; | |
| 66 | } | |
| 67 | } | |
| 68 | ||
| 69 | public function bind($key = '', $value = null) | |
| 70 | {
| |
| 71 | if (empty($key)) | |
| 72 | {
| |
| 73 | return false; | |
| 74 | } | |
| 75 | ||
| 76 | view::$_data[$key] = $value; | |
| 77 | ||
| 78 | return true; | |
| 79 | } | |
| 80 | ||
| 81 | public function render() | |
| 82 | {
| |
| 83 | if (!empty(view::$_data)) | |
| 84 | {
| |
| 85 | extract(view::$_data); | |
| 86 | } | |
| 87 | ||
| 88 | extract($this->data); | |
| 89 | ||
| 90 | ob_start(); | |
| 91 | ||
| 92 | include($this->file); | |
| 93 | ||
| 94 | $this->content = ob_get_contents(); | |
| 95 | ||
| 96 | ob_end_clean(); | |
| 97 | ||
| 98 | return (string) $this->content; | |
| 99 | } | |
| 100 | ||
| 101 | public function __toString() | |
| 102 | {
| |
| 103 | return $this->render(); | |
| 104 | } |