Jeff PHP framework  0.99
Modular, extensible, OOP, MVC, lightweight php framework designed to ease the programmers in the development of web applications.
template.class.php
Go to the documentation of this file.
00001 <?php
00021 class template {
00022 
00026         protected $_registry;
00027         
00031         protected $_path;
00032         
00036         protected $_mdl_url_content;
00037         
00041         protected $_modules;
00042 
00049         function __construct($tpl_path) {
00050         
00051                 $this->_registry = registry::instance();
00052                 $this->_path = $tpl_path;
00053         }
00054 
00060         public function getPath() {
00061 
00062                 return $this->_path;
00063 
00064         }
00065         
00073         public function parse() {
00074                 
00075                 if(!is_readable($this->getPath()))
00076                         Error::syserrorMessage('template', 'parse', sprintf(__("TplNotFound"), $this->getTplPath()), __LINE__);
00077 
00078                 $tplContent = file_get_contents($this->getPath());
00079 
00080                 /* charge/exec url module-method content */
00081                 if(preg_match("#\{module:url_module method:url_method\}#", $tplContent))
00082                         $this->_mdl_url_content = $this->_registry->router->loader(null);
00083                 
00084                 /* precharge modules methods so that registry properties are setted*/
00085                 $regexp = "/\{module:(\w+) method:(\w+)(\sparams:(\w+))?\}/";
00086                 $buffer = preg_replace_callback($regexp, array($this, 'chargeModules'), $tplContent);
00087 
00088                 /* parse template sobstituting variables charged in the registry */
00089                 $regexp = "/\{([A-Z_]+)\}/";
00090                 $buffer = preg_replace_callback($regexp, array($this, 'parseVariables'), $buffer);
00091 
00092                 /* insert modules methods contents */   
00093                 $regexp = "/\{module:(\w+) method:(\w+)(\sparams:(\w+))?\}/";
00094                 $buffer = preg_replace_callback($regexp, array($this, 'parseModules'), $buffer);
00095 
00096                 return $buffer;
00097         
00098         }
00099 
00106         protected function parseVariables($matches) {
00107                 
00108                 $m = $matches[1];
00109 
00110                 if($m == 'TITLE') return $this->_registry->title;
00111                 elseif($m == 'DESCRIPTION') return $this->_registry->description;
00112                 elseif($m == 'LANGUAGE') return $this->_registry->language;
00113                 elseif($m == 'KEYWORDS') return $this->_registry->keywords;
00114                 elseif($m == 'FAVICON') return $this->_registry->favicon;
00115                 elseif($m == 'META') {
00116                         $r = '';
00117                         foreach($this->_registry->meta as $meta) { 
00118                                 $r .= "<meta"
00119                                         .(isset($meta['name']) ? " name=\"".$meta['name']."\"" : '')
00120                                         .(isset($meta['property']) ? " property=\"".$meta['property']."\"" : '')
00121                                         ." content=\"".$meta['content']."\" />\n";
00122                         }
00123                         return $r;
00124                 }
00125                 elseif($m == 'CSS') {
00126                         $r = '';
00127                         foreach(array_unique($this->_registry->css) as $css) 
00128                                 $r .= "<link rel=\"stylesheet\" href=\"$css\" type=\"text/css\" />\n";
00129                         return $r;
00130                 }
00131                 elseif($m == 'HEAD_LINKS') {
00132                         $r = '';
00133                         foreach($this->_registry->head_links as $hlink) { 
00134                                 $r .= "<link"
00135                                         .(isset($hlink['rel']) ? " rel=\"".$hlink['rel']."\"" : '')
00136                                         .(isset($hlink['type']) ? " type=\"".$hlink['type']."\"" : '')
00137                                         .(isset($hlink['title']) ? " title=\"".$hlink['title']."\"" : '')
00138                                         ." href=\"".$hlink['href']."\" />\n";
00139                         }
00140                         return $r;
00141                 }
00142                 elseif($m == 'JAVASCRIPT') {
00143                         $r = '';
00144                         foreach(array_unique($this->_registry->js) as $js) 
00145                                 $r .= "<script type=\"text/javascript\" src=\"$js\"></script>\n";
00146                         return $r;
00147                 }
00148 
00149                 elseif($m == 'ERRORS') return document::errorMessages();
00150 
00151         }
00152 
00159         protected function parseModules($matches) {
00160                 
00161                 return ($matches[1]=='url_module' && $matches[2]=='url_method')
00162                         ? $this->_mdl_url_content
00163                         : $this->_modules[$matches[1]."-".$matches[2]."-".(isset($matches[4]) ? $matches[4] : '')];
00164 
00165 
00166         }
00167 
00174         protected function chargeModules($matches) {
00175                 
00176                 $params = isset($matches[4]) ? $matches[4] : null;
00177                 if(!($matches[1]=='url_module' && $matches[2]=='url_method')) 
00178                         $this->_modules[$matches[1]."-".$matches[2]."-".$params] = $this->_registry->router->loader(array("module"=>$matches[1], "method"=>$matches[2], "params"=>$params));
00179                 return $matches[0];
00180 
00181         }
00182 
00183 }
00184 
00185 ?>