view.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. <?php namespace Laravel; use Closure, ArrayAccess;
  2. class View implements ArrayAccess {
  3. /**
  4. * The name of the view.
  5. *
  6. * @var string
  7. */
  8. public $view;
  9. /**
  10. * The view data.
  11. *
  12. * @var array
  13. */
  14. public $data;
  15. /**
  16. * The path to the view on disk.
  17. *
  18. * @var string
  19. */
  20. public $path;
  21. /**
  22. * All of the shared view data.
  23. *
  24. * @var array
  25. */
  26. public static $shared = array();
  27. /**
  28. * All of the registered view names.
  29. *
  30. * @var array
  31. */
  32. public static $names = array();
  33. /**
  34. * The Laravel view loader event name.
  35. *
  36. * @var string
  37. */
  38. const loader = 'laravel.view.loader';
  39. /**
  40. * The Laravel view engine event name.
  41. *
  42. * @var string
  43. */
  44. const engine = 'laravel.view.engine';
  45. /**
  46. * Create a new view instance.
  47. *
  48. * <code>
  49. * // Create a new view instance
  50. * $view = new View('home.index');
  51. *
  52. * // Create a new view instance of a bundle's view
  53. * $view = new View('admin::home.index');
  54. *
  55. * // Create a new view instance with bound data
  56. * $view = new View('home.index', array('name' => 'Taylor'));
  57. * </code>
  58. *
  59. * @param string $view
  60. * @param array $data
  61. * @return void
  62. */
  63. public function __construct($view, $data = array())
  64. {
  65. $this->view = $view;
  66. $this->data = $data;
  67. // In order to allow developers to load views outside of the normal loading
  68. // conventions, we'll allow for a raw path to be given in place of the
  69. // typical view name, giving total freedom on view loading.
  70. if (starts_with($view, 'path: '))
  71. {
  72. $this->path = substr($view, 6);
  73. }
  74. else
  75. {
  76. $this->path = $this->path($view);
  77. }
  78. // If a session driver has been specified, we will bind an instance of the
  79. // validation error message container to every view. If an error instance
  80. // exists in the session, we will use that instance.
  81. if ( ! isset($this->data['errors']))
  82. {
  83. if (Session::started() and Session::has('errors'))
  84. {
  85. $this->data['errors'] = Session::get('errors');
  86. }
  87. else
  88. {
  89. $this->data['errors'] = new Messages;
  90. }
  91. }
  92. }
  93. /**
  94. * Get the path to a given view on disk.
  95. *
  96. * @param string $view
  97. * @return string
  98. */
  99. protected function path($view)
  100. {
  101. list($bundle, $view) = Bundle::parse($view);
  102. $view = str_replace('.', '/', $view);
  103. // We delegate the determination of view paths to the view loader event
  104. // so that the developer is free to override and manage the loading
  105. // of views in any way they see fit for their application.
  106. $path = Event::first(static::loader, array($bundle, $view));
  107. if ( ! is_null($path))
  108. {
  109. return $path;
  110. }
  111. throw new \Exception("View [$view] doesn't exist.");
  112. }
  113. /**
  114. * Get the path to a view using the default folder convention.
  115. *
  116. * @param string $bundle
  117. * @param string $view
  118. * @param string $directory
  119. * @return string
  120. */
  121. public static function file($bundle, $view, $directory)
  122. {
  123. $directory = str_finish($directory, DS);
  124. // Views may have either the default PHP file extension of the "Blade"
  125. // extension, so we will need to check for both in the view path
  126. // and return the first one we find for the given view.
  127. if (file_exists($path = $directory.$view.EXT))
  128. {
  129. return $path;
  130. }
  131. elseif (file_exists($path = $directory.$view.BLADE_EXT))
  132. {
  133. return $path;
  134. }
  135. }
  136. /**
  137. * Create a new view instance.
  138. *
  139. * <code>
  140. * // Create a new view instance
  141. * $view = View::make('home.index');
  142. *
  143. * // Create a new view instance of a bundle's view
  144. * $view = View::make('admin::home.index');
  145. *
  146. * // Create a new view instance with bound data
  147. * $view = View::make('home.index', array('name' => 'Taylor'));
  148. * </code>
  149. *
  150. * @param string $view
  151. * @param array $data
  152. * @return View
  153. */
  154. public static function make($view, $data = array())
  155. {
  156. return new static($view, $data);
  157. }
  158. /**
  159. * Create a new view instance of a named view.
  160. *
  161. * <code>
  162. * // Create a new named view instance
  163. * $view = View::of('profile');
  164. *
  165. * // Create a new named view instance with bound data
  166. * $view = View::of('profile', array('name' => 'Taylor'));
  167. * </code>
  168. *
  169. * @param string $name
  170. * @param array $data
  171. * @return View
  172. */
  173. public static function of($name, $data = array())
  174. {
  175. return new static(static::$names[$name], $data);
  176. }
  177. /**
  178. * Assign a name to a view.
  179. *
  180. * <code>
  181. * // Assign a name to a view
  182. * View::name('partials.profile', 'profile');
  183. *
  184. * // Resolve an instance of a named view
  185. * $view = View::of('profile');
  186. * </code>
  187. *
  188. * @param string $view
  189. * @param string $name
  190. * @return void
  191. */
  192. public static function name($view, $name)
  193. {
  194. static::$names[$name] = $view;
  195. }
  196. /**
  197. * Register a view composer with the Event class.
  198. *
  199. * <code>
  200. * // Register a composer for the "home.index" view
  201. * View::composer('home.index', function($view)
  202. * {
  203. * $view['title'] = 'Home';
  204. * });
  205. * </code>
  206. *
  207. * @param string $view
  208. * @param Closure $composer
  209. * @return void
  210. */
  211. public static function composer($view, $composer)
  212. {
  213. Event::listen("laravel.composing: {$view}", $composer);
  214. }
  215. /**
  216. * Get the rendered contents of a partial from a loop.
  217. *
  218. * @param string $view
  219. * @param array $data
  220. * @param string $iterator
  221. * @param string $empty
  222. * @return string
  223. */
  224. public static function render_each($view, array $data, $iterator, $empty = 'raw|')
  225. {
  226. $result = '';
  227. // If is actually data in the array, we will loop through the data and
  228. // append an instance of the partial view to the final result HTML,
  229. // passing in the iterated value of the data array.
  230. if (count($data) > 0)
  231. {
  232. foreach ($data as $key => $value)
  233. {
  234. $with = array('key' => $key, $iterator => $value);
  235. $result .= render($view, $with);
  236. }
  237. }
  238. // If there is no data in the array, we will render the contents of
  239. // the "empty" view. Alternative, the "empty view" can be a raw
  240. // string that is prefixed with "raw|" for convenience.
  241. else
  242. {
  243. if (starts_with($empty, 'raw|'))
  244. {
  245. $result = substr($empty, 4);
  246. }
  247. else
  248. {
  249. $result = render($empty);
  250. }
  251. }
  252. return $result;
  253. }
  254. /**
  255. * Get the evaluated string content of the view.
  256. *
  257. * @return string
  258. */
  259. public function render()
  260. {
  261. // To allow bundles or other pieces of the application to modify the
  262. // view before it is rendered, we'll fire an event, passing in the
  263. // view instance so it can modified.
  264. $composer = "laravel.composing: {$this->view}";
  265. Event::fire($composer, array($this));
  266. // If there are listeners to the view engine event, we'll pass them
  267. // the view so they can render it according to their needs, which
  268. // allows easy attachment of other view parsers.
  269. if (Event::listeners(static::engine))
  270. {
  271. $result = Event::first(static::engine, array($this));
  272. if ($result !== false) return $result;
  273. }
  274. return $this->get();
  275. }
  276. /**
  277. * Get the evaluated contents of the view.
  278. *
  279. * @return string
  280. */
  281. public function get()
  282. {
  283. $__data = $this->data();
  284. ob_start() and extract($__data, EXTR_SKIP);
  285. // We'll include the view contents for parsing within a catcher
  286. // so we can avoid any WSOD errors. If an exception occurs we
  287. // will throw it out to the exception handler.
  288. try
  289. {
  290. include $this->path;
  291. }
  292. // If we caught an exception, we'll silently flush the output
  293. // buffer so that no partially rendered views get thrown out
  294. // to the client and confuse the user.
  295. catch (\Exception $e)
  296. {
  297. ob_get_clean(); throw $e;
  298. }
  299. return ob_get_clean();
  300. }
  301. /**
  302. * Get the array of view data for the view instance.
  303. *
  304. * The shared view data will be combined with the view data.
  305. *
  306. * @return array
  307. */
  308. public function data()
  309. {
  310. $data = array_merge($this->data, static::$shared);
  311. // All nested views and responses are evaluated before the main view.
  312. // This allows the assets used by nested views to be added to the
  313. // asset container before the main view is evaluated.
  314. foreach ($data as $key => $value)
  315. {
  316. if ($value instanceof View or $value instanceof Response)
  317. {
  318. $data[$key] = $value->render();
  319. }
  320. }
  321. return $data;
  322. }
  323. /**
  324. * Add a view instance to the view data.
  325. *
  326. * <code>
  327. * // Add a view instance to a view's data
  328. * $view = View::make('foo')->nest('footer', 'partials.footer');
  329. *
  330. * // Equivalent functionality using the "with" method
  331. * $view = View::make('foo')->with('footer', View::make('partials.footer'));
  332. * </code>
  333. *
  334. * @param string $key
  335. * @param string $view
  336. * @param array $data
  337. * @return View
  338. */
  339. public function nest($key, $view, $data = array())
  340. {
  341. return $this->with($key, static::make($view, $data));
  342. }
  343. /**
  344. * Add a key / value pair to the view data.
  345. *
  346. * Bound data will be available to the view as variables.
  347. *
  348. * @param string $key
  349. * @param mixed $value
  350. * @return View
  351. */
  352. public function with($key, $value = null)
  353. {
  354. if (is_array($key))
  355. {
  356. $this->data = array_merge($this->data, $key);
  357. }
  358. else
  359. {
  360. $this->data[$key] = $value;
  361. }
  362. return $this;
  363. }
  364. /**
  365. * Add a key / value pair to the shared view data.
  366. *
  367. * Shared view data is accessible to every view created by the application.
  368. *
  369. * @param string $key
  370. * @param mixed $value
  371. * @return View
  372. */
  373. public function shares($key, $value)
  374. {
  375. static::share($key, $value);
  376. return $this;
  377. }
  378. /**
  379. * Add a key / value pair to the shared view data.
  380. *
  381. * Shared view data is accessible to every view created by the application.
  382. *
  383. * @param string $key
  384. * @param mixed $value
  385. * @return void
  386. */
  387. public static function share($key, $value)
  388. {
  389. static::$shared[$key] = $value;
  390. }
  391. /**
  392. * Implementation of the ArrayAccess offsetExists method.
  393. */
  394. public function offsetExists($offset)
  395. {
  396. return array_key_exists($offset, $this->data);
  397. }
  398. /**
  399. * Implementation of the ArrayAccess offsetGet method.
  400. */
  401. public function offsetGet($offset)
  402. {
  403. if (isset($this[$offset])) return $this->data[$offset];
  404. }
  405. /**
  406. * Implementation of the ArrayAccess offsetSet method.
  407. */
  408. public function offsetSet($offset, $value)
  409. {
  410. $this->data[$offset] = $value;
  411. }
  412. /**
  413. * Implementation of the ArrayAccess offsetUnset method.
  414. */
  415. public function offsetUnset($offset)
  416. {
  417. unset($this->data[$offset]);
  418. }
  419. /**
  420. * Magic Method for handling dynamic data access.
  421. */
  422. public function __get($key)
  423. {
  424. return $this->data[$key];
  425. }
  426. /**
  427. * Magic Method for handling the dynamic setting of data.
  428. */
  429. public function __set($key, $value)
  430. {
  431. $this->data[$key] = $value;
  432. }
  433. /**
  434. * Magic Method for checking dynamically-set data.
  435. */
  436. public function __isset($key)
  437. {
  438. return isset($this->data[$key]);
  439. }
  440. /**
  441. * Get the evaluated string content of the view.
  442. *
  443. * @return string
  444. */
  445. public function __toString()
  446. {
  447. return $this->render();
  448. }
  449. }