summaryrefslogtreecommitdiff
path: root/src/Renderer/TwigEngine.php
blob: aa51a177c93bf6bac2b1b37fb33759d532a1bfed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php

namespace Engelsystem\Renderer;

use Twig_Environment as Twig;
use Twig_Error_Loader as LoaderError;
use Twig_Error_Runtime as RuntimeError;
use Twig_Error_Syntax as SyntaxError;

class TwigEngine extends Engine
{
    /** @var Twig */
    protected $twig;

    public function __construct(Twig $twig)
    {
        $this->twig = $twig;
    }

    /**
     * Render a twig template
     *
     * @param string $path
     * @param array  $data
     * @return string
     * @throws LoaderError|RuntimeError|SyntaxError
     */
    public function get(string $path, array $data = []): string
    {
        $data = array_replace_recursive($this->sharedData, $data);

        return $this->twig->render($path, $data);
    }

    /**
     * @param string $path
     * @return bool
     */
    public function canRender(string $path): bool
    {
        return $this->twig->getLoader()->exists($path);
    }
}