Subversion Repositories PHPX

Rev

Rev 64 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 64 Rev 70
Line 30... Line 30...
30
   * @var array
30
   * @var array
31
   */
31
   */
32
  protected $_template_vars = array();
32
  protected $_template_vars = array();
33
33
34
  /**
34
  /**
-
 
35
   * Stylesheets to be inserted into the <code>head</code> element
-
 
36
   *
-
 
37
   * @var array
-
 
38
   */
-
 
39
  protected $_stylesheets = array();
-
 
40
-
 
41
  /**
-
 
42
   * Scripts to be inserted into the <code>head</code> or
-
 
43
   * <code>body</code> element
-
 
44
   *
-
 
45
   * @var array
-
 
46
   */
-
 
47
  protected $_scripts = array();
-
 
48
-
 
49
  /**
35
   * Creates a new view
50
   * Creates a new view
36
   *
51
   *
37
   * @param string $template
52
   * @param string $template
38
   *   Template resource path
53
   *   Template resource path
39
   */
54
   */
40
  public function __construct($template)
55
  public function __construct ($template)
41
  {
56
  {
42
    $this->_template = $template;
57
    $this->_template = $template;
43
  }
58
  }
44
59
45
  /**
60
  /**
Line 48... Line 63...
48
   * @param string $name
63
   * @param string $name
49
   *   Variable name
64
   *   Variable name
50
   * @param mixed $value
65
   * @param mixed $value
51
   *   Variable value
66
   *   Variable value
52
   */
67
   */
53
  public function __set($name, $value)
68
  public function __set ($name, $value)
54
  {
69
  {
55
    $this->_template_vars[$name] = $value;
70
    $this->_template_vars[$name] = $value;
56
  }
71
  }
57
72
58
  /**
73
  /**
59
   * Magic getter method used for retrieving values of template variables
74
   * Magic getter method used for retrieving values of template variables
60
   *
75
   *
61
   * @param string $name
76
   * @param string $name
62
   *   Variable name
77
   *   Variable name
63
   */
78
   */
64
  public function __get($name)
79
  public function __get ($name)
65
  {
80
  {
66
    return $this->_template_vars[$name];
81
    return $this->_template_vars[$name];
67
  }
82
  }
68
83
69
  /**
84
  /**
Line 73... Line 88...
73
   * reference (as required primarily in HTML for attribute values and element
88
   * reference (as required primarily in HTML for attribute values and element
74
   * content).
89
   * content).
75
   *
90
   *
76
   * @param mixed $value
91
   * @param mixed $value
77
   */
92
   */
78
  public function escape($value)
93
  public function escape ($value)
79
  {
94
  {
80
    if (is_array($value))
95
    if (is_array($value))
81
    {
96
    {
82
      return array_map(array('self', 'escape'), $value);
97
      return array_map(array('self', 'escape'), $value);
83
    }
98
    }
Line 121... Line 136...
121
   *   in <var>$value</var> with their HTML entity references.  The default is
136
   *   in <var>$value</var> with their HTML entity references.  The default is
122
   *   <code>false</code>.
137
   *   <code>false</code>.
123
   * @return mixed The assigned value (after possible HTML encoding)
138
   * @return mixed The assigned value (after possible HTML encoding)
124
   * @see View::escape()
139
   * @see View::escape()
125
   */
140
   */
126
  public function assign($name, $value, $escape = false)
141
  public function assign ($name, $value, $escape = false)
127
  {
142
  {
128
    if ($escape)
143
    if ($escape)
129
    {
144
    {
130
      $value = $this->escape($value);
145
      $value = $this->escape($value);
131
    }
146
    }
Line 133... Line 148...
133
    $this->$name = $value;
148
    $this->$name = $value;
134
    return $value;
149
    return $value;
135
  }
150
  }
136
151
137
  /**
152
  /**
-
 
153
   * Adds a CSS resource (stylesheet) to the list of external
-
 
154
   * stylesheets
-
 
155
   *
-
 
156
   * @param string $uri
-
 
157
   *   Stylesheet URI
-
 
158
   * @param mixed $key (optional)
-
 
159
   *   Array key for the script.  May be used later to exclude
-
 
160
   *   or include the code for a specific stylesheet.
-
 
161
   */
-
 
162
  public function addStylesheet ($uri, $key = null)
-
 
163
  {
-
 
164
    $stylesheets =& $this->_stylesheets;
-
 
165
-
 
166
    if ($key !== null)
-
 
167
    {
-
 
168
      $stylesheets[$key] = $uri;
-
 
169
    }
-
 
170
    else
-
 
171
    {
-
 
172
      $stylesheets[] = $uri;
-
 
173
    }
-
 
174
  }
-
 
175
-
 
176
  /**
-
 
177
   * Adds an ECMAScript resource (script) to the list of external
-
 
178
   * scripts
-
 
179
   *
-
 
180
   * @param string $uri
-
 
181
   *   Script URI
-
 
182
   * @param mixed $key (optional)
-
 
183
   *   Array key for the script.  May be used later to exclude
-
 
184
   *   or include the code for a specific script.
-
 
185
   */
-
 
186
  public function addScript ($uri, $key = null)
-
 
187
  {
-
 
188
    $scripts =& $this->_scripts;
-
 
189
-
 
190
    if ($key !== null)
-
 
191
    {
-
 
192
      $scripts[$key] = $uri;
-
 
193
    }
-
 
194
    else
-
 
195
    {
-
 
196
      $scripts[] = $uri;
-
 
197
    }
-
 
198
  }
-
 
199
-
 
200
  /**
138
   * Renders the view by including a template
201
   * Renders the view by including a template
139
   *
202
   *
140
   * @param string $template
203
   * @param string $template
141
   *   Optional alternative template resource path.
204
   *   Optional alternative template resource path.
142
   *   If not provided, the default template ($template property) will be used.
205
   *   If not provided, the default template ($template property) will be used.
143
   * @throws Exception if no template has been defined before
206
   * @throws Exception if no template has been defined before
144
   */
207
   */
145
  public function render($template = null, $content = null)
208
  public function render ($template = null, $content = null)
146
  {
209
  {
147
        if (!is_null($content))
210
        if (!is_null($content))
148
    {
211
    {
149
      ob_start();
212
      ob_start();
150
        require_once $content;
213
        require_once $content;
Line 165... Line 228...
165
      throw new \Exception('No template defined');
228
      throw new \Exception('No template defined');
166
    }
229
    }
167
  }
230
  }
168
231
169
  /**
232
  /**
-
 
233
   * Returns the code for including all stylesheets,
-
 
234
   * with exclusions.
-
 
235
   *
-
 
236
   * @param array $exclusions (optional, future)
-
 
237
   *   The keys of the stylesheets that should be excluded
-
 
238
   * @return string
-
 
239
   */
-
 
240
  public function getStylesheets ($exclusions = array())
-
 
241
  {
-
 
242
    return (
-
 
243
      implode(PHP_EOL, array_map(function ($uri) {
-
 
244
        return '<link rel="stylesheet" href="' . $this->escape($uri) . '">';
-
 
245
      }, array_diff_key($this->_stylesheets, array_flip($exclusions))))
-
 
246
      . PHP_EOL);
-
 
247
  }
-
 
248
-
 
249
  /**
-
 
250
   * Returns the code for including a specific stylesheet
-
 
251
   *
-
 
252
   * @param mixed $key
-
 
253
   * @return string
-
 
254
   */
-
 
255
  public function getStylesheet ($key)
-
 
256
  {
-
 
257
    return '<link rel="stylesheet" href="'
-
 
258
      . $this->escape($this->_stylesheets[$key])
-
 
259
      . '">' . PHP_EOL;
-
 
260
  }
-
 
261
-
 
262
  /**
-
 
263
   * Returns the code for including all stylesheets,
-
 
264
   * with exclusions.
-
 
265
   *
-
 
266
   * @param array $exclusions (optional, future)
-
 
267
   *   The keys of the scripts that should be excluded.
-
 
268
   *   Usually you would specify those scripts that you
-
 
269
   *   want to include with {@link #getScript()}.
-
 
270
   * @return string
-
 
271
   */
-
 
272
  public function getScripts ($exclusions = array())
-
 
273
  {
-
 
274
    return (
-
 
275
     implode(PHP_EOL, array_map(function ($uri) {
-
 
276
        return '<script type="text/javascript" src="'
-
 
277
          . $this->escape($uri)
-
 
278
          . '"></script>';
-
 
279
      }, array_diff_key($this->_scripts, array_flip($exclusions))))
-
 
280
      . PHP_EOL);
-
 
281
  }
-
 
282
-
 
283
  public function getScript ($key)
-
 
284
  {
-
 
285
    return '<link rel="stylesheet" href="'
-
 
286
      . $this->escape($this->_scripts[$key])
-
 
287
      . '">' . PHP_EOL;
-
 
288
  }
-
 
289
-
 
290
  /**
170
   * Returns the content for insertion into the template
291
   * Returns the content for insertion into the template
171
   */
292
   */
172
  public function getContent()
293
  public function getContent ()
173
  {
294
  {
174
    return $this->_content;
295
    return $this->_content;
175
  }
296
  }
176
297
177
  /**
298
  /**
178
   * @param string[optional] $controller
299
   * @param string[optional] $controller
179
   * @param string[optional] $action
300
   * @param string[optional] $action
180
   * @param int[optional] $id
301
   * @param int[optional] $id
181
   * @see Application::getURL()
302
   * @see Application::getURL()
182
   */
303
   */
183
  public function getURL($controller = null, $action = null, $id = null)
304
  public function getURL ($controller = null, $action = null, $id = null)
184
  {
305
  {
185
    return Application::getInstance()->getURL($controller, $action, $id);
306
    return Application::getInstance()->getURL($controller, $action, $id);
186
  }
307
  }
187
}
308
}
188
309