|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Nip\View\Traits; |
|
5
|
|
|
|
|
6
|
|
|
use League\Plates\Template\Template; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class CanRenderTrait |
|
10
|
|
|
* @package Nip\View\Traits |
|
11
|
|
|
*/ |
|
12
|
|
|
trait CanRenderTrait |
|
13
|
|
|
{ |
|
14
|
|
|
protected $blocks = []; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @param bool|Callable $condition |
|
18
|
2 |
|
* @param $view |
|
19
|
|
|
* @param array $variables |
|
20
|
2 |
|
* @param bool $return |
|
21
|
2 |
|
* @return bool|string|void|null |
|
22
|
2 |
|
*/ |
|
23
|
|
|
public function loadIf($condition, $view, $variables = [], $return = false) |
|
24
|
2 |
|
{ |
|
25
|
|
|
$condition = is_callable($condition) ? $condition() : $condition; |
|
26
|
|
|
if ($condition == false) { |
|
27
|
|
|
return; |
|
28
|
|
|
} |
|
29
|
|
|
return $this->load($view, $variables, $return); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
1 |
|
* @param $view |
|
34
|
|
|
* @param array $variables |
|
35
|
1 |
|
* @param bool $return |
|
36
|
|
|
* @return bool|string|void|null |
|
37
|
|
|
*/ |
|
38
|
|
|
public function loadIfExists($view, $variables = [], $return = false) |
|
39
|
|
|
{ |
|
40
|
|
|
return $this->loadIf($this->existPath($view), $view, $variables, $return); |
|
|
|
|
|
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param $view |
|
45
|
4 |
|
* @param array $variables |
|
46
|
|
|
* @param bool $return |
|
47
|
4 |
|
* @return bool|string|void|null |
|
48
|
|
|
*/ |
|
49
|
4 |
|
public function loadWithFallback($view, $fallback, $variables = [], $return = false) |
|
50
|
|
|
{ |
|
51
|
|
|
$view = $this->existPath($view) ? $view : $fallback; |
|
52
|
|
|
return $this->load($view, $variables, $return); |
|
53
|
4 |
|
} |
|
54
|
|
|
|
|
55
|
4 |
|
/** @noinspection PhpInconsistentReturnPointsInspection |
|
56
|
|
|
* |
|
57
|
|
|
* @param $view |
|
58
|
|
|
* @param array $variables |
|
59
|
|
|
* @param bool $return |
|
60
|
|
|
* @return string|boolean |
|
61
|
|
|
*/ |
|
62
|
|
|
public function load($view, $variables = [], $return = false) |
|
63
|
4 |
|
{ |
|
64
|
|
|
$html = $this->getContents($view, $variables); |
|
|
|
|
|
|
65
|
4 |
|
|
|
66
|
|
|
if ($return === true) { |
|
67
|
4 |
|
return $html; |
|
68
|
|
|
} |
|
69
|
4 |
|
|
|
70
|
4 |
|
echo $html; |
|
71
|
|
|
|
|
72
|
4 |
|
return null; |
|
73
|
4 |
|
} |
|
74
|
4 |
|
|
|
75
|
|
|
/** |
|
76
|
4 |
|
* @param $view |
|
77
|
|
|
* @param array $variables |
|
78
|
|
|
* @return string |
|
79
|
|
|
* @deprecated use render($view, $variables) |
|
80
|
|
|
*/ |
|
81
|
|
|
public function getContents($view, array $variables = []) |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->render($view, $variables); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param string $block |
|
88
|
|
|
*/ |
|
89
|
|
|
public function render($name, array $data = []) |
|
90
|
|
|
{ |
|
91
|
|
|
if ($this->isBlock($name)) { |
|
92
|
|
|
$name = "/" . $this->blocks[$name]; |
|
93
|
|
|
} |
|
94
|
|
|
return parent::render($name, $data); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Builds path for including |
|
99
|
|
|
* If $view starts with / the path will be relative to the root of the views folder. |
|
100
|
|
|
* Otherwise to caller file location. |
|
101
|
|
|
* |
|
102
|
|
|
* @param string $view |
|
103
|
|
|
* @return string |
|
104
|
|
|
*/ |
|
105
|
|
|
public function buildPath($view) |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->getResolveTemplatePath()->find($view); |
|
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param $name |
|
112
|
|
|
* @return mixed |
|
113
|
|
|
*/ |
|
114
|
|
|
public function getBlock($name) |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->blocks[$name]; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param $name |
|
121
|
|
|
* @param $block |
|
122
|
|
|
*/ |
|
123
|
|
|
public function setBlock($name, $block) |
|
124
|
|
|
{ |
|
125
|
|
|
$this->blocks[$name] = $block; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @param string $block |
|
130
|
|
|
* @return bool |
|
131
|
|
|
*/ |
|
132
|
|
|
public function isBlock($block = 'default'): bool |
|
133
|
|
|
{ |
|
134
|
|
|
if (!is_string($block)) { |
|
|
|
|
|
|
135
|
|
|
return false; |
|
136
|
|
|
} |
|
137
|
|
|
return isset($this->blocks[$block]) && !empty($this->blocks[$block]); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Create a new template. |
|
143
|
|
|
* @param string $name |
|
144
|
|
|
* @return Template |
|
145
|
|
|
*/ |
|
146
|
|
|
public function make($name) |
|
147
|
|
|
{ |
|
148
|
|
|
return new \Nip\View\Template\Template($this, $name); |
|
|
|
|
|
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|