Skip to content

Commit e4438f7

Browse files
authored
Merge pull request #7457 from kenjis/fix-docs-view-cells
docs: fix view_cells.rst
2 parents fe7e2e4 + 99451b8 commit e4438f7

1 file changed

Lines changed: 31 additions & 24 deletions

File tree

user_guide_src/source/outgoing/view_cells.rst

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,19 @@ Calling a View Cell
1919
No matter which type of View Cell you are using, you can call it from any view by using the ``view_cell()`` helper method. The first parameter is the name of the class and method to call, and the second parameter is an array of parameters to pass to the method. The method must return a string, which will be inserted into the view where the ``view_cell()`` method was called.
2020
::
2121

22-
<?= view_cell('App\Cells\MyClass::myMethod', ['param1' => 'value1', 'param2' => 'value2']); ?>
22+
<?= view_cell('App\Cells\MyClass::myMethod', ['param1' => 'value1', 'param2' => 'value2']) ?>
2323

24-
If you do not include the full namespace for the class, it will assume in can be found in the ``App\Cells`` namespace. So, the above example would attempt to find the ``MyClass`` class in ``app/Cells/MyClass.php``. If it is not found there, all namespaces will be scanned until it is found, searching within a ``Cells`` subdirectory of each namespace.
24+
If you do not include the full namespace for the class, it will assume in can be found in the ``App\Cells`` namespace. So, the following example would attempt to find the ``MyClass`` class in ``app/Cells/MyClass.php``. If it is not found there, all namespaces will be scanned until it is found, searching within a ``Cells`` subdirectory of each namespace.
2525
::
2626

27-
<?= view_cell('MyClass::myMethod', ['param1' => 'value1', 'param2' => 'value2']); ?>
27+
<?= view_cell('MyClass::myMethod', ['param1' => 'value1', 'param2' => 'value2']) ?>
28+
29+
.. note:: Namespace omission is available since v4.3.0 and later.
2830

2931
You can also pass the parameters along as a key/value string:
3032
::
3133

32-
<?= view_cell('MyClass::myMethod', 'param1=value1, param2=value2'); ?>
34+
<?= view_cell('MyClass::myMethod', 'param1=value1, param2=value2') ?>
3335

3436
************
3537
Simple Cells
@@ -42,7 +44,7 @@ Simple Cells are classes that return a string from the chosen method. An example
4244

4345
class AlertMessage
4446
{
45-
public function show($params): string
47+
public function show(array $params): string
4648
{
4749
return "<div class="alert alert-{$params['type']}">{$params['message']}</div>";
4850
}
@@ -51,13 +53,15 @@ Simple Cells are classes that return a string from the chosen method. An example
5153
You would call it from within a view like:
5254
::
5355

54-
<?= view_cell('AlertMessage::show', ['type' => 'success', 'message' => 'The user has been updated.']); ?>
56+
<?= view_cell('AlertMessage::show', ['type' => 'success', 'message' => 'The user has been updated.']) ?>
5557

5658
Additionally, you can use parameter names that match the parameter variables in the method for better readability.
5759
When you use it this way, all of the parameters must always be specified in the view cell call::
5860

59-
<?= view_cell('\App\Libraries\Blog::recentPosts', 'category=codeigniter, limit=5') ?>
61+
// In a View.
62+
<?= view_cell('Blog::recentPosts', 'category=codeigniter, limit=5') ?>
6063

64+
// In a Cell.
6165
public function recentPosts(string $category, int $limit)
6266
{
6367
$posts = $this->blogModel->where('category', $category)
@@ -74,6 +78,8 @@ When you use it this way, all of the parameters must always be specified in the
7478
Controlled Cells
7579
****************
7680

81+
.. versionadded:: 4.3.0
82+
7783
Controlled Cells have two primary goals: to make it as fast as possible to build the cell, and provide additional logic and flexibility to your views, if they need it. The class must extend ``CodeIgniter\View\Cells\Cell``. They should have a view file in the same folder. By convention the class name should be PascalCase and the view should be the snake_cased version of the class name. So, for example, if you have a ``MyCell`` class, the view file should be ``my_cell.php``.
7884

7985
Creating a Controlled Cell
@@ -94,22 +100,23 @@ At the most basic level, all you need to implement within the class are public p
94100
}
95101

96102
// app/Cells/alert_message_cell.php
97-
<div class="alert alert-<?= $type; ?>">
98-
<?= $message; ?>
103+
<div class="alert alert-<?= esc($type, 'attr') ?>">
104+
<?= esc($message) ?>
99105
</div>
100106

107+
// Called in main View:
108+
<?= view_cell('AlertMessageCell', 'type=warning, message=Failed.') ?>
109+
101110
.. _generating-cell-via-command:
102111

103112
Generating Cell via Command
104113
===========================
105114

106-
.. versionadded:: 4.3.0
107-
108115
You can also create a controlled cell via a built in command from the CLI. The command is ``php spark make:cell``. It takes one argument, the name of the cell to create. The name should be in PascalCase, and the class will be created in the ``app/Cells`` directory. The view file will also be created in the ``app/Cells`` directory.
109116

110117
::
111118

112-
> php spark make:cell AlertMessage
119+
> php spark make:cell AlertMessageCell
113120

114121
Using a Different View
115122
======================
@@ -122,7 +129,7 @@ You can specify a custom view name by setting the ``view`` property in the class
122129

123130
use CodeIgniter\View\Cells\Cell;
124131

125-
class AlertMessage extends Cell
132+
class AlertMessageCell extends Cell
126133
{
127134
public $type;
128135
public $message;
@@ -140,7 +147,7 @@ If you need more control over the rendering of the HTML, you can implement a ``r
140147

141148
use CodeIgniter\View\Cells\Cell;
142149

143-
class AlertMessage extends Cell
150+
class AlertMessageCell extends Cell
144151
{
145152
public $type;
146153
public $message;
@@ -161,7 +168,7 @@ If you need to perform additional logic for one or more properties you can use c
161168

162169
use CodeIgniter\View\Cells\Cell;
163170

164-
class AlertMessage extends Cell
171+
class AlertMessageCell extends Cell
165172
{
166173
protected $type;
167174
protected $message;
@@ -188,7 +195,7 @@ Sometimes you need to perform additional logic for the view, but you don't want
188195

189196
use CodeIgniter\View\Cells\Cell;
190197

191-
class RecentPosts extends Cell
198+
class RecentPostsCell extends Cell
192199
{
193200
protected $posts;
194201

@@ -198,11 +205,11 @@ Sometimes you need to perform additional logic for the view, but you don't want
198205
}
199206
}
200207

201-
// app/Cells/recent_posts.php
208+
// app/Cells/recent_posts_cell.php
202209
<ul>
203210
<?php foreach ($posts as $post): ?>
204211
<li><?= $this->linkPost($post) ?></li>
205-
<?php endforeach; ?>
212+
<?php endforeach ?>
206213
</ul>
207214

208215
Performing Setup Logic
@@ -216,7 +223,7 @@ If you need to perform additional logic before the view is rendered, you can imp
216223

217224
use CodeIgniter\View\Cells\Cell;
218225

219-
class RecentPosts extends Cell
226+
class RecentPostsCell extends Cell
220227
{
221228
protected $posts;
222229

@@ -229,12 +236,12 @@ If you need to perform additional logic before the view is rendered, you can imp
229236
You can pass additional parameters to the ``mount()`` method by passing them as an array to the ``view_cell()`` helper function. Any of the parameters sent that match a parameter name of the ``mount`` method will be passed in.
230237
::
231238

232-
// app/Cells/RecentPosts.php
239+
// app/Cells/RecentPostsCell.php
233240
namespace App\Cells;
234241

235242
use CodeIgniter\View\Cells\Cell;
236243

237-
class RecentPosts extends Cell
244+
class RecentPostsCell extends Cell
238245
{
239246
protected $posts;
240247

@@ -249,7 +256,7 @@ You can pass additional parameters to the ``mount()`` method by passing them as
249256
}
250257

251258
// Called in main View:
252-
<?= view_cell('RecentPosts::show', ['categoryId' => 5]); ?>
259+
<?= view_cell('RecentPostsCell', ['categoryId' => 5]) ?>
253260

254261
************
255262
Cell Caching
@@ -260,10 +267,10 @@ third parameter. This will use the currently configured cache engine.
260267
::
261268

262269
// Cache the view for 5 minutes
263-
<?= view_cell('\App\Libraries\Blog::recentPosts', 'limit=5', 300) ?>
270+
<?= view_cell('App\Cells\Blog::recentPosts', 'limit=5', 300) ?>
264271

265272
You can provide a custom name to use instead of the auto-generated one if you like, by passing the new name
266273
as the fourth parameter::
267274

268275
// Cache the view for 5 minutes
269-
<?= view_cell('\App\Libraries\Blog::recentPosts', 'limit=5', 300, 'newcacheid') ?>
276+
<?= view_cell('App\Cells\Blog::recentPosts', 'limit=5', 300, 'newcacheid') ?>

0 commit comments

Comments
 (0)