aboutsummaryrefslogtreecommitdiff
path: root/vendor/flexi/lib/template.php
blob: b4d97e845e719081eddd8ae18fae0b9b2cca8cd2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php

# Copyright (c)  2008 - Marcus Lunzenauer <mlunzena@uos.de>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

/**
 * Abstract template class representing the presentation layer of an action.
 * Output can be customized by supplying attributes, which a template can
 * manipulate and display.
 *
 * @package   flexi
 *
 * @author    Marcus Lunzenauer (mlunzena@uos.de)
 * @copyright (c) Authors
 * @version   $Id$
 */

abstract class Flexi_Template {

  /**
   * @ignore
   */
  public $_attributes, $_factory, $_options, $_layout, $_template;


  /**
   * Constructor
   *
   * @param string                 the path of the template.
   * @param Flexi_TemplateFactory  the factory creating this template
   * @param array                  optional array of options
   *
   * @return void
   */
  function __construct($template, &$factory, $options = array()) {

    # set template
    $this->_template = $template;

    # set factory
    $this->_factory = $factory;

    # set options
    $this->_options = $options;

    # init attributes
    $this->clear_attributes();

    # set layout
    $this->set_layout(NULL);
  }


  /**
   * __set() is a magic method run when writing data to inaccessible members.
   * In this class it is used to set attributes for the template in a
   * comfortable way.
   *
   * @see http://php.net/__set
   *
   * @param  string     the name of the member field
   * @param  mixed      the value for the member field
   *
   * @return void
   */
  function __set($name, $value) {
    $this->set_attribute($name, $value);
  }


  /**
   * __get() is a magic method utilized for reading data from inaccessible
   * members.
   * In this class it is used to get attributes for the template in a
   * comfortable way.
   *
   * @see http://php.net/__set
   *
   * @param  string     the name of the member field
   *
   * @return mixed      the value for the member field
   */
  function __get($name) {
    return $this->get_attribute($name);
  }


  /**
   * __isset() is a magic method triggered by calling isset() or empty() on
   * inaccessible members.
   * In this class it is used to check for attributes for the template in a
   * comfortable way.
   *
   * @see http://php.net/__set
   *
   * @param  string     the name of the member field
   *
   * @return bool       TRUE if that attribute exists, FALSE otherwise
   */
  function __isset($name) {
    return isset($this->_attributes[$name]);
  }


  /**
   * __unset() is a magic method invoked when unset() is used on inaccessible
   * members.
   * In this class it is used to check for attributes for the template in a
   * comfortable way.
   *
   * @see http://php.net/__set
   *
   * @param  string     the name of the member field
   *
   * @return void
   */
  function __unset($name) {
    $this->clear_attribute($name);
  }


  /**
   * Parse, render and return the presentation.
   *
   * @param array  An optional associative array of attributes and their
   *               associated values.
   * @param string A name of a layout template.
   *
   * @return string A string representing the rendered presentation.
   */
  function render($attributes = null, $layout = null) {

    if (isset($layout)) {
      $this->set_layout($layout);
    }

    # merge attributes
    $this->set_attributes($attributes);

    return $this->_render();
  }


  /**
   * Parse, render and return the presentation.
   *
   * @return string A string representing the rendered presentation.
   */
  abstract function _render();


  /**
   * Returns the value of an attribute.
   *
   * @param string An attribute name.
   * @param mixed  An attribute value.
   *
   * @return mixed  An attribute value.
   */
  function get_attribute($name) {
    return isset($this->_attributes[$name]) ? $this->_attributes[$name] : NULL;
  }


  /**
   * Set an array of attributes.
   *
   * @return array An associative array of attributes and their associated
   *               values.
   */
  function get_attributes() {
    return $this->_attributes;
  }


  /**
   * Set an attribute.
   *
   * @param string An attribute name.
   * @param mixed  An attribute value.
   *
   * @return void
   */
  function set_attribute($name, $value) {
    $this->_attributes[$name] = $value;
  }


  /**
   * Set an array of attributes.
   *
   * @param array An associative array of attributes and their associated
   *              values.
   *
   * @return void
   */
  function set_attributes($attributes) {
    $this->_attributes = (array)$attributes + (array)$this->_attributes;
  }


  /**
   * Clear all attributes associated with this template.
   *
   * @return void
   */
  function clear_attributes() {
    $this->_attributes = array();
  }


  /**
   * Clear an attribute associated with this template.
   *
   * @param string The name of the attribute to be cleared.
   *
   * @return void
   */
  function clear_attribute($name) {
    unset($this->_attributes[$name]);
  }


  /**
   * Set the template's layout.
   *
   * @param mixed A name of a layout template or a layout template.
   *
   * @return void
   */
  function set_layout($layout) {
    $this->_layout = $this->_factory->open($layout);
  }
}