00001 <?php
00010 class Template
00011 {
00012 var $variables;
00013 var $module_name;
00014
00023 function Template($module_name='')
00024 {
00025 $this->variables = array();
00026 $this->module_name = $module_name;
00027
00028
00029 foreach(explode(' ', HELPERS) as $p) {
00030 $this->import_helper($p);
00031 }
00032 }
00033
00039 function set_module($name)
00040 {
00041 $this->module_name = $name;
00042 }
00043
00049 function &import_helper($name)
00050 {
00051 foreach(func_get_args() as $name) {
00052 $class =& Factory::plugin($name, 'template');
00053 if($class === false) {
00054 trigger_error("Helper $name does not exist");
00055 die;
00056 }
00057 }
00058 if($class) return $class;
00059 }
00060
00067 function set($name, $value)
00068 {
00069 if(is_array($name)) {
00070
00071 reset($value);
00072 foreach($name as $k) {
00073 $this->variables[$k] = current($value);
00074 next($value);
00075 }
00076 } else {
00077 $this->variables[$name] = $value;
00078 }
00079 }
00080
00087 function &get($name)
00088 {
00089 return $this->variables[$name];
00090 }
00091
00098 function is_set($name)
00099 {
00100 return isset($this->variables[$name]);
00101 }
00102
00108 function un_set($name)
00109 {
00110 unset($this->variables[$name]);
00111 }
00112
00121 function fetch($filename, $vars=array(), $language=false)
00122 {
00123 if(!is_array($vars)) $vars = array();
00124
00125 $helpers =& Registry::get('pronto:helpers', array());
00126
00127 $helpers_arr = array();
00128 foreach($helpers as $k=>$v) {
00129 $helpers_arr[$k] = $v;
00130 }
00131
00132 $vars = array_merge($this->variables, $vars, $helpers_arr);
00133
00134 $GLOBALS['PLUGINS'] = $helpers_arr;
00135 $GLOBALS['HELPERS'] = $helpers_arr;
00136
00137 if(substr($filename, 0, 1) == DS) {
00138
00139 $template_path = $filename;
00140 } else {
00141
00142 if(!empty($this->module_name)) {
00143 $template_path = DIR_FS_APP.DS.'modules'.DS.$this->module_name.DS.'templates'.DS.$filename;
00144 } else {
00145 $template_path = DIR_FS_APP.DS.'templates'.DS.$filename;
00146 }
00147 }
00148 $vars['__template_path'] = dirname($template_path);
00149
00150 $i18n = Registry::get('pronto:i18n');
00151 $old = ($language && $i18n) ? $i18n->set_language($language) : '';
00152 ob_start();
00153 extract($vars, EXTR_OVERWRITE);
00154 require $template_path;
00155 $content = ob_get_contents();
00156 ob_end_clean();
00157
00158
00159 if($old) $i18n->set_language($old);
00160 unset($GLOBALS['PLUGINS'], $GLOBALS['HELPERS']);
00161
00162 return $content;
00163 }
00164
00165 }
00166
00167 ?>