1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace League\Plates\Extension\Data; |
4
|
|
|
|
5
|
|
|
use League\Plates; |
6
|
|
|
|
7
|
|
|
/** The DataExtension adds the ability to hydrate data into a template before it gets rendered. */ |
8
|
|
|
final class DataExtension implements Plates\Extension |
9
|
|
|
{ |
10
|
|
|
public function register(Plates\Engine $plates) { |
11
|
|
|
$c = $plates->getContainer(); |
12
|
|
|
$c->add('data.globals', []); |
13
|
|
|
$c->add('data.template_data', []); |
14
|
|
|
$c->merge('config', ['merge_parent_data' => true]); |
15
|
|
|
$c->wrap('compose', function($compose, $c) { |
16
|
|
|
return Plates\Util\compose(...array_filter([ |
17
|
|
|
$c->get('config')['merge_parent_data'] ? mergeParentDataCompose() : null, |
18
|
|
|
$c->get('data.globals') ? globalResolveData($c->get('data.globals')) : null, |
19
|
|
|
$c->get('data.template_data') ? perTemplateResolveData($c->get('data.template_data')) : null, |
20
|
|
|
$compose |
21
|
|
|
])); |
22
|
|
|
}); |
23
|
|
|
|
24
|
|
|
$plates->addMethods([ |
25
|
|
|
'addGlobals' => function(Plates\Engine $e, array $data) { |
26
|
|
|
$c = $e->getContainer(); |
27
|
|
|
$c->merge('data.globals', $data); |
28
|
|
|
}, |
29
|
|
|
'addGlobal' => function(Plates\Engine $e, $name, $value) { |
30
|
|
|
$e->getContainer()->merge('data.globals', [$name => $value]); |
31
|
|
|
}, |
32
|
|
|
'assignTemplateData' => function(Plates\Engine $e, $name, $data) { |
33
|
|
|
$template_data = $e->getContainer()->get('data.template_data'); |
34
|
|
|
if (!isset($template_data[$name])) { |
35
|
|
|
$template_data[$name] = []; |
36
|
|
|
} |
37
|
|
|
$template_data[$name] = array_merge($template_data[$name], $data); |
38
|
|
|
$e->getContainer()->add('data.template_data', $template_data); |
39
|
|
|
} |
40
|
|
|
]); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|