TwigTemplate   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 4
c 4
b 0
f 1
lcom 1
cbo 2
dl 0
loc 50
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A setContext() 0 4 1
A toHtml() 0 5 1
A getHtml() 0 4 1
1
<?php
2
namespace Mouf\Html\Renderer\Twig;
3
4
use Mouf\Html\HtmlElement\HtmlElementInterface;
5
use Mouf\Utils\Value\MapValueInterface;
6
use Mouf\Utils\Value\ValueUtils;
7
use Mouf\Utils\Value\ValueInterface;
8
9
/**
10
 * This class represents a Twig template that can be rendered, using the toHtml() method.
11
 * See Twig documentation for more information about Twig templates:
12
 * 	http://twig.sensiolabs.org
13
 *
14
 * @author David Negrier <[email protected]>
15
 */
16
class TwigTemplate implements HtmlElementInterface
17
{
18
19
    private $environment;
20
    private $template;
21
    private $context;
22
23
    /**
24
     * @param \Twig_Environment                                            $environment The twig environment.
25
     * @param string                                                       $template    The name of the twig file to be rendered. This is relative to the directory defined by the Twig_Environment (usually ROOT_PATH).
26
     * @param array<string, ValueInterface>|MapValueInterface|array|object $context     The context passed to the template
27
     */
28
    public function __construct(\Twig_Environment $environment, $template, $context = array())
29
    {
30
        $this->environment = $environment;
31
        $this->template = $template;
32
        $this->context = $context;
33
    }
34
35
    /**
36
     * Sets the context for this template.
37
     * This can be anything from on array to an object to a ValueInterface...
38
     *
39
     * @param array<string, ValueInterface>|MapValueInterface|array|object $context
40
     */
41
    public function setContext($context)
42
    {
43
        $this->context = $context;
44
    }
45
46
    /**
47
     * (non-PHPdoc)
48
     * @see \Mouf\Html\HtmlElement\HtmlElementInterface::toHtml()
49
     */
50
    public function toHtml()
51
    {
52
        $context = ValueUtils::val($this->context);
53
        $this->environment->display($this->template, $context);
54
    }
55
56
    /**
57
     * Same function as toHtml() but we return Html instead of echo it
58
     *
59
     * @return string
60
     */
61
    public function getHtml()
62
    {
63
        return $this->environment->render($this->template, ValueUtils::val($this->context));
64
    }
65
}
66