Completed
Push — 2.1 ( 21da0e...a39d12 )
by
unknown
10:45
created

Block::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

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