1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Widgets; |
6
|
|
|
|
7
|
|
|
use function ob_get_clean; |
8
|
|
|
use function ob_implicit_flush; |
9
|
|
|
|
10
|
|
|
use function ob_start; |
11
|
|
|
use Yiisoft\View\WebView; |
12
|
|
|
use Yiisoft\Widget\Widget; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Block records all output between {@see begin()} and {@see end()} calls and stores it in |
16
|
|
|
* |
17
|
|
|
* The general idea is that you're defining block default in a view or layout: |
18
|
|
|
* |
19
|
|
|
* ```php |
20
|
|
|
* <?php $this->beginBlock('index') ?> |
21
|
|
|
* Nothing. |
22
|
|
|
* <?php $this->endBlock() ?> |
23
|
|
|
* ``` |
24
|
|
|
* |
25
|
|
|
* And then overriding default in views: |
26
|
|
|
* |
27
|
|
|
* ```php |
28
|
|
|
* <?php $this->beginBlock('index') ?> |
29
|
|
|
* Umm... hello? |
30
|
|
|
* <?php $this->endBlock() ?> |
31
|
|
|
* ``` |
32
|
|
|
* |
33
|
|
|
* in subviews show block: |
34
|
|
|
* |
35
|
|
|
* <?= $this->getBlock('index') ?> |
36
|
|
|
* |
37
|
|
|
* Second parameter defines if block content should be outputted which is desired when rendering its content but isn't |
38
|
|
|
* desired when redefining it in subviews. |
39
|
|
|
*/ |
40
|
|
|
final class Block extends Widget |
41
|
|
|
{ |
42
|
|
|
private string $id; |
43
|
|
|
private bool $renderInPlace = false; |
44
|
|
|
private WebView $webView; |
45
|
|
|
|
46
|
2 |
|
public function __construct(WebView $webView) |
47
|
|
|
{ |
48
|
2 |
|
$this->webView = $webView; |
49
|
2 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Starts recording a block. |
53
|
|
|
*/ |
54
|
2 |
|
public function begin(): ?string |
55
|
|
|
{ |
56
|
2 |
|
parent::begin(); |
57
|
2 |
|
ob_start(); |
58
|
2 |
|
PHP_VERSION_ID >= 80000 ? ob_implicit_flush(false) : ob_implicit_flush(0); |
|
|
|
|
59
|
2 |
|
return null; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Ends recording a block. |
64
|
|
|
* This method stops output buffering and saves the rendering result as a named block in the view. |
65
|
|
|
* |
66
|
|
|
* @return string the result of widget execution to be outputted. |
67
|
|
|
*/ |
68
|
2 |
|
public function run(): string |
69
|
|
|
{ |
70
|
2 |
|
$block = ob_get_clean(); |
71
|
|
|
|
72
|
2 |
|
if ($this->renderInPlace) { |
73
|
1 |
|
return $block; |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
if (!empty($block)) { |
77
|
1 |
|
$this->webView->setBlock($this->id, $block); |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
return ''; |
81
|
|
|
} |
82
|
|
|
|
83
|
2 |
|
public function id(string $value): self |
84
|
|
|
{ |
85
|
2 |
|
$this->id = $value; |
86
|
|
|
|
87
|
2 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param bool $value whether to render the block content in place. Defaults to false, meaning the captured block |
92
|
|
|
* content will not be displayed. |
93
|
|
|
* |
94
|
|
|
* @return $this |
95
|
|
|
*/ |
96
|
1 |
|
public function renderInPlace(bool $value): self |
97
|
|
|
{ |
98
|
1 |
|
$this->renderInPlace = $value; |
99
|
|
|
|
100
|
1 |
|
return $this; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|