From 67135008db58879c81b93ecc67a4666d46e009ff Mon Sep 17 00:00:00 2001 From: Lonnie Ezell Date: Tue, 14 May 2019 22:22:01 -0500 Subject: [PATCH 1/2] Added test to ensure view parser plugins works when defined as closure. --- tests/system/View/ParserTest.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/system/View/ParserTest.php b/tests/system/View/ParserTest.php index dc4d1891bd3d..e6e88e70ec5d 100644 --- a/tests/system/View/ParserTest.php +++ b/tests/system/View/ParserTest.php @@ -747,6 +747,25 @@ public function testParserPluginNoParams() //-------------------------------------------------------------------- + /** + * @group parserplugins + */ + public function testParserPluginClosure() + { + $config = $this->config; + $config->plugins['hello'] = function (array $params = []) { + return 'Hello, ' . trim($params[0]); + }; + + $parser = new Parser($config, $this->viewsDir, $this->loader); + + $template = '{+ hello world +}'; + + $this->assertEquals('Hello, world', $parser->renderString($template)); + } + + //-------------------------------------------------------------------- + /** * @group parserplugins */ From 3319a8d70bc9b1125a4c88fccce2e7ba285d7958 Mon Sep 17 00:00:00 2001 From: Lonnie Ezell Date: Tue, 14 May 2019 22:29:38 -0500 Subject: [PATCH 2/2] Update docs for view parser plugins. Closes #1997 --- user_guide_src/source/outgoing/view_parser.rst | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/user_guide_src/source/outgoing/view_parser.rst b/user_guide_src/source/outgoing/view_parser.rst index efd9a5c63f7e..240852999949 100644 --- a/user_guide_src/source/outgoing/view_parser.rst +++ b/user_guide_src/source/outgoing/view_parser.rst @@ -478,7 +478,7 @@ callable:: PHP Native functions as Filters ------------------------------- -You can easily use native php function as filters by editing **app/Config/View.php** and adding new entries to the +You can use native php function as filters by editing **app/Config/View.php** and adding new entries to the ``$filters`` array.Each key is the name of the native PHP function is called by in the view, and its value is any valid native PHP function prefixed with:: @@ -543,6 +543,22 @@ used within the template file. The value is any valid PHP callable, including st }, ]; +Any closures that are being used must be defined in the config file's constructor:: + + class View extends \CodeIgniter\Config\View + { + public $plugins = []; + + public function __construct() + { + $this->plugins['bar'] = function(array $params=[]) { + return $params[0] ?? ''; + }; + + parent::__construct(); + } + } + If the callable is on its own, it is treated as a single tag, not a open/close one. It will be replaced by the return value from the plugin::