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 */ 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::