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