Jeff PHP framework  0.99
Modular, extensible, OOP, MVC, lightweight php framework designed to ease the programmers in the development of web applications.
view.class.php
Go to the documentation of this file.
00001 <?php
00026 class view {
00027         
00031         protected $_data;
00032         
00036         protected $_registry;
00037 
00041         protected $_assets;
00042 
00046         protected $_view_folder;
00047 
00051         protected $_dft_view_folder;
00052 
00056         protected $_css_folder;
00057 
00061         protected $_dft_css_folder;
00062 
00070         function __construct() {
00071 
00072                 $this->_data = new stdClass();
00073                 $this->_registry = registry::instance();
00074                 $this->_view_folder = $this->_registry->theme->viewPath();
00075                 $this->_css_folder = $this->_registry->theme->cssPath();
00076                 $this->_dft_view_folder = $this->_registry->theme->dftViewPath();
00077                 $this->_dft_css_folder = $this->_registry->theme->dftCssPath();
00078         }
00079 
00092         public function setTpl($tpl, $opts=null) {
00093 
00094                 if(is_readable($tpl.".php")) $this->_tpl = $tpl.".php";
00095                 elseif(is_readable($this->_view_folder.DS.$tpl.".php")) $this->_tpl = $this->_view_folder.DS.$tpl.".php";
00096                 elseif(is_readable($this->_dft_view_folder.DS.$tpl.".php")) $this->_tpl = $this->_dft_view_folder.DS.$tpl.".php";
00097                 else Error::syserrorMessage('view', 'setTpl', sprintf(__("CantChargeTemplateError"), $tpl.".php"), __LINE__);
00098 
00099                 if(gOpt($opts, 'css') && is_readable($this->_css_folder.DS.gOpt($opts, 'css').".css"))
00100                         $this->_registry->addCss(relativePath($this->_css_folder).'/'.gOpt($opts, 'css').'.css');
00101                 elseif(gOpt($opts, 'css') && is_readable($this->_dft_css_folder.DS.gOpt($opts, 'css').".css"))
00102                         $this->_registry->addCss(relativePath($this->_dft_css_folder).'/'.gOpt($opts, 'css').'.css');
00103 
00104         }
00105 
00112         public function setAssets($assets) {
00113                 $this->_assets = $assets;
00114         }
00115 
00125         public function assign($name, $value) {
00126                 $this->_data->$name = $value;
00127         }
00128 
00134         public function render() {
00135 
00136                 $buffer = '';
00137                 if(count($this->_assets)) 
00138                         foreach($this->_assets as $path=>$type) 
00139                                 $buffer = $this->asset($path, $type);
00140 
00141                 foreach($this->_data as $k=>$v) $$k=$v;
00142 
00143                 ob_start();
00144                 include($this->_tpl);
00145                 $buffer .= ob_get_contents();
00146                 ob_clean();
00147 
00148                 return $buffer;
00149 
00150         }
00151 
00159         protected function asset($path, $type) {
00160         
00161                 $tag = $type=='css' ? "link" : "script";
00162                 $method = $type=='css' ? "css" : "javascript";
00163                 $id = md5($path);
00164                 
00165                 $buffer = '';
00166 
00167                 if(is_readable($path)) {
00168                         $buffer = "<script type=\"text/javascript\">\n";
00169                         $buffer .= "if(typeof $$('".$tag."[id=$id]')[0] == undefined || $$('".$tag."[id=$id]')[0] == null) new Asset.".$method."('".relativePath($path)."', {id: '".$id."'});";
00170                         $buffer .= "</script>";
00171                 }
00172 
00173                 return $buffer;
00174         
00175         }
00176 }
00177 
00178 ?>