1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Nip\View\Traits; |
6
|
|
|
|
7
|
|
|
use League\Plates\Template\Template; |
8
|
|
|
use function is_string; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class CanRenderTrait. |
12
|
|
|
*/ |
13
|
|
|
trait CanRenderTrait |
14
|
|
|
{ |
15
|
|
|
protected $blocks = []; |
16
|
|
|
|
17
|
|
|
/** @noinspection PhpInconsistentReturnPointsInspection |
18
|
2 |
|
* @param array $variables |
19
|
|
|
* @param bool $return |
20
|
2 |
|
* |
21
|
2 |
|
* @return string|bool |
22
|
2 |
|
*/ |
23
|
|
|
public function load($view, $variables = [], $return = false) |
24
|
2 |
|
{ |
25
|
|
|
$html = $this->getContents($view, $variables); |
|
|
|
|
26
|
|
|
|
27
|
|
|
if (true === $return) { |
28
|
|
|
return $html; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
echo $html; |
32
|
|
|
|
33
|
1 |
|
return null; |
34
|
|
|
} |
35
|
1 |
|
|
36
|
|
|
/** |
37
|
|
|
* @return string |
38
|
|
|
* |
39
|
|
|
* @deprecated use render($view, $variables) |
40
|
|
|
*/ |
41
|
|
|
public function getContents($view, array $variables = []) |
42
|
|
|
{ |
43
|
|
|
return $this->render($view, $variables); |
44
|
|
|
} |
45
|
4 |
|
|
46
|
|
|
/** |
47
|
4 |
|
* @param string $block |
48
|
|
|
*/ |
49
|
4 |
|
public function render($name, array $data = []) |
50
|
|
|
{ |
51
|
|
|
if ($this->isBlock($name)) { |
52
|
|
|
$name = '/' . $this->blocks[$name]; |
53
|
4 |
|
} |
54
|
|
|
|
55
|
4 |
|
return parent::render($name, $data); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Builds path for including |
60
|
|
|
* If $view starts with / the path will be relative to the root of the views folder. |
61
|
|
|
* Otherwise to caller file location. |
62
|
|
|
* |
63
|
4 |
|
* @param string $view |
64
|
|
|
* |
65
|
4 |
|
* @return string |
66
|
|
|
*/ |
67
|
4 |
|
public function buildPath($view) |
68
|
|
|
{ |
69
|
4 |
|
return $this->getResolveTemplatePath()->find($view); |
|
|
|
|
70
|
4 |
|
} |
71
|
|
|
|
72
|
4 |
|
/** |
73
|
4 |
|
* @return mixed |
74
|
4 |
|
*/ |
75
|
|
|
public function getBlock($name) |
76
|
4 |
|
{ |
77
|
|
|
return $this->blocks[$name]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function setBlock($name, $block) |
81
|
|
|
{ |
82
|
|
|
$this->blocks[$name] = $block; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $block |
87
|
|
|
*/ |
88
|
|
|
public function isBlock($block = 'default'): bool |
89
|
|
|
{ |
90
|
|
|
if (!is_string($block)) { |
|
|
|
|
91
|
|
|
return false; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return isset($this->blocks[$block]) && !empty($this->blocks[$block]); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Create a new template. |
99
|
|
|
* |
100
|
|
|
* @param string $name |
101
|
|
|
* |
102
|
|
|
* @return Template |
103
|
|
|
*/ |
104
|
|
|
public function make($name, array $data = []) |
105
|
|
|
{ |
106
|
|
|
$template = new \Nip\View\Template\Template($this, $name); |
|
|
|
|
107
|
|
|
$template->data($data); |
108
|
|
|
return $template; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.