1 <?php
defined('SYSPATH') OR die('No direct access allowed.');
3 * Loads and displays Kohana view files. Can also handle output of some binary
4 * files, such as image, Javascript, and CSS files.
6 * $Id: View.php 3917 2009-01-21 03:06:22Z zombor $
10 * @copyright (c) 2007-2008 Kohana Team
11 * @license http://kohanaphp.com/license.html
15 // The view file name and type
16 protected $kohana_filename = FALSE;
17 protected $kohana_filetype = FALSE;
19 // View variable storage
20 protected $kohana_local_data = array();
21 protected static $kohana_global_data = array();
24 * Creates a new View using the given parameters.
26 * @param string view name
27 * @param array pre-load data
28 * @param string type of file: html, css, js, etc.
31 public static function factory($name = NULL, $data = NULL, $type = NULL)
33 return new View($name, $data, $type);
37 * Attempts to load a view and pre-load view data.
39 * @throws Kohana_Exception if the requested view cannot be found
40 * @param string view name
41 * @param array pre-load data
42 * @param string type of file: html, css, js, etc.
45 public function __construct($name = NULL, $data = NULL, $type = NULL)
47 if (is_string($name) AND $name !== '')
50 $this->set_filename($name, $type);
53 if (is_array($data) AND ! empty($data))
55 // Preload data using array_merge, to allow user extensions
56 $this->kohana_local_data
= array_merge($this->kohana_local_data
, $data);
61 * Magic method access to test for view property
63 * @param string View property to test for
66 public function __isset($key = NULL)
68 return $this->is_set($key);
72 * Sets the view filename.
75 * @param string view filename
76 * @param string view file type
79 public function set_filename($name, $type = NULL)
83 // Load the filename and set the content type
84 $this->kohana_filename
= Kohana
::find_file('views', $name, TRUE);
85 $this->kohana_filetype
= EXT
;
89 // Check if the filetype is allowed by the configuration
90 if ( ! in_array($type, Kohana
::config('view.allowed_filetypes')))
91 throw new Kohana_Exception('core.invalid_filetype', $type);
93 // Load the filename and set the content type
94 $this->kohana_filename
= Kohana
::find_file('views', $name, TRUE, $type);
95 $this->kohana_filetype
= Kohana
::config('mimes.'.$type);
97 if ($this->kohana_filetype
== NULL)
99 // Use the specified type
100 $this->kohana_filetype
= $type;
108 * Sets a view variable.
110 * @param string|array name of variable or an array of variables
111 * @param mixed value when using a named variable
114 public function set($name, $value = NULL)
118 foreach ($name as $key => $value)
120 $this->__set($key, $value);
125 $this->__set($name, $value);
132 * Checks for a property existence in the view locally or globally. Unlike the built in __isset(),
133 * this method can take an array of properties to test simultaneously.
135 * @param string $key property name to test for
136 * @param array $key array of property names to test for
137 * @return boolean property test result
138 * @return array associative array of keys and boolean test result
140 public function is_set( $key = FALSE )
145 // If key is an array
148 // Set the result to an array
152 foreach ($key as $property)
154 // Set the result to an associative array
155 $result[$property] = (array_key_exists($property, $this->kohana_local_data
) OR array_key_exists($property, self
::$kohana_global_data)) ?
TRUE : FALSE;
160 // Otherwise just check one property
161 $result = (array_key_exists($key, $this->kohana_local_data
) OR array_key_exists($key, self
::$kohana_global_data)) ?
TRUE : FALSE;
169 * Sets a bound variable by reference.
171 * @param string name of variable
172 * @param mixed variable to assign by reference
175 public function bind($name, & $var)
177 $this->kohana_local_data
[$name] =& $var;
183 * Sets a view global variable.
185 * @param string|array name of variable or an array of variables
186 * @param mixed value when using a named variable
189 public function set_global($name, $value = NULL)
193 foreach ($name as $key => $value)
195 self
::$kohana_global_data[$key] = $value;
200 self
::$kohana_global_data[$name] = $value;
207 * Magically sets a view variable.
209 * @param string variable key
210 * @param string variable value
213 public function __set($key, $value)
215 $this->kohana_local_data
[$key] = $value;
219 * Magically gets a view variable.
221 * @param string variable key
222 * @return mixed variable value if the key is found
223 * @return void if the key is not found
225 public function &__get($key)
227 if (isset($this->kohana_local_data
[$key]))
228 return $this->kohana_local_data
[$key];
230 if (isset(self
::$kohana_global_data[$key]))
231 return self
::$kohana_global_data[$key];
233 if (isset($this->$key))
236 $this->kohana_local_data
[$key] = null;
237 return $this->kohana_local_data
[$key];
241 * Magically converts view object to string.
245 public function __toString()
247 return $this->render();
253 * @param boolean set to TRUE to echo the output instead of returning it
254 * @param callback special renderer to pass the output through
255 * @return string if print is FALSE
256 * @return void if print is TRUE
258 public function render($print = FALSE, $renderer = FALSE)
260 if (empty($this->kohana_filename
))
261 throw new Kohana_Exception('core.view_set_filename');
263 if (is_string($this->kohana_filetype
))
265 // Merge global and local data, local overrides global with the same name
266 $data = array_merge(self
::$kohana_global_data, $this->kohana_local_data
);
268 // Load the view in the controller for access to $this
269 $output = Kohana
::$instance->_kohana_load_view($this->kohana_filename
, $data);
271 if ($renderer !== FALSE AND is_callable($renderer, TRUE))
273 // Pass the output through the user defined renderer
274 $output = call_user_func($renderer, $output);
279 // Display the output
286 // Set the content type and size
287 header('Content-Type: '.$this->kohana_filetype
[0]);
291 if ($file = fopen($this->kohana_filename
, 'rb'))
293 // Display the output
300 // Fetch the file contents
301 $output = file_get_contents($this->kohana_filename
);