Partial.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ LsIyAcSahn0uF+QZ9tGePJX0AaQdEhKpWX0PHRCWJsE=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2015 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.4
  11. */
  12. /**
  13. * partial view helper
  14. */
  15. namespace Cube\View\Helper;
  16. class Partial extends AbstractHelper
  17. {
  18. /**
  19. *
  20. * data to be used by the partial
  21. *
  22. * @var array
  23. */
  24. protected $_data = array();
  25. /**
  26. *
  27. * path where to search for view partial files
  28. *
  29. * @var string
  30. */
  31. protected $_path;
  32. /**
  33. *
  34. * get view partials path
  35. *
  36. * @return string
  37. */
  38. public function getPath()
  39. {
  40. return $this->_path;
  41. }
  42. /**
  43. *
  44. * set view partials path
  45. *
  46. * @param string $path
  47. *
  48. * @return $this
  49. */
  50. public function setPath($path)
  51. {
  52. $this->_path = rtrim($path, DIRECTORY_SEPARATOR);
  53. return $this;
  54. }
  55. /**
  56. *
  57. * process view partial and return output
  58. *
  59. * @param string $partial view partial name/path
  60. * @param array $data input data to be handled by the view partial
  61. * @param string $path view partials path
  62. *
  63. * @return string|$this partial output
  64. */
  65. public function partial($partial = null, array $data = null, $path = null)
  66. {
  67. if ($partial === null) {
  68. return $this;
  69. }
  70. $this->setPath($path);
  71. $this->setPartial($partial);
  72. $view = clone $this->getView();
  73. $view->clearVariables();
  74. if ($data !== null) {
  75. $view->setVariables($data);
  76. }
  77. $file = $this->getPath() . DIRECTORY_SEPARATOR . $this->getPartial();
  78. return $view->process(
  79. $file, true);
  80. }
  81. }