1: <?php
2:
3: namespace Charcoal\View\Twig;
4:
5: // 3rd-party libraries (`twigphp/twig`) dependencies
6: use \Twig_Environment;
7:
8: // Intra-module (`charcoal-view`) depentencies
9: use \Charcoal\View\AbstractEngine;
10:
11: /**
12: *
13: */
14: class TwigEngine extends AbstractEngine
15: {
16: /**
17: * @var Twig_Environment $twig
18: */
19: private $twig;
20:
21: /**
22: * @return string
23: */
24: public function type()
25: {
26: return 'twig';
27: }
28:
29: /**
30: * @return Twig_Environment
31: */
32: public function twig()
33: {
34: if ($this->twig === null) {
35: $this->twig = $this->createTwig();
36: }
37: return $this->twig;
38: }
39:
40: /**
41: * @return Twig_Environment
42: */
43: protected function createTwig()
44: {
45: $twig = new Twig_Environment($this->loader(), [
46: 'cache' => 'twig_cache',
47: 'charset' => 'utf-8',
48: 'debug' => false
49: ]);
50:
51: return $twig;
52: }
53:
54: /**
55: * @param string $templateIdent The template identifier to load and render.
56: * @param mixed $context The rendering context.
57: * @return string The rendered template string.
58: */
59: public function render($templateIdent, $context)
60: {
61: return $this->twig()->render($templateIdent, $context);
62: }
63:
64: /**
65: * @param string $templateString The template string to render.
66: * @param mixed $context The rendering context.
67: * @return string The rendered template string.
68: */
69: public function renderTemplate($templateString, $context)
70: {
71: $template = $this->twig()->createTemplate($templateString);
72: return $template->render($context);
73: }
74: }
75: