1
|
|
|
<?php |
2
|
|
|
namespace Tuum\View; |
3
|
|
|
|
4
|
|
|
class Section |
5
|
|
|
{ |
6
|
|
|
/** |
7
|
|
|
* do not render section if the section has this value. |
8
|
|
|
*/ |
9
|
|
|
const NO_SECTION_RENDER = false; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
|
|
private $section_data = []; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var int |
18
|
|
|
*/ |
19
|
|
|
private $ob_level = 0; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* start capturing a section. |
23
|
|
|
*/ |
24
|
|
|
public function start() |
25
|
|
|
{ |
26
|
|
|
$this->ob_level ++; |
27
|
|
|
ob_start(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* end capture with name. |
32
|
|
|
* |
33
|
|
|
* @param $name |
34
|
|
|
*/ |
35
|
|
|
public function saveAs($name) |
36
|
|
|
{ |
37
|
|
|
$this->section_data[$name] = ob_get_clean(); |
38
|
|
|
$this->ob_level--; |
39
|
|
|
if ($this->ob_level) { |
40
|
|
|
echo $this->section_data[$name]; |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* get a captured section. |
46
|
|
|
* |
47
|
|
|
* @param string $name |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
|
|
public function get($name) |
51
|
|
|
{ |
52
|
|
|
return array_key_exists($name, $this->section_data) ? $this->section_data[$name] : ''; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $name |
57
|
|
|
* @param string|mixed $data |
58
|
|
|
* @return $this |
59
|
|
|
*/ |
60
|
|
|
public function set($name, $data) |
61
|
|
|
{ |
62
|
|
|
$this->section_data[$name] = $data; |
63
|
|
|
|
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $name |
69
|
|
|
*/ |
70
|
|
|
public function markNotToRender($name) |
71
|
|
|
{ |
72
|
|
|
$this->section_data[$name] = self::NO_SECTION_RENDER; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $name |
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
|
|
public function exists($name) |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
$names = func_get_args(); |
82
|
|
|
foreach ($names as $name) { |
83
|
|
|
if (array_key_exists($name, $this->section_data)) { |
84
|
|
|
return true; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return false; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* render the part of template as $name section. |
93
|
|
|
* will not render if the section is marked as NO_RENDER |
94
|
|
|
* |
95
|
|
|
* @param string $name |
96
|
|
|
*/ |
97
|
|
|
public function renderAs($name) |
98
|
|
|
{ |
99
|
|
|
if ($this->get($name) !== self::NO_SECTION_RENDER) { |
100
|
|
|
echo ob_get_clean(); |
101
|
|
|
} else { |
102
|
|
|
ob_end_clean(); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* render the part of a template only if section $name does not exist. |
108
|
|
|
* |
109
|
|
|
* @param string $name |
110
|
|
|
*/ |
111
|
|
|
public function replaceBy($name) |
112
|
|
|
{ |
113
|
|
|
$content = $this->get($name); |
114
|
|
|
if ($content === self::NO_SECTION_RENDER) { |
115
|
|
|
ob_end_clean(); |
116
|
|
|
|
117
|
|
|
return; // do not render anything. |
118
|
|
|
} elseif ($content) { |
119
|
|
|
ob_end_clean(); |
120
|
|
|
echo $content; |
121
|
|
|
} else { |
122
|
|
|
echo ob_get_clean(); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.