TemplateEngine.php
6 days ago
View.php
6 days ago
ViewContext.php
6 days ago
layout-wrapper.php
6 days ago
View.php
151 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Renderable view value object returned by the view() helper. |
| 5 | * |
| 6 | * @package Framework |
| 7 | * @subpackage View |
| 8 | * @since 1.0.0 |
| 9 | */ |
| 10 | namespace Kirki\Framework\View; |
| 11 | |
| 12 | \defined('ABSPATH') || exit; |
| 13 | use function Kirki\Framework\app; |
| 14 | class View |
| 15 | { |
| 16 | /** |
| 17 | * The template name in dot notation. |
| 18 | * |
| 19 | * @var string |
| 20 | * |
| 21 | * @since 1.0.0 |
| 22 | */ |
| 23 | protected $template; |
| 24 | /** |
| 25 | * Data passed to the template. |
| 26 | * |
| 27 | * @var array |
| 28 | * |
| 29 | * @since 1.0.0 |
| 30 | */ |
| 31 | protected $data = []; |
| 32 | /** |
| 33 | * Whether to wrap the view in the theme layout. |
| 34 | * |
| 35 | * @var bool |
| 36 | * |
| 37 | * @since 1.0.0 |
| 38 | */ |
| 39 | protected $with_layout = \true; |
| 40 | /** |
| 41 | * Create a new View instance. |
| 42 | * |
| 43 | * @param string $template The template name. |
| 44 | * @param array $data The template data. |
| 45 | * |
| 46 | * @return void |
| 47 | * |
| 48 | * @since 1.0.0 |
| 49 | */ |
| 50 | public function __construct(string $template, array $data = []) |
| 51 | { |
| 52 | $this->template = $template; |
| 53 | $this->data = $data; |
| 54 | } |
| 55 | /** |
| 56 | * Disable layout wrapping for this view. |
| 57 | * |
| 58 | * @return $this |
| 59 | * |
| 60 | * @since 1.0.0 |
| 61 | */ |
| 62 | public function partial() |
| 63 | { |
| 64 | $this->with_layout = \false; |
| 65 | return $this; |
| 66 | } |
| 67 | /** |
| 68 | * Enable or set layout wrapping for this view. |
| 69 | * |
| 70 | * @param bool $enabled Whether layout wrapping is enabled. |
| 71 | * |
| 72 | * @return $this |
| 73 | * |
| 74 | * @since 1.0.0 |
| 75 | */ |
| 76 | public function layout($enabled = \true) |
| 77 | { |
| 78 | $this->with_layout = (bool) $enabled; |
| 79 | return $this; |
| 80 | } |
| 81 | /** |
| 82 | * Merge additional data into the view. |
| 83 | * |
| 84 | * @param array $data The data to merge. |
| 85 | * |
| 86 | * @return $this |
| 87 | * |
| 88 | * @since 1.0.0 |
| 89 | */ |
| 90 | public function with(array $data) |
| 91 | { |
| 92 | $this->data = \array_merge($this->data, $data); |
| 93 | return $this; |
| 94 | } |
| 95 | /** |
| 96 | * Get the template name. |
| 97 | * |
| 98 | * @return string |
| 99 | * |
| 100 | * @since 1.0.0 |
| 101 | */ |
| 102 | public function get_template() |
| 103 | { |
| 104 | return $this->template; |
| 105 | } |
| 106 | /** |
| 107 | * Get the view data. |
| 108 | * |
| 109 | * @return array |
| 110 | * |
| 111 | * @since 1.0.0 |
| 112 | */ |
| 113 | public function get_data() |
| 114 | { |
| 115 | return $this->data; |
| 116 | } |
| 117 | /** |
| 118 | * Whether the view uses layout wrapping. |
| 119 | * |
| 120 | * @return bool |
| 121 | * |
| 122 | * @since 1.0.0 |
| 123 | */ |
| 124 | public function uses_layout() |
| 125 | { |
| 126 | return $this->with_layout; |
| 127 | } |
| 128 | /** |
| 129 | * Render the view to a string. |
| 130 | * |
| 131 | * @return string |
| 132 | * |
| 133 | * @since 1.0.0 |
| 134 | */ |
| 135 | public function render() |
| 136 | { |
| 137 | return app(TemplateEngine::class)->render($this->template, $this->data, $this->with_layout); |
| 138 | } |
| 139 | /** |
| 140 | * Render the view when cast to string. |
| 141 | * |
| 142 | * @return string |
| 143 | * |
| 144 | * @since 1.0.0 |
| 145 | */ |
| 146 | public function __toString() |
| 147 | { |
| 148 | return $this->render(); |
| 149 | } |
| 150 | } |
| 151 |