ViewVarsTrait.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright (c), Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP(tm) Project
  11. * @since CakePHP(tm) v 3.0.0
  12. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  13. */
  14. namespace Cake\Utility;
  15. /**
  16. * Provides the set() method for collecting template context.
  17. *
  18. * Once collected context data can be passed to another object.
  19. * This is done in Controller, TemplateTask and View for example.
  20. *
  21. */
  22. trait ViewVarsTrait {
  23. /**
  24. * Variables for the view
  25. *
  26. * @var array
  27. */
  28. public $viewVars = [];
  29. /**
  30. * Saves a variable for use inside a template.
  31. *
  32. * @param string|array $name A string or an array of data.
  33. * @param string|array $val Value in case $name is a string (which then works as the key).
  34. * Unused if $name is an associative array, otherwise serves as the values to $name's keys.
  35. * @return void
  36. */
  37. public function set($name, $val = null) {
  38. if (is_array($name)) {
  39. if (is_array($val)) {
  40. $data = array_combine($name, $val);
  41. } else {
  42. $data = $name;
  43. }
  44. } else {
  45. $data = [$name => $val];
  46. }
  47. $this->viewVars = $data + $this->viewVars;
  48. }
  49. }