Template::renderTemplate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 7
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-ember/
7
 */
8
9
namespace flipbox\craft\ember\views;
10
11
use Craft;
12
use craft\web\View;
13
use yii\base\BaseObject;
14
15
/**
16
 * @author Flipbox Factory <[email protected]>
17
 * @since 2.0.0
18
 */
19
class Template extends BaseObject implements ViewInterface
20
{
21
    /**
22
     * @var string
23
     */
24
    public $mode = View::TEMPLATE_MODE_CP;
25
26
    /**
27
     * @var string
28
     */
29
    public $template = '';
30
31
    /**
32
     * @param array $params
33
     * @return string
34
     *
35
     * @throws \Twig\Error\LoaderError
36
     * @throws \Twig\Error\RuntimeError
37
     * @throws \Twig\Error\SyntaxError
38
     * @throws \yii\base\Exception
39
     */
40
    public function render(array $params = []): string
41
    {
42
        $view = Craft::$app->getView();
43
44
        $currentMode = $view->getTemplateMode();
45
        $view->setTemplateMode($this->mode);
46
47
        $html = $this->renderTemplate($params);
48
49
        $view->setTemplateMode($currentMode);
50
51
        return $html;
52
    }
53
54
    /**
55
     * @param array $params
56
     * @return string
57
     *
58
     * @throws \Twig\Error\LoaderError
59
     * @throws \Twig\Error\RuntimeError
60
     * @throws \Twig\Error\SyntaxError
61
     */
62
    protected function renderTemplate(array $params = []): string
63
    {
64
        return Craft::$app->getView()->renderTemplate(
65
            $this->template,
66
            $params
67
        );
68
    }
69
}
70