path = ($path ? $path : $tpl_path); $this->filename = $filename; //$this->set_parent($parent); $this->init(); } function init() { $this->vars = array(); $this->templates = array(); } /** * Set the path to the template files. * * @param string $path path to template files * * @return void */ function set_path($path) { $this->path = $path; } /** * Set a template variable. * * @param string $name name of the variable to set * @param mixed $value the value of the variable * * @return void */ function set($name, $value) { $this->vars[$name] = $value; } function set_glue($name, $value) { $this->glues[$name] = $value; } /** * Set a bunch of variables at once using an associative array. * * @param array $vars array of vars to set * @param bool $clear whether to completely overwrite the existing vars * * @return void */ function set_vars($vars, $clear = false) { if($clear) { $this->vars = $vars; } else { if(is_array($vars)) $this->vars = array_merge($this->vars, $vars); } } /** * Open, parse, and return the template file. * * @param string string the template file name * * @return string */ function fetch($file_name) { if (isset($this->over_template)) { return $this->over_template->render(); } if (!isset($file_name)) return ''; foreach ($this->templates as $name => $tpl) { if (is_array($tpl)) { $glue = (isset($this->glues[$name]) ? $this->glues[$name] : ""); foreach ($tpl as $subtpl) { $rendered[] = $subtpl->render(); $this->vars[$name] = implode($glue, $rendered); } } else { $this->vars[$name] = $tpl->render(); } } extract($this->vars); // Extract the vars to local namespace ob_start(); // Start output buffering include($this->path . $file_name); // Include the file $contents = ob_get_contents(); // Get the contents of the buffer ob_end_clean(); // End buffering and discard //kui templeidis leidusid muutujad, mida pole defineeritud, //tuleks neile anda vaikimisi tühjad väärtused. //if() //kuidas saaks kontrollida, kas oli mingi warning väärtustamata muutuja kohta? //tegelt ei tee praegu, kirjutan tingimused templat'i return $contents; // Return the contents } function render() { return $this->fetch($this->filename); } /** */ function set_tplfile($filename) { $this->filename = $filename; } /** * Vaikimisi template fail - juhul kui pole juba väärtustatud * */ function def_tplfile($filename) { if (!isset($this->filename)) $this->filename = $filename; } /** * Template :: set_parent() * * Vajadusel võivad järglased siin teostada mingeid tegevusi, * kui on näiteks mingi inf vaja parentile edasi anda, * või ennast kohandada kuidagi parentiga. */ function set_parent(&$parent) { $this->parent =& $parent; } function set_template($name, &$template) { $this->templates[$name] =& $template; $template->set_parent($this); } function add_template($name, &$template) { if (isset($this->templates[$name])) { if (is_array($this->templates[$name])) { //$this->templates[$name][] = $template; $this->templates[$name][] =& $template; //$template->set_parent($this);//miks see siin on??? } else { $newarr = array(); $newarr[] =& $this->templates[$name]; $newarr[] =& $template; $this->templates[$name] =& $newarr; //$this->templates[$name] = array(&$this->templates[$name], &$template); } $template->set_parent($this); } else $this->set_template($name, $template); } function set_over_template(&$template) { $this->over_template =& $template; } } ?>