-
Notifications
You must be signed in to change notification settings - Fork 1
/
SirTrevor.php
68 lines (57 loc) · 1.83 KB
/
SirTrevor.php
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
namespace IrishDistillers\SirTrevorTwig;
use Symfony\Bundle\TwigBundle\TwigEngine;
class SirTrevor
{
/**
* @var Symfony\Bundle\TwigBundle\TwigEngine
*/
protected $templating = null;
/**
* @var array
*/
protected $loadedBlocks = [];
/**
* @var array
*/
protected $defaultOptions = [
'mobile' => false
];
public function __construct(TwigEngine $templating = null)
{
$this->templating = $templating;
}
public function render(\stdClass $content, array $options = [], \Twig_Environment $environment = null) : string
{
if (is_null($environment)) {
$environment = $this->templating;
}
return implode(
"\n",
array_map(
function ($block) use ($environment, $options) {
try {
// Deep copy hack, otherwise the rand value gets overwritten
$block = unserialize(serialize($block));
$block->data->rand = mt_rand();
$this->loadedBlocks[] = $block;
return $environment->render(
'SirTrevorTwig:_snippets:sirtrevor/'.$block->type.'.html.twig',
[
'data' => $block->data,
'options' => array_merge($this->defaultOptions, $options)
]
);
} catch (\Twig_Error_Loader $exception) {
return sprintf('<!-- Snippet type: "%s" not found -->', $block->type);
}
},
$content->data
)
);
}
public function getLoadedBlocks() : array
{
return $this->loadedBlocks;
}
}