From 9fd98b7b3824562c0eeaf95a307b41182a6dbf82 Mon Sep 17 00:00:00 2001 From: RJ Garcia Date: Sun, 14 Jan 2018 22:49:32 -0800 Subject: [PATCH] Added helpful funcs for DX #194 - `data` func now accepts an array of data and will merge with the current templates data for returning - Updated the func extension API to make it easier to add simple functions Signed-off-by: RJ Garcia --- .../LayoutSectionsExtension.php | 6 ++--- .../RenderContext/RenderContextExtension.php | 25 ++++++++++++++++++- src/Extension/RenderContext/func.php | 18 +++++++++++++ 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/Extension/LayoutSections/LayoutSectionsExtension.php b/src/Extension/LayoutSections/LayoutSectionsExtension.php index e764649e..bc7ad94c 100644 --- a/src/Extension/LayoutSections/LayoutSectionsExtension.php +++ b/src/Extension/LayoutSections/LayoutSectionsExtension.php @@ -22,17 +22,17 @@ public function register(Plates\Engine $plates) { $factories[] = LayoutRenderTemplate::factory(); return $factories; }); - $c->wrap('renderContext.func.funcs', function($funcs, $c) { + $plates->addFuncs(function($c) { $template_args = RenderContext\assertTemplateArgsFunc(); $one_arg = RenderContext\assertArgsFunc(1); - return array_merge($funcs, [ + return [ 'layout' => [$template_args, layoutFunc()], 'section' => [$one_arg, sectionFunc()], 'start' => [$one_arg, startFunc()], 'push' => [$one_arg, startFunc(START_APPEND)], 'unshift' => [$one_arg, startFunc(START_PREPEND)], - ]); + ]; }); } } diff --git a/src/Extension/RenderContext/RenderContextExtension.php b/src/Extension/RenderContext/RenderContextExtension.php index ffe12330..11529c38 100644 --- a/src/Extension/RenderContext/RenderContextExtension.php +++ b/src/Extension/RenderContext/RenderContextExtension.php @@ -36,7 +36,7 @@ public function register(Plates\Engine $plates) { ? escapeFunc($config['escape_flags'], $config['escape_encoding']) : escapeFunc() ], - 'data' => [accessTemplatePropFunc('data')], + 'data' => [assertArgsFunc(0, 1), templateDataFunc()], 'name' => [accessTemplatePropFunc('name')], 'context' => [accessTemplatePropFunc('context')], 'component' => [$template_args, componentFunc()], @@ -62,5 +62,28 @@ function() use ($c) { return $c->get('renderTemplate'); }, $c->get('renderContext.func') ); }); + + $plates->addMethods([ + 'registerFunction' => function(Plates\Engine $e, $name, callable $func, callable $assert_args = null, $simple = true) { + $c = $e->getContainer(); + $func = $simple ? wrapSimpleFunc($func) : $func; + + $c->wrap('renderContext.func.funcs', function($funcs, $c) use ($name, $func, $assert_args) { + $funcs[$name] = $assert_args ? [$assert_args, $func] : [$func]; + return $funcs; + }); + }, + 'addFuncs' => function(Plates\Engine $e, callable $add_funcs, $simple = false) { + $e->getContainer()->wrap('renderContext.func.funcs', function($funcs, $c) use ($add_funcs, $simple) { + $new_funcs = $simple + ? array_map(wrapSimpleFunc::class, $add_funcs($c)) + : $add_funcs($c); + return array_merge($funcs, $new_funcs); + }); + }, + 'wrapFuncs' => function(Plates\Engine $e, callable $wrap_funcs) { + $e->getContainer()->wrap('renderContext.func.funcs', $wrap_funcs); + } + ]); } } diff --git a/src/Extension/RenderContext/func.php b/src/Extension/RenderContext/func.php index a3c41aac..727a5f2b 100644 --- a/src/Extension/RenderContext/func.php +++ b/src/Extension/RenderContext/func.php @@ -85,6 +85,24 @@ function insertFunc($echo = null) { }; } +function templateDataFunc() { + return function(FuncArgs $args) { + list($data) = $args->args; + return array_merge($args->template()->data, $data); + }; +} + +/** Enables the backwards compatibility with the old extension functions */ +function wrapSimpleFunc(callable $func, $enable_bc = false) { + return function(FuncArgs $args) use ($func, $enable_bc) { + if ($enable_bc && is_array($func) && isset($func[0]) && $func[0] instanceof Plates\Extension\ExtensionInterface) { + $func[0]->template = $args->template(); + } + + return $func(...$args->args); + }; +} + function accessTemplatePropFunc($prop) { return function(FuncArgs $args) use ($prop) { return $args->template()->{$prop};