|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace League\Plates\Extension\RenderContext; |
|
4
|
|
|
|
|
5
|
|
|
use League\Plates; |
|
6
|
|
|
|
|
7
|
|
|
/** The render context extension provides a RenderContext object and functions to be used within the render context object. This RenderContext object is injected into the template data to allow usefulness in the templates. */ |
|
8
|
|
|
final class RenderContextExtension implements Plates\Extension |
|
9
|
|
|
{ |
|
10
|
24 |
|
public function register(Plates\Engine $plates) { |
|
11
|
24 |
|
$c = $plates->getContainer(); |
|
12
|
24 |
|
$c->addStack('renderContext.func', function($c) { |
|
13
|
|
|
return [ |
|
14
|
24 |
|
'notFound' => notFoundFunc(), |
|
15
|
24 |
|
'plates' => Plates\Util\stackGroup([ |
|
16
|
24 |
|
splitByNameFunc($c->get('renderContext.func.funcs')), |
|
17
|
24 |
|
aliasNameFunc($c->get('renderContext.func.aliases')), |
|
18
|
|
|
]), |
|
19
|
|
|
]; |
|
20
|
24 |
|
}); |
|
21
|
24 |
|
$c->add('renderContext.func.aliases', [ |
|
22
|
24 |
|
'e' => 'escape', |
|
23
|
|
|
'__invoke' => 'escape', |
|
24
|
|
|
'stop' => 'end', |
|
25
|
|
|
]); |
|
26
|
24 |
|
$c->add('renderContext.func.funcs', function($c) { |
|
27
|
24 |
|
$template_args = assertTemplateArgsFunc(); |
|
28
|
24 |
|
$one_arg = assertArgsFunc(1); |
|
29
|
24 |
|
$config = $c->get('config'); |
|
30
|
|
|
|
|
31
|
|
|
return [ |
|
32
|
24 |
|
'insert' => [insertFunc(), $template_args], |
|
33
|
|
|
'escape' => [ |
|
34
|
24 |
|
isset($config['escape_flags'], $config['escape_encoding']) |
|
35
|
|
|
? escapeFunc($config['escape_flags'], $config['escape_encoding']) |
|
36
|
24 |
|
: escapeFunc(), |
|
37
|
24 |
|
$one_arg, |
|
38
|
|
|
], |
|
39
|
24 |
|
'data' => [templateDataFunc(), assertArgsFunc(0, 1)], |
|
40
|
24 |
|
'name' => [accessTemplatePropFunc('name')], |
|
41
|
24 |
|
'context' => [accessTemplatePropFunc('context')], |
|
42
|
24 |
|
'component' => [componentFunc(), $template_args], |
|
43
|
24 |
|
'slot' => [slotFunc(), $one_arg], |
|
44
|
24 |
|
'end' => [endFunc()] |
|
45
|
|
|
]; |
|
46
|
24 |
|
}); |
|
47
|
24 |
|
$c->add('include.bind', function($c) { |
|
48
|
|
|
return renderContextBind($c->get('config')['render_context_var_name']); |
|
49
|
24 |
|
}); |
|
50
|
24 |
|
$c->add('renderContext.factory', function($c) { |
|
51
|
24 |
|
return RenderContext::factory( |
|
52
|
|
|
function() use ($c) { return $c->get('renderTemplate'); }, |
|
53
|
24 |
|
$c->get('renderContext.func') |
|
54
|
|
|
); |
|
55
|
24 |
|
}); |
|
56
|
|
|
|
|
57
|
24 |
|
$plates->defineConfig([ |
|
|
|
|
|
|
58
|
24 |
|
'render_context_var_name' => 'v', |
|
59
|
|
|
'escape_encoding' => null, |
|
60
|
|
|
'escape_flags' => null, |
|
61
|
|
|
]); |
|
62
|
24 |
|
$plates->pushComposers(function($c) { |
|
|
|
|
|
|
63
|
|
|
return [ |
|
64
|
24 |
|
'renderContext.renderContext' => renderContextCompose( |
|
65
|
24 |
|
$c->get('renderContext.factory'), |
|
66
|
24 |
|
$c->get('config')['render_context_var_name'] |
|
67
|
|
|
) |
|
68
|
|
|
]; |
|
69
|
24 |
|
}); |
|
70
|
|
|
|
|
71
|
24 |
|
$plates->addMethods([ |
|
72
|
24 |
|
'registerFunction' => function(Plates\Engine $e, $name, callable $func, callable $assert_args = null, $simple = true) { |
|
73
|
|
|
$c = $e->getContainer(); |
|
74
|
|
|
$func = $simple ? wrapSimpleFunc($func) : $func; |
|
75
|
|
|
|
|
76
|
|
|
$c->wrap('renderContext.func.funcs', function($funcs, $c) use ($name, $func, $assert_args) { |
|
|
|
|
|
|
77
|
|
|
$funcs[$name] = $assert_args ? [$assert_args, $func] : [$func]; |
|
78
|
|
|
return $funcs; |
|
79
|
|
|
}); |
|
80
|
24 |
|
}, |
|
81
|
|
|
'addFuncs' => function(Plates\Engine $e, callable $add_funcs, $simple = false) { |
|
82
|
24 |
|
$e->getContainer()->wrap('renderContext.func.funcs', function($funcs, $c) use ($add_funcs, $simple) { |
|
83
|
24 |
|
$new_funcs = $simple |
|
84
|
|
|
? array_map(wrapSimpleFunc::class, $add_funcs($c)) |
|
85
|
24 |
|
: $add_funcs($c); |
|
86
|
24 |
|
return array_merge($funcs, $new_funcs); |
|
87
|
24 |
|
}); |
|
88
|
24 |
|
}, |
|
89
|
24 |
|
'wrapFuncs' => function(Plates\Engine $e, callable $wrap_funcs) { |
|
90
|
|
|
$e->getContainer()->wrap('renderContext.func.funcs', $wrap_funcs); |
|
91
|
24 |
|
} |
|
92
|
|
|
]); |
|
93
|
24 |
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: