Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
html.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage View
5  *
6  * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
7  * @license GNU General Public License version 2 or later; see LICENSE
8  */
9 
10 defined('JPATH_PLATFORM') or die;
11 
12 jimport('joomla.filesystem.path');
13 
14 /**
15  * Joomla Platform HTML View Class
16  *
17  * @package Joomla.Platform
18  * @subpackage View
19  * @since 12.1
20  */
21 abstract class JViewHtml extends JViewBase
22 {
23  /**
24  * The view layout.
25  *
26  * @var string
27  * @since 12.1
28  */
29  protected $layout = 'default';
30 
31  /**
32  * The paths queue.
33  *
34  * @var SplPriorityQueue
35  * @since 12.1
36  */
37  protected $paths;
38 
39  /**
40  * Method to instantiate the view.
41  *
42  * @param JModel $model The model object.
43  * @param SplPriorityQueue $paths The paths queue.
44  *
45  * @since 12.1
46  */
47  public function __construct(JModel $model, SplPriorityQueue $paths = null)
48  {
49  parent::__construct($model);
50 
51  // Setup dependencies.
52  $this->paths = isset($paths) ? $paths : $this->loadPaths();
53  }
54 
55  /**
56  * Magic toString method that is a proxy for the render method.
57  *
58  * @return string
59  *
60  * @since 12.1
61  */
62  public function __toString()
63  {
64  return $this->render();
65  }
66 
67  /**
68  * Method to escape output.
69  *
70  * @param string $output The output to escape.
71  *
72  * @return string The escaped output.
73  *
74  * @see JView::escape()
75  * @since 12.1
76  */
77  public function escape($output)
78  {
79  // Escape the output.
80  return htmlspecialchars($output, ENT_COMPAT, 'UTF-8');
81  }
82 
83  /**
84  * Method to get the view layout.
85  *
86  * @return string The layout name.
87  *
88  * @since 12.1
89  */
90  public function getLayout()
91  {
92  return $this->layout;
93  }
94 
95  /**
96  * Method to get the layout path.
97  *
98  * @param string $layout The layout name.
99  *
100  * @return mixed The layout file name if found, false otherwise.
101  *
102  * @since 12.1
103  */
104  public function getPath($layout)
105  {
106  // Get the layout file name.
107  $file = JPath::clean($layout . '.php');
108 
109  // Find the layout file path.
110  $path = JPath::find(clone($this->paths), $file);
111 
112  return $path;
113  }
114 
115  /**
116  * Method to get the view paths.
117  *
118  * @return SplPriorityQueue The paths queue.
119  *
120  * @since 12.1
121  */
122  public function getPaths()
123  {
124  return $this->paths;
125  }
126 
127  /**
128  * Method to render the view.
129  *
130  * @return string The rendered view.
131  *
132  * @since 12.1
133  * @throws RuntimeException
134  */
135  public function render()
136  {
137  // Get the layout path.
138  $path = $this->getPath($this->getLayout());
139 
140  // Check if the layout path was found.
141  if (!$path)
142  {
143  throw new RuntimeException('Layout Path Not Found');
144  }
145 
146  // Start an output buffer.
147  ob_start();
148 
149  // Load the layout.
150  include $path;
151 
152  // Get the layout contents.
153  $output = ob_get_clean();
154 
155  return $output;
156  }
157 
158  /**
159  * Method to set the view layout.
160  *
161  * @param string $layout The layout name.
162  *
163  * @return JViewHtml Method supports chaining.
164  *
165  * @since 12.1
166  */
167  public function setLayout($layout)
168  {
169  $this->layout = $layout;
170 
171  return $this;
172  }
173 
174  /**
175  * Method to set the view paths.
176  *
177  * @param SplPriorityQueue $paths The paths queue.
178  *
179  * @return JViewHtml Method supports chaining.
180  *
181  * @since 12.1
182  */
183  public function setPaths(SplPriorityQueue $paths)
184  {
185  $this->paths = $paths;
186 
187  return $this;
188  }
189 
190  /**
191  * Method to load the paths queue.
192  *
193  * @return SplPriorityQueue The paths queue.
194  *
195  * @since 12.1
196  */
197  protected function loadPaths()
198  {
199  return new SplPriorityQueue;
200  }
201 }