Passed
Push — main ( 2519a4...62a0e8 )
by Thierry
03:58
created

Context   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 4
eloc 9
dl 0
loc 47
rs 10
c 2
b 1
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A include() 0 3 1
A render() 0 3 1
A __construct() 0 7 2
1
<?php
2
3
/**
4
 * Context.php - Template context
5
 *
6
 * A context for a template being rendered.
7
 *
8
 * The "$this" var in a template will refer to an instance of this
9
 * class, which will then provide the template variables, and the
10
 * render() method, to render a template inside of another.
11
 *
12
 * @package jaxon-core
13
 * @author Thierry Feuzeu <[email protected]>
14
 * @copyright 2022 Thierry Feuzeu <[email protected]>
15
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
16
 * @link https://github.com/jaxon-php/jaxon-core
17
 */
18
19
namespace Jaxon\Utils\Template;
20
21
class Context
22
{
23
    /**
24
     * @var Engine
25
     */
26
    private $__engine__;
27
28
    /**
29
     * The constructor
30
     *
31
     * @param Engine $engine
32
     * @param array $aVars The template vars
33
     */
34
    public function __construct(Engine $engine, array $aVars)
35
    {
36
        $this->__engine__ = $engine;
37
        foreach($aVars as $sName => $xValue)
38
        {
39
            $sName = (string)$sName;
40
            $this->$sName = $xValue;
41
        }
42
    }
43
44
    /**
45
     * Render a template
46
     *
47
     * @param string $sTemplate The name of template to be rendered
48
     * @param array $aVars The template vars
49
     *
50
     * @return string
51
     */
52
    public function render(string $sTemplate, array $aVars = []): string
53
    {
54
        return $this->__engine__->render($sTemplate, $aVars);
55
    }
56
57
    /**
58
     * Include a template
59
     *
60
     * @param string $sTemplate The name of template to be rendered
61
     * @param array $aVars The template vars
62
     *
63
     * @return void
64
     */
65
    public function include(string $sTemplate, array $aVars = [])
66
    {
67
        echo $this->render($sTemplate, $aVars);
68
    }
69
}
70