2012-01-16 13:59:24 -06:00
|
|
|
<?php namespace Laravel; use Closure, ArrayAccess;
|
2011-11-02 21:27:43 -05:00
|
|
|
|
2012-01-16 13:59:24 -06:00
|
|
|
class View implements ArrayAccess {
|
2011-08-26 21:42:04 -05:00
|
|
|
|
2011-08-29 19:27:02 -05:00
|
|
|
/**
|
2011-10-05 18:32:48 -05:00
|
|
|
* The name of the view.
|
2011-08-29 19:27:02 -05:00
|
|
|
*
|
2011-09-20 23:14:09 -05:00
|
|
|
* @var string
|
2011-08-29 19:27:02 -05:00
|
|
|
*/
|
2011-10-05 18:32:48 -05:00
|
|
|
public $view;
|
2011-09-20 23:14:09 -05:00
|
|
|
|
2011-09-25 23:00:29 -05:00
|
|
|
/**
|
2011-10-05 18:32:48 -05:00
|
|
|
* The view data.
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
public $data;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The path to the view on disk.
|
2011-09-25 23:00:29 -05:00
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2012-01-25 08:37:04 -06:00
|
|
|
public $path;
|
2011-09-25 23:00:29 -05:00
|
|
|
|
2011-09-20 23:14:09 -05:00
|
|
|
/**
|
2012-01-16 13:59:24 -06:00
|
|
|
* All of the shared view data.
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
public static $shared = array();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* All of the registered view names.
|
2011-08-29 19:27:02 -05:00
|
|
|
*
|
2011-10-05 18:32:48 -05:00
|
|
|
* @var array
|
2011-08-29 19:27:02 -05:00
|
|
|
*/
|
2012-01-16 13:59:24 -06:00
|
|
|
public static $names = array();
|
2011-08-29 19:27:02 -05:00
|
|
|
|
2012-04-01 21:11:45 -05:00
|
|
|
/**
|
|
|
|
|
* The cache content of loaded view files.
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
public static $cache = array();
|
|
|
|
|
|
2012-09-26 16:20:56 -04:00
|
|
|
/**
|
|
|
|
|
* THe last view to be rendered.
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
public static $last;
|
|
|
|
|
|
2012-11-06 15:04:13 -06:00
|
|
|
/**
|
|
|
|
|
* The render operations taking place.
|
|
|
|
|
*
|
|
|
|
|
* @var int
|
|
|
|
|
*/
|
|
|
|
|
public static $render_count = 0;
|
|
|
|
|
|
2012-03-01 11:52:05 -06:00
|
|
|
/**
|
|
|
|
|
* The Laravel view loader event name.
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
const loader = 'laravel.view.loader';
|
|
|
|
|
|
2012-02-23 22:34:42 -06:00
|
|
|
/**
|
|
|
|
|
* The Laravel view engine event name.
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
const engine = 'laravel.view.engine';
|
|
|
|
|
|
2011-08-26 21:42:04 -05:00
|
|
|
/**
|
2011-10-05 18:32:48 -05:00
|
|
|
* Create a new view instance.
|
2011-08-26 21:42:04 -05:00
|
|
|
*
|
2012-01-16 13:59:24 -06:00
|
|
|
* <code>
|
|
|
|
|
* // Create a new view instance
|
|
|
|
|
* $view = new View('home.index');
|
|
|
|
|
*
|
|
|
|
|
* // Create a new view instance of a bundle's view
|
|
|
|
|
* $view = new View('admin::home.index');
|
|
|
|
|
*
|
|
|
|
|
* // Create a new view instance with bound data
|
|
|
|
|
* $view = new View('home.index', array('name' => 'Taylor'));
|
|
|
|
|
* </code>
|
|
|
|
|
*
|
2011-10-05 18:32:48 -05:00
|
|
|
* @param string $view
|
|
|
|
|
* @param array $data
|
2011-08-26 21:42:04 -05:00
|
|
|
* @return void
|
|
|
|
|
*/
|
2011-10-05 18:32:48 -05:00
|
|
|
public function __construct($view, $data = array())
|
2011-08-26 21:42:04 -05:00
|
|
|
{
|
2011-10-05 18:32:48 -05:00
|
|
|
$this->view = $view;
|
|
|
|
|
$this->data = $data;
|
2012-03-23 13:01:36 -05:00
|
|
|
|
|
|
|
|
// In order to allow developers to load views outside of the normal loading
|
|
|
|
|
// conventions, we'll allow for a raw path to be given in place of the
|
|
|
|
|
// typical view name, giving total freedom on view loading.
|
|
|
|
|
if (starts_with($view, 'path: '))
|
|
|
|
|
{
|
|
|
|
|
$this->path = substr($view, 6);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
$this->path = $this->path($view);
|
|
|
|
|
}
|
2011-11-02 21:27:43 -05:00
|
|
|
|
2012-01-16 13:59:24 -06:00
|
|
|
// If a session driver has been specified, we will bind an instance of the
|
2012-02-23 17:00:36 -06:00
|
|
|
// validation error message container to every view. If an error instance
|
2012-01-16 13:59:24 -06:00
|
|
|
// exists in the session, we will use that instance.
|
2012-01-25 08:37:04 -06:00
|
|
|
if ( ! isset($this->data['errors']))
|
2011-11-02 21:27:43 -05:00
|
|
|
{
|
2012-01-25 08:37:04 -06:00
|
|
|
if (Session::started() and Session::has('errors'))
|
2011-11-02 21:27:43 -05:00
|
|
|
{
|
2012-01-25 08:37:04 -06:00
|
|
|
$this->data['errors'] = Session::get('errors');
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
$this->data['errors'] = new Messages;
|
|
|
|
|
}
|
2011-11-02 21:27:43 -05:00
|
|
|
}
|
2011-10-05 18:32:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-04-17 20:13:42 -05:00
|
|
|
* Determine if the given view exists.
|
|
|
|
|
*
|
|
|
|
|
* @param string $view
|
|
|
|
|
* @param boolean $return_path
|
|
|
|
|
* @return string|bool
|
2011-10-05 18:32:48 -05:00
|
|
|
*/
|
2012-04-17 19:25:41 +01:00
|
|
|
public static function exists($view, $return_path = false)
|
2011-10-05 18:32:48 -05:00
|
|
|
{
|
2012-06-16 10:29:45 +09:30
|
|
|
if (starts_with($view, 'name: ') and array_key_exists($name = substr($view, 6), static::$names))
|
2012-06-15 10:29:09 +09:30
|
|
|
{
|
2012-06-16 10:29:45 +09:30
|
|
|
$view = static::$names[$name];
|
2012-06-15 10:29:09 +09:30
|
|
|
}
|
|
|
|
|
|
2012-03-01 11:52:05 -06:00
|
|
|
list($bundle, $view) = Bundle::parse($view);
|
2011-10-05 18:32:48 -05:00
|
|
|
|
2012-03-01 11:52:05 -06:00
|
|
|
$view = str_replace('.', '/', $view);
|
2012-01-16 13:59:24 -06:00
|
|
|
|
2012-03-22 11:48:07 -05:00
|
|
|
// We delegate the determination of view paths to the view loader event
|
|
|
|
|
// so that the developer is free to override and manage the loading
|
|
|
|
|
// of views in any way they see fit for their application.
|
2012-05-04 12:35:24 +01:00
|
|
|
$path = Event::until(static::loader, array($bundle, $view));
|
2012-02-23 17:00:36 -06:00
|
|
|
|
2012-03-01 11:52:05 -06:00
|
|
|
if ( ! is_null($path))
|
2012-04-17 19:25:41 +01:00
|
|
|
{
|
|
|
|
|
return $return_path ? $path : true;
|
|
|
|
|
}
|
2012-04-17 20:13:42 -05:00
|
|
|
|
2012-04-17 19:25:41 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the path to a given view on disk.
|
|
|
|
|
*
|
|
|
|
|
* @param string $view
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
protected function path($view)
|
|
|
|
|
{
|
|
|
|
|
if ($path = $this->exists($view,true))
|
2011-10-05 18:32:48 -05:00
|
|
|
{
|
2012-03-01 11:52:05 -06:00
|
|
|
return $path;
|
2011-10-05 18:32:48 -05:00
|
|
|
}
|
|
|
|
|
|
2012-02-23 17:00:36 -06:00
|
|
|
throw new \Exception("View [$view] doesn't exist.");
|
2011-08-26 21:42:04 -05:00
|
|
|
}
|
|
|
|
|
|
2012-03-01 11:52:05 -06:00
|
|
|
/**
|
|
|
|
|
* Get the path to a view using the default folder convention.
|
|
|
|
|
*
|
|
|
|
|
* @param string $bundle
|
|
|
|
|
* @param string $view
|
2012-03-26 08:27:53 -05:00
|
|
|
* @param string $directory
|
2012-03-01 11:52:05 -06:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2012-03-26 08:27:53 -05:00
|
|
|
public static function file($bundle, $view, $directory)
|
2012-03-01 11:52:05 -06:00
|
|
|
{
|
2012-03-26 08:27:53 -05:00
|
|
|
$directory = str_finish($directory, DS);
|
2012-03-01 11:52:05 -06:00
|
|
|
|
2012-06-13 11:19:20 -04:00
|
|
|
// Views may have either the default PHP file extension or the "Blade"
|
2012-03-22 11:48:07 -05:00
|
|
|
// extension, so we will need to check for both in the view path
|
|
|
|
|
// and return the first one we find for the given view.
|
2012-03-26 08:27:53 -05:00
|
|
|
if (file_exists($path = $directory.$view.EXT))
|
2012-03-01 11:52:05 -06:00
|
|
|
{
|
|
|
|
|
return $path;
|
|
|
|
|
}
|
2012-03-26 08:27:53 -05:00
|
|
|
elseif (file_exists($path = $directory.$view.BLADE_EXT))
|
2012-03-22 11:48:07 -05:00
|
|
|
{
|
|
|
|
|
return $path;
|
|
|
|
|
}
|
2012-03-01 11:52:05 -06:00
|
|
|
}
|
|
|
|
|
|
2011-08-31 23:51:52 -05:00
|
|
|
/**
|
|
|
|
|
* Create a new view instance.
|
|
|
|
|
*
|
2011-09-25 22:13:43 -05:00
|
|
|
* <code>
|
|
|
|
|
* // Create a new view instance
|
|
|
|
|
* $view = View::make('home.index');
|
|
|
|
|
*
|
2012-01-16 13:59:24 -06:00
|
|
|
* // Create a new view instance of a bundle's view
|
|
|
|
|
* $view = View::make('admin::home.index');
|
|
|
|
|
*
|
2011-09-25 22:13:43 -05:00
|
|
|
* // Create a new view instance with bound data
|
|
|
|
|
* $view = View::make('home.index', array('name' => 'Taylor'));
|
|
|
|
|
* </code>
|
|
|
|
|
*
|
2011-09-21 21:46:16 -05:00
|
|
|
* @param string $view
|
|
|
|
|
* @param array $data
|
2011-08-31 23:51:52 -05:00
|
|
|
* @return View
|
|
|
|
|
*/
|
2011-10-05 18:32:48 -05:00
|
|
|
public static function make($view, $data = array())
|
2011-08-31 23:51:52 -05:00
|
|
|
{
|
2011-10-05 18:32:48 -05:00
|
|
|
return new static($view, $data);
|
2011-08-31 23:51:52 -05:00
|
|
|
}
|
|
|
|
|
|
2011-08-26 21:42:04 -05:00
|
|
|
/**
|
2012-01-16 13:59:24 -06:00
|
|
|
* Create a new view instance of a named view.
|
2011-09-08 17:49:16 -05:00
|
|
|
*
|
2011-09-25 22:13:43 -05:00
|
|
|
* <code>
|
2012-01-16 13:59:24 -06:00
|
|
|
* // Create a new named view instance
|
|
|
|
|
* $view = View::of('profile');
|
2011-09-25 22:13:43 -05:00
|
|
|
*
|
2012-01-16 13:59:24 -06:00
|
|
|
* // Create a new named view instance with bound data
|
|
|
|
|
* $view = View::of('profile', array('name' => 'Taylor'));
|
2011-09-25 22:13:43 -05:00
|
|
|
* </code>
|
|
|
|
|
*
|
2011-08-26 21:42:04 -05:00
|
|
|
* @param string $name
|
|
|
|
|
* @param array $data
|
|
|
|
|
* @return View
|
|
|
|
|
*/
|
2011-10-05 18:32:48 -05:00
|
|
|
public static function of($name, $data = array())
|
2011-08-26 21:42:04 -05:00
|
|
|
{
|
2012-01-16 13:59:24 -06:00
|
|
|
return new static(static::$names[$name], $data);
|
2011-08-26 21:42:04 -05:00
|
|
|
}
|
|
|
|
|
|
2011-09-21 21:46:16 -05:00
|
|
|
/**
|
2012-01-16 13:59:24 -06:00
|
|
|
* Assign a name to a view.
|
|
|
|
|
*
|
|
|
|
|
* <code>
|
|
|
|
|
* // Assign a name to a view
|
|
|
|
|
* View::name('partials.profile', 'profile');
|
2011-09-21 21:46:16 -05:00
|
|
|
*
|
2012-01-16 13:59:24 -06:00
|
|
|
* // Resolve an instance of a named view
|
|
|
|
|
* $view = View::of('profile');
|
|
|
|
|
* </code>
|
2011-09-25 22:13:43 -05:00
|
|
|
*
|
2012-01-16 13:59:24 -06:00
|
|
|
* @param string $view
|
2011-09-21 21:46:16 -05:00
|
|
|
* @param string $name
|
2012-01-16 13:59:24 -06:00
|
|
|
* @return void
|
2011-09-21 21:46:16 -05:00
|
|
|
*/
|
2012-01-16 13:59:24 -06:00
|
|
|
public static function name($view, $name)
|
2011-09-21 21:46:16 -05:00
|
|
|
{
|
2012-01-16 13:59:24 -06:00
|
|
|
static::$names[$name] = $view;
|
2011-09-21 21:46:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-01-16 13:59:24 -06:00
|
|
|
* Register a view composer with the Event class.
|
2011-09-21 21:46:16 -05:00
|
|
|
*
|
2012-01-16 13:59:24 -06:00
|
|
|
* <code>
|
|
|
|
|
* // Register a composer for the "home.index" view
|
|
|
|
|
* View::composer('home.index', function($view)
|
|
|
|
|
* {
|
|
|
|
|
* $view['title'] = 'Home';
|
|
|
|
|
* });
|
|
|
|
|
* </code>
|
|
|
|
|
*
|
2012-08-03 18:17:48 +03:00
|
|
|
* @param string|array $views
|
2012-04-19 08:37:42 -05:00
|
|
|
* @param Closure $composer
|
2011-09-21 21:46:16 -05:00
|
|
|
* @return void
|
|
|
|
|
*/
|
2012-04-19 08:37:42 -05:00
|
|
|
public static function composer($views, $composer)
|
2011-09-21 21:46:16 -05:00
|
|
|
{
|
2012-04-19 08:37:42 -05:00
|
|
|
$views = (array) $views;
|
|
|
|
|
|
|
|
|
|
foreach ($views as $view)
|
|
|
|
|
{
|
|
|
|
|
Event::listen("laravel.composing: {$view}", $composer);
|
|
|
|
|
}
|
2011-09-21 21:46:16 -05:00
|
|
|
}
|
|
|
|
|
|
2012-02-27 16:27:28 -06:00
|
|
|
/**
|
2012-03-22 15:45:26 -05:00
|
|
|
* Get the rendered contents of a partial from a loop.
|
2012-02-27 16:27:28 -06:00
|
|
|
*
|
2012-03-22 15:45:26 -05:00
|
|
|
* @param string $view
|
|
|
|
|
* @param array $data
|
|
|
|
|
* @param string $iterator
|
|
|
|
|
* @param string $empty
|
|
|
|
|
* @return string
|
2012-02-27 16:27:28 -06:00
|
|
|
*/
|
2012-03-22 16:05:33 -05:00
|
|
|
public static function render_each($view, array $data, $iterator, $empty = 'raw|')
|
2012-02-27 16:27:28 -06:00
|
|
|
{
|
2012-03-22 15:45:26 -05:00
|
|
|
$result = '';
|
2012-02-27 16:27:28 -06:00
|
|
|
|
2012-03-22 15:45:26 -05:00
|
|
|
// If is actually data in the array, we will loop through the data and
|
|
|
|
|
// append an instance of the partial view to the final result HTML,
|
|
|
|
|
// passing in the iterated value of the data array.
|
|
|
|
|
if (count($data) > 0)
|
|
|
|
|
{
|
|
|
|
|
foreach ($data as $key => $value)
|
|
|
|
|
{
|
|
|
|
|
$with = array('key' => $key, $iterator => $value);
|
|
|
|
|
|
|
|
|
|
$result .= render($view, $with);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If there is no data in the array, we will render the contents of
|
2012-06-13 11:19:20 -04:00
|
|
|
// the "empty" view. Alternatively, the "empty view" can be a raw
|
2012-03-22 15:45:26 -05:00
|
|
|
// string that is prefixed with "raw|" for convenience.
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (starts_with($empty, 'raw|'))
|
|
|
|
|
{
|
|
|
|
|
$result = substr($empty, 4);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2012-03-22 16:05:33 -05:00
|
|
|
$result = render($empty);
|
2012-03-22 15:45:26 -05:00
|
|
|
}
|
|
|
|
|
}
|
2012-02-27 16:27:28 -06:00
|
|
|
|
2012-03-22 15:45:26 -05:00
|
|
|
return $result;
|
2012-02-27 16:27:28 -06:00
|
|
|
}
|
|
|
|
|
|
2011-11-02 21:27:43 -05:00
|
|
|
/**
|
2012-01-16 13:59:24 -06:00
|
|
|
* Get the evaluated string content of the view.
|
2011-11-02 21:27:43 -05:00
|
|
|
*
|
2012-01-16 13:59:24 -06:00
|
|
|
* @return string
|
2011-11-02 21:27:43 -05:00
|
|
|
*/
|
2012-01-16 13:59:24 -06:00
|
|
|
public function render()
|
2011-11-02 21:27:43 -05:00
|
|
|
{
|
2012-11-06 15:04:13 -06:00
|
|
|
static::$render_count++;
|
|
|
|
|
|
2012-04-01 21:11:45 -05:00
|
|
|
Event::fire("laravel.composing: {$this->view}", array($this));
|
2012-01-16 13:59:24 -06:00
|
|
|
|
2012-11-06 15:04:13 -06:00
|
|
|
$contents = null;
|
|
|
|
|
|
2012-02-23 17:00:36 -06:00
|
|
|
// If there are listeners to the view engine event, we'll pass them
|
|
|
|
|
// the view so they can render it according to their needs, which
|
|
|
|
|
// allows easy attachment of other view parsers.
|
2012-02-23 22:34:42 -06:00
|
|
|
if (Event::listeners(static::engine))
|
2012-02-23 17:00:36 -06:00
|
|
|
{
|
2012-05-04 12:35:24 +01:00
|
|
|
$result = Event::until(static::engine, array($this));
|
2012-02-27 15:29:11 -06:00
|
|
|
|
2012-11-06 15:04:13 -06:00
|
|
|
if ( ! is_null($result)) $contents = $result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (is_null($contents)) $contents = $this->get();
|
|
|
|
|
|
|
|
|
|
static::$render_count--;
|
|
|
|
|
|
|
|
|
|
if (static::$render_count == 0)
|
|
|
|
|
{
|
|
|
|
|
Section::$sections = array();
|
2012-02-23 17:00:36 -06:00
|
|
|
}
|
2012-02-27 15:29:11 -06:00
|
|
|
|
2012-11-06 15:04:13 -06:00
|
|
|
return $contents;
|
2012-02-23 17:00:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the evaluated contents of the view.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function get()
|
|
|
|
|
{
|
|
|
|
|
$__data = $this->data();
|
2011-11-30 10:54:51 -06:00
|
|
|
|
2012-04-01 21:11:45 -05:00
|
|
|
// The contents of each view file is cached in an array for the
|
|
|
|
|
// request since partial views may be rendered inside of for
|
|
|
|
|
// loops which could incur performance penalties.
|
2012-04-01 21:13:07 -05:00
|
|
|
$__contents = $this->load();
|
2012-04-01 21:11:45 -05:00
|
|
|
|
2012-02-23 17:00:36 -06:00
|
|
|
ob_start() and extract($__data, EXTR_SKIP);
|
2012-01-16 13:59:24 -06:00
|
|
|
|
2012-02-23 17:00:36 -06:00
|
|
|
// We'll include the view contents for parsing within a catcher
|
|
|
|
|
// so we can avoid any WSOD errors. If an exception occurs we
|
2012-02-25 22:30:35 -06:00
|
|
|
// will throw it out to the exception handler.
|
2012-02-23 17:00:36 -06:00
|
|
|
try
|
2012-01-16 13:59:24 -06:00
|
|
|
{
|
2012-04-01 21:11:45 -05:00
|
|
|
eval('?>'.$__contents);
|
2012-01-16 13:59:24 -06:00
|
|
|
}
|
|
|
|
|
|
2012-02-23 17:00:36 -06:00
|
|
|
// If we caught an exception, we'll silently flush the output
|
|
|
|
|
// buffer so that no partially rendered views get thrown out
|
2012-04-01 21:11:45 -05:00
|
|
|
// to the client and confuse the user with junk.
|
2012-02-23 17:00:36 -06:00
|
|
|
catch (\Exception $e)
|
|
|
|
|
{
|
|
|
|
|
ob_get_clean(); throw $e;
|
|
|
|
|
}
|
2012-01-16 13:59:24 -06:00
|
|
|
|
2012-09-26 10:43:34 -04:00
|
|
|
$content = ob_get_clean();
|
|
|
|
|
|
|
|
|
|
// The view filter event gives us a last chance to modify the
|
|
|
|
|
// evaluated contents of the view and return them. This lets
|
|
|
|
|
// us do something like run the contents through Jade, etc.
|
|
|
|
|
if (Event::listeners('view.filter'))
|
|
|
|
|
{
|
2012-09-26 11:20:03 -04:00
|
|
|
return Event::first('view.filter', array($content, $this->path));
|
2012-09-26 10:43:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $content;
|
2011-11-02 21:27:43 -05:00
|
|
|
}
|
|
|
|
|
|
2012-04-01 21:11:45 -05:00
|
|
|
/**
|
|
|
|
|
* Get the contents of the view file from disk.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
protected function load()
|
|
|
|
|
{
|
2012-09-26 16:20:56 -04:00
|
|
|
static::$last = array('name' => $this->view, 'path' => $this->path);
|
|
|
|
|
|
2012-04-01 21:11:45 -05:00
|
|
|
if (isset(static::$cache[$this->path]))
|
|
|
|
|
{
|
|
|
|
|
return static::$cache[$this->path];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2012-04-02 11:02:09 -05:00
|
|
|
return static::$cache[$this->path] = file_get_contents($this->path);
|
2012-04-01 21:11:45 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-05 11:11:26 -05:00
|
|
|
/**
|
2012-01-16 13:59:24 -06:00
|
|
|
* Get the array of view data for the view instance.
|
2011-06-08 23:45:08 -05:00
|
|
|
*
|
2012-03-23 13:01:36 -05:00
|
|
|
* The shared view data will be combined with the view data.
|
2012-01-16 13:59:24 -06:00
|
|
|
*
|
|
|
|
|
* @return array
|
2011-06-08 23:45:08 -05:00
|
|
|
*/
|
2012-02-23 17:00:36 -06:00
|
|
|
public function data()
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2012-01-16 13:59:24 -06:00
|
|
|
$data = array_merge($this->data, static::$shared);
|
2011-08-05 13:21:31 -05:00
|
|
|
|
2011-10-20 21:44:18 -05:00
|
|
|
// All nested views and responses are evaluated before the main view.
|
2012-01-16 13:59:24 -06:00
|
|
|
// This allows the assets used by nested views to be added to the
|
2012-02-21 10:50:25 -06:00
|
|
|
// asset container before the main view is evaluated.
|
2012-03-23 17:03:27 -05:00
|
|
|
foreach ($data as $key => $value)
|
2011-08-08 15:19:44 -05:00
|
|
|
{
|
2012-01-16 13:59:24 -06:00
|
|
|
if ($value instanceof View or $value instanceof Response)
|
2011-10-15 22:44:37 -05:00
|
|
|
{
|
2012-03-23 17:03:27 -05:00
|
|
|
$data[$key] = $value->render();
|
2011-10-15 22:44:37 -05:00
|
|
|
}
|
2011-08-08 15:19:44 -05:00
|
|
|
}
|
|
|
|
|
|
2012-01-16 13:59:24 -06:00
|
|
|
return $data;
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
2011-09-21 21:46:16 -05:00
|
|
|
/**
|
|
|
|
|
* Add a view instance to the view data.
|
2011-09-08 17:49:16 -05:00
|
|
|
*
|
2011-09-25 22:13:43 -05:00
|
|
|
* <code>
|
|
|
|
|
* // Add a view instance to a view's data
|
2011-09-28 23:30:11 -05:00
|
|
|
* $view = View::make('foo')->nest('footer', 'partials.footer');
|
2011-09-25 22:13:43 -05:00
|
|
|
*
|
|
|
|
|
* // Equivalent functionality using the "with" method
|
|
|
|
|
* $view = View::make('foo')->with('footer', View::make('partials.footer'));
|
|
|
|
|
* </code>
|
|
|
|
|
*
|
2011-07-26 14:35:44 -05:00
|
|
|
* @param string $key
|
|
|
|
|
* @param string $view
|
|
|
|
|
* @param array $data
|
|
|
|
|
* @return View
|
|
|
|
|
*/
|
2011-09-28 23:30:11 -05:00
|
|
|
public function nest($key, $view, $data = array())
|
2011-07-26 14:35:44 -05:00
|
|
|
{
|
2011-10-05 18:32:48 -05:00
|
|
|
return $this->with($key, static::make($view, $data));
|
2011-07-26 14:35:44 -05:00
|
|
|
}
|
|
|
|
|
|
2011-06-16 21:55:02 -05:00
|
|
|
/**
|
|
|
|
|
* Add a key / value pair to the view data.
|
|
|
|
|
*
|
2011-08-24 22:51:32 -05:00
|
|
|
* Bound data will be available to the view as variables.
|
|
|
|
|
*
|
2011-06-16 21:55:02 -05:00
|
|
|
* @param string $key
|
|
|
|
|
* @param mixed $value
|
|
|
|
|
* @return View
|
|
|
|
|
*/
|
2012-03-24 22:39:23 -05:00
|
|
|
public function with($key, $value = null)
|
2011-06-16 21:55:02 -05:00
|
|
|
{
|
2012-03-23 13:01:36 -05:00
|
|
|
if (is_array($key))
|
|
|
|
|
{
|
|
|
|
|
$this->data = array_merge($this->data, $key);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
$this->data[$key] = $value;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-16 21:55:02 -05:00
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-22 13:38:22 -05:00
|
|
|
/**
|
|
|
|
|
* Add a key / value pair to the shared view data.
|
|
|
|
|
*
|
|
|
|
|
* Shared view data is accessible to every view created by the application.
|
|
|
|
|
*
|
|
|
|
|
* @param string $key
|
|
|
|
|
* @param mixed $value
|
|
|
|
|
* @return View
|
|
|
|
|
*/
|
|
|
|
|
public function shares($key, $value)
|
|
|
|
|
{
|
|
|
|
|
static::share($key, $value);
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-08 23:45:08 -05:00
|
|
|
/**
|
2012-01-16 13:59:24 -06:00
|
|
|
* Add a key / value pair to the shared view data.
|
|
|
|
|
*
|
|
|
|
|
* Shared view data is accessible to every view created by the application.
|
|
|
|
|
*
|
|
|
|
|
* @param string $key
|
|
|
|
|
* @param mixed $value
|
|
|
|
|
* @return void
|
2011-06-08 23:45:08 -05:00
|
|
|
*/
|
2012-01-16 13:59:24 -06:00
|
|
|
public static function share($key, $value)
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2012-01-16 13:59:24 -06:00
|
|
|
static::$shared[$key] = $value;
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-01-16 13:59:24 -06:00
|
|
|
* Implementation of the ArrayAccess offsetExists method.
|
2011-06-08 23:45:08 -05:00
|
|
|
*/
|
2012-01-16 13:59:24 -06:00
|
|
|
public function offsetExists($offset)
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2012-01-16 13:59:24 -06:00
|
|
|
return array_key_exists($offset, $this->data);
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-01-16 13:59:24 -06:00
|
|
|
* Implementation of the ArrayAccess offsetGet method.
|
2011-06-08 23:45:08 -05:00
|
|
|
*/
|
2012-01-16 13:59:24 -06:00
|
|
|
public function offsetGet($offset)
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2012-01-16 13:59:24 -06:00
|
|
|
if (isset($this[$offset])) return $this->data[$offset];
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-01-16 13:59:24 -06:00
|
|
|
* Implementation of the ArrayAccess offsetSet method.
|
2011-06-08 23:45:08 -05:00
|
|
|
*/
|
2012-01-16 13:59:24 -06:00
|
|
|
public function offsetSet($offset, $value)
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2012-01-16 13:59:24 -06:00
|
|
|
$this->data[$offset] = $value;
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
2011-11-11 21:27:30 -06:00
|
|
|
/**
|
2012-01-16 13:59:24 -06:00
|
|
|
* Implementation of the ArrayAccess offsetUnset method.
|
2011-11-11 21:27:30 -06:00
|
|
|
*/
|
2012-01-16 13:59:24 -06:00
|
|
|
public function offsetUnset($offset)
|
2011-11-11 21:27:30 -06:00
|
|
|
{
|
2012-01-16 13:59:24 -06:00
|
|
|
unset($this->data[$offset]);
|
2011-11-11 21:27:30 -06:00
|
|
|
}
|
|
|
|
|
|
2012-01-17 13:32:04 -06:00
|
|
|
/**
|
|
|
|
|
* Magic Method for handling dynamic data access.
|
|
|
|
|
*/
|
|
|
|
|
public function __get($key)
|
|
|
|
|
{
|
2012-01-22 17:34:08 -08:00
|
|
|
return $this->data[$key];
|
2012-01-17 13:32:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Magic Method for handling the dynamic setting of data.
|
|
|
|
|
*/
|
|
|
|
|
public function __set($key, $value)
|
|
|
|
|
{
|
2012-01-22 17:34:08 -08:00
|
|
|
$this->data[$key] = $value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Magic Method for checking dynamically-set data.
|
|
|
|
|
*/
|
|
|
|
|
public function __isset($key)
|
|
|
|
|
{
|
|
|
|
|
return isset($this->data[$key]);
|
2012-01-17 13:32:04 -06:00
|
|
|
}
|
|
|
|
|
|
2011-10-05 18:32:48 -05:00
|
|
|
/**
|
2012-01-16 13:59:24 -06:00
|
|
|
* Get the evaluated string content of the view.
|
2011-10-05 18:32:48 -05:00
|
|
|
*
|
2012-01-16 13:59:24 -06:00
|
|
|
* @return string
|
2011-10-05 18:32:48 -05:00
|
|
|
*/
|
2012-01-16 13:59:24 -06:00
|
|
|
public function __toString()
|
2011-10-05 18:32:48 -05:00
|
|
|
{
|
2012-01-16 13:59:24 -06:00
|
|
|
return $this->render();
|
2011-10-05 18:32:48 -05:00
|
|
|
}
|
|
|
|
|
|
2012-06-25 21:31:54 +03:00
|
|
|
/**
|
|
|
|
|
* Magic Method for handling dynamic functions.
|
|
|
|
|
*
|
|
|
|
|
* This method handles calls to dynamic with helpers.
|
|
|
|
|
*/
|
|
|
|
|
public function __call($method, $parameters)
|
|
|
|
|
{
|
|
|
|
|
if (strpos($method, 'with_') === 0)
|
|
|
|
|
{
|
|
|
|
|
$key = substr($method, 5);
|
|
|
|
|
return $this->with($key, $parameters[0]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new \Exception("Method [$method] is not defined on the View class.");
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-16 13:12:33 -06:00
|
|
|
}
|