Skip to content

Commit

Permalink
Merge pull request #2005 from codeigniter4/pluginclosures
Browse files Browse the repository at this point in the history
Plugin closures docs update and test
  • Loading branch information
lonnieezell authored May 15, 2019
2 parents 2133346 + 3319a8d commit 2533f79
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
19 changes: 19 additions & 0 deletions tests/system/View/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
18 changes: 17 additions & 1 deletion user_guide_src/source/outgoing/view_parser.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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::

Expand Down Expand Up @@ -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::

Expand Down

0 comments on commit 2533f79

Please sign in to comment.