Go to the documentation of this file.00001 <?php
00010 class tpl extends object {
00011
00017 protected $vars = array();
00018
00024 protected $responseProxy;
00025
00026 protected function afterInit() {
00027 $this->responseProxy = response::getInstance()->getProxy();
00028 }
00029
00035 public function getResponseProxy() {
00036 return $this->responseProxy;
00037 }
00038
00045 public function get($name) {
00046 return isset($this->vars[$name]) ? $this->vars[$name] : null;
00047 }
00048
00055 public function set($name, $value) {
00056 $this->vars[$name] = $value;
00057 }
00058
00065 public function setA(array $values) {
00066 $this->vars = array_merge($this->vars, $values);
00067 }
00068
00072 public function reset() {
00073 $this->vars = array();
00074 }
00075
00083 public function fetch(array $prm=array()) {
00084 $content = null;
00085 $cachedContent = false;
00086 $cachedLayout = false;
00087
00088 $oldProxy = response::getProxy();
00089 response::setProxy($this->responseProxy);
00090
00091 $cacheResp = null;
00092
00093 if ($this->cfg->cache['auto']) {
00094 $cache = cache::getInstance(array_merge(array('serialize'=>false), $this->cfg->cache));
00095 $cache->get($content, array(
00096 'id'=>$this->cfg->module.'-'.$this->cfg->action.'-'.$this->cfg->param
00097 ));
00098 $cacheResp = cache::getInstance($this->cfg->cache);
00099 $cacheResp->get($callResp, array(
00100 'id'=>$this->cfg->module.'-'.$this->cfg->action.'-'.$this->cfg->param.'-callResp'
00101 ));
00102 if (!empty($content)) {
00103 $cachedContent = true;
00104 $cachedLayout = $this->cfg->cache['layout'];
00105 if (!empty($callResp)) {
00106 $this->responseProxy->doCalls($callResp);
00107 $this->responseProxy->initCall();
00108 }
00109 }
00110 }
00111
00112 if (!$cachedContent) {
00113
00114 $action = $this->cfg->action;
00115 if (array_key_exists('callback', $prm))
00116 $action = call_user_func($prm['callback'], $prm['callbackPrm']);
00117 $file = $this->findTpl($prm, array(
00118 'module_'.$this->cfg->module.'_view_'.$action,
00119 'module_'.$this->cfg->defaultModule.'_view_'.$this->cfg->default
00120 ));
00121
00122 if (file::exists($file))
00123 $content = $this->_fetch($file);
00124 }
00125
00126 if ($this->cfg->layout && !$cachedLayout) {
00127
00128 $file = $this->findTpl($prm, array(
00129 'module_'.$this->cfg->module.'_view_'.$this->cfg->action.'Layout',
00130 'module_'.$this->cfg->module.'_view_layout'
00131 ));
00132 if (file::exists($file)) {
00133 $this->content = $content;
00134 $content = $this->_fetch($file);
00135 }
00136 if ($this->cfg->cache['auto'] && $this->cfg->cache['layout'])
00137 $cache->save();
00138 }
00139
00140 if ($cacheResp && $this->responseProxy->hasCall()) {
00141 $callResp = $this->responseProxy->getCall();
00142 $cacheResp->save();
00143 }
00144
00145 response::setProxy($oldProxy);
00146 return $content;
00147 }
00148
00155 protected function _fetch($file) {
00156 $this->set('response', $this->responseProxy);
00157 extract($this->vars, EXTR_REFS OR EXTR_OVERWRITE);
00158 ob_start();
00159 include($file);
00160 $contents = ob_get_contents();
00161 ob_end_clean();
00162 return $contents;
00163 }
00164
00172 protected function findTpl(array $prm, array $name) {
00173 foreach($name as $n) {
00174 if ($file = file::nyroExists(array_merge($prm, array('name'=>$n,'type'=>'tpl'))))
00175 return $file;
00176 }
00177 return null;
00178 }
00179
00188 public function render($prm) {
00189 if (!is_array($prm)){
00190 $tmp = explode('/', $prm);
00191 $prm = array();
00192 $prm['module'] = isset($tmp[0]) ? $tmp[0] : null;
00193 $prm['action'] = isset($tmp[1]) ? $tmp[1] : null;
00194 $prm['param'] = isset($tmp[2]) ? $tmp[2] : null;
00195 }
00196 $prm = array_merge(array('module'=>$this->cfg->module), $prm);
00197 $module = factory::getModule($prm['module'], array('render'=>true));
00198 $module->exec($prm);
00199 return $module->publish($prm);
00200 }
00201
00208 function __toString() {
00209 return $this->fetch();
00210 }
00211
00219 public function __get($name) {
00220 return $this->get($name);
00221 }
00222
00230 public function __set($name, $val) {
00231 $this->set($name, $val);
00232 }
00233
00234 }