view.php 11 KB

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