| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Copyright 2003, Paul James |
| 5 | * |
| 6 | * This file contains some methods from the Smarty templating engine version |
| 7 | * 2.5.0 by Monte Ohrt <monte@ispi.net> and Andrei Zmievski <andrei@php.net>. |
| 8 | * |
| 9 | * The SquirrelMail (Foowd) template implementation. |
| 10 | * Derived from the foowd template implementation and adapted |
| 11 | * for squirrelmail |
| 12 | * @copyright © 2005-2006 The SquirrelMail Project Team |
| 13 | * @license http://opensource.org/licenses/gpl-license.php GNU Public License |
| 14 | * @version $Id$ |
| 15 | * @package squirrelmail |
| 16 | */ |
| 17 | |
| 18 | /** |
| 19 | * The SquirrelMail (Foowd) template class. |
| 20 | * |
| 21 | * Basic template class for capturing values and pluging them into a template. |
| 22 | * This class uses a similar API to Smarty. |
| 23 | * |
| 24 | * @author Paul James |
| 25 | * @author Monte Ohrt <monte at ispi.net> |
| 26 | * @author Andrei Zmievski <andrei at php.net> |
| 27 | * @package squirrelmail |
| 28 | */ |
| 29 | class Template |
| 30 | { |
| 31 | /** |
| 32 | * The templates values array |
| 33 | * |
| 34 | * @var array |
| 35 | */ |
| 36 | var $values = array(); |
| 37 | |
| 38 | /** |
| 39 | * The template directory to use |
| 40 | * |
| 41 | * @var string |
| 42 | */ |
| 43 | var $template_dir = 'templates\default'; |
| 44 | |
| 45 | /** |
| 46 | * Constructor |
| 47 | * |
| 48 | * @param string $sTplDir where the template set is located |
| 49 | */ |
| 50 | function Template($sTplDir = 'templates\default') { |
| 51 | $this->template_dir = $sTplDir; |
| 52 | } |
| 53 | |
| 54 | |
| 55 | /** |
| 56 | * Assigns values to template variables |
| 57 | * |
| 58 | * @param array|string $tpl_var the template variable name(s) |
| 59 | * @param mixed $value the value to assign |
| 60 | */ |
| 61 | function assign($tpl_var, $value = NULL) { |
| 62 | if (is_array($tpl_var)) |
| 63 | { |
| 64 | foreach ($tpl_var as $key => $val) |
| 65 | { |
| 66 | if ($key != '') |
| 67 | $this->values[$key] = $val; |
| 68 | } |
| 69 | } |
| 70 | else |
| 71 | { |
| 72 | if ($tpl_var != '') |
| 73 | $this->values[$tpl_var] = $value; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Assigns values to template variables by reference |
| 79 | * |
| 80 | * @param string $tpl_var the template variable name |
| 81 | * @param mixed $value the referenced value to assign |
| 82 | */ |
| 83 | function assign_by_ref($tpl_var, &$value) |
| 84 | { |
| 85 | if ($tpl_var != '') |
| 86 | $this->values[$tpl_var] = &$value; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Appends values to template variables |
| 91 | * |
| 92 | * @param array|string $tpl_var the template variable name(s) |
| 93 | * @param mixed $value the value to append |
| 94 | */ |
| 95 | function append($tpl_var, $value = NULL, $merge = FALSE) |
| 96 | { |
| 97 | if (is_array($tpl_var)) |
| 98 | { |
| 99 | foreach ($tpl_var as $_key => $_val) |
| 100 | { |
| 101 | if ($_key != '') |
| 102 | { |
| 103 | if(isset($this->values[$_key]) && !is_array($this->values[$_key])) |
| 104 | settype($this->values[$_key],'array'); |
| 105 | |
| 106 | if($merge && is_array($_val)) |
| 107 | { |
| 108 | foreach($_val as $_mkey => $_mval) |
| 109 | $this->values[$_key][$_mkey] = $_mval; |
| 110 | } |
| 111 | else |
| 112 | $this->values[$_key][] = $_val; |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | else |
| 117 | { |
| 118 | if ($tpl_var != '' && isset($value)) |
| 119 | { |
| 120 | if(isset($this->values[$tpl_var]) && !is_array($this->values[$tpl_var])) |
| 121 | settype($this->values[$tpl_var],'array'); |
| 122 | |
| 123 | if($merge && is_array($value)) |
| 124 | { |
| 125 | foreach($value as $_mkey => $_mval) |
| 126 | $this->values[$tpl_var][$_mkey] = $_mval; |
| 127 | } |
| 128 | else |
| 129 | $this->values[$tpl_var][] = $value; |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Appends values to template variables by reference |
| 136 | * |
| 137 | * @param string $tpl_var the template variable name |
| 138 | * @param mixed $value the referenced value to append |
| 139 | */ |
| 140 | function append_by_ref($tpl_var, &$value, $merge = FALSE) |
| 141 | { |
| 142 | if ($tpl_var != '' && isset($value)) |
| 143 | { |
| 144 | if(!@is_array($this->values[$tpl_var])) |
| 145 | settype($this->values[$tpl_var],'array'); |
| 146 | |
| 147 | if ($merge && is_array($value)) |
| 148 | { |
| 149 | foreach($value as $_key => $_val) |
| 150 | $this->values[$tpl_var][$_key] = &$value[$_key]; |
| 151 | } |
| 152 | else |
| 153 | $this->values[$tpl_var][] = &$value; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Display the template |
| 159 | * |
| 160 | * @param string $file The template file to use |
| 161 | */ |
| 162 | function display($file) |
| 163 | { |
| 164 | $t = &$this->values; // place values array directly in scope |
| 165 | ob_start(); |
| 166 | include($this->template_dir.$file); |
| 167 | ob_end_flush(); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Return the results of applying a template. |
| 172 | * |
| 173 | * @param string $file The template file to use |
| 174 | * @return string A string of the results |
| 175 | */ |
| 176 | function fetch($file) |
| 177 | { |
| 178 | ob_start(); |
| 179 | $t = &$this->values; // place values array directly in scope |
| 180 | include($this->template_dir.$file); |
| 181 | $contents = ob_get_contents(); |
| 182 | ob_end_clean(); |
| 183 | return $contents; |
| 184 | } |
| 185 | |
| 186 | } |
| 187 | |
| 188 | ?> |