Completed
Push — master ( e3663d...ab0db8 )
by Alex
03:22
created

ViewStatic::render()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 7
c 1
b 0
f 1
nc 4
nop 1
dl 0
loc 15
rs 10
1
<?php
2
namespace Mezon\Application;
3
4
use Mezon\HtmlTemplate\HtmlTemplate;
5
6
/**
7
 * Class ViewStatic
8
 *
9
 * @package Mezon
10
 * @subpackage View
11
 * @author Dodonov A.A.
12
 * @version v.1.0 (2019/08/06)
13
 * @copyright Copyright (c) 2019, aeon.org
14
 */
15
16
/**
17
 * Class outputs static block by it's name
18
 */
19
class ViewStatic extends ViewBase
20
{
21
22
    /**
23
     * Block name
24
     *
25
     * @var string
26
     */
27
    private $blockName = '';
28
29
    /**
30
     * Constructor
31
     *
32
     * @param HtmlTemplate $template
33
     *            template
34
     * @param string $blockName
35
     *            View's block name to be rendered
36
     */
37
    public function __construct(HtmlTemplate $template, string $blockName = '')
38
    {
39
        parent::__construct($template);
40
41
        $this->blockName = $blockName;
42
    }
43
44
    /**
45
     * Method renders content from view
46
     *
47
     * @param string $blockName
48
     *            name of the block to be rendered
49
     * @return string Generated content
50
     */
51
    public function render(string $blockName = ''): string
52
    {
53
        if ($blockName === '') {
54
            $blockName = $this->blockName;
55
        }
56
57
        if ($blockName === '') {
58
            throw (new \Exception('Block name must be set', - 1));
59
        }
60
61
        $content = $this->getTemplate()->getBlock($blockName);
62
63
        $this->getTemplate()->compilePageVars($content);
64
65
        return $content;
66
    }
67
68
    /**
69
     * Method returns view name
70
     *
71
     * @return string view name
72
     */
73
    public function getViewName(): string
74
    {
75
        return $this->blockName;
76
    }
77
}
78