Passed
Pull Request — master (#20134)
by Wilmer
10:17 queued 29s
created

Block   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 44.44%

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 31
ccs 4
cts 9
cp 0.4444
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 6 1
A run() 0 7 2
1
<?php
2
3
/**
4
 * @link https://www.yiiframework.com/
5
 * @copyright Copyright (c) 2008 Yii Software LLC
6
 * @license https://www.yiiframework.com/license/
7
 */
8
9
namespace yii\widgets;
10
11
use yii\base\Widget;
12
13
/**
14
 * Block records all output between [[begin()]] and [[end()]] calls and stores it in [[\yii\base\View::$blocks]].
15
 * for later use.
16
 *
17
 * [[\yii\base\View]] component contains two methods [[\yii\base\View::beginBlock()]] and [[\yii\base\View::endBlock()]].
18
 * The general idea is that you're defining block default in a view or layout:
19
 *
20
 * ```php
21
 * <?php $this->beginBlock('messages', true) ?>
22
 * Nothing.
23
 * <?php $this->endBlock() ?>
24
 * ```
25
 *
26
 * And then overriding default in sub-views:
27
 *
28
 * ```php
29
 * <?php $this->beginBlock('username') ?>
30
 * Umm... hello?
31
 * <?php $this->endBlock() ?>
32
 * ```
33
 *
34
 * Second parameter defines if block content should be outputted which is desired when rendering its content but isn't
35
 * desired when redefining it in subviews.
36
 *
37
 * @author Qiang Xue <[email protected]>
38
 * @since 2.0
39
 */
40
class Block extends Widget
41
{
42
    /**
43
     * @var bool whether to render the block content in place. Defaults to false,
44
     * meaning the captured block content will not be displayed.
45
     */
46
    public $renderInPlace = false;
47
48
49
    /**
50
     * Starts recording a block.
51
     */
52 1
    public function init()
53
    {
54 1
        parent::init();
55
56 1
        ob_start();
57 1
        ob_implicit_flush(false);
58
    }
59
60
    /**
61
     * Ends recording a block.
62
     * This method stops output buffering and saves the rendering result as a named block in the view.
63
     */
64
    public function run()
65
    {
66
        $block = ob_get_clean();
67
        if ($this->renderInPlace) {
68
            echo $block;
69
        }
70
        $this->view->blocks[$this->getId()] = $block;
71
    }
72
}
73