Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugin closures docs update and test #2005

Merged
merged 2 commits into from
May 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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