Skip to content

Commit 245b36e

Browse files
authored
Merge pull request #7662 from kenjis/fix-docs-view-cells
docs: extract source code to files in ViewCells
2 parents 6425b1d + 1a54772 commit 245b36e

23 files changed

Lines changed: 242 additions & 190 deletions

File tree

user_guide_src/source/outgoing/view_cells.rst

Lines changed: 45 additions & 190 deletions
Original file line numberDiff line numberDiff line change
@@ -16,61 +16,48 @@ CodeIgniter supports two types of View Cells: simple and controlled. Simple View
1616
Calling a View Cell
1717
*******************
1818

19-
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.
20-
::
19+
No matter which type of View Cell you are using, you can call it from any view by using the ``view_cell()`` helper function.
2120

22-
<?= view_cell('App\Cells\MyClass::myMethod', ['param1' => 'value1', 'param2' => 'value2']) ?>
21+
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:
2322

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.
25-
::
23+
.. literalinclude:: view_cells/001.php
24+
25+
The Cell method must return a string, which will be inserted into the view where the ``view_cell()`` function was called.
26+
27+
Namespace Omission
28+
==================
29+
30+
.. versionadded:: 4.3.0
2631

27-
<?= view_cell('MyClass::myMethod', ['param1' => 'value1', 'param2' => 'value2']) ?>
32+
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:
2833

29-
.. note:: Namespace omission is available since v4.3.0 and later.
34+
.. literalinclude:: view_cells/002.php
35+
36+
Passing Parameters as Key/Value String
37+
======================================
3038

3139
You can also pass the parameters along as a key/value string:
32-
::
3340

34-
<?= view_cell('MyClass::myMethod', 'param1=value1, param2=value2') ?>
41+
.. literalinclude:: view_cells/003.php
3542

3643
************
3744
Simple Cells
3845
************
3946

4047
Simple Cells are classes that return a string from the chosen method. An example of a simple Alert Message cell might look like this:
41-
::
4248

43-
namespace App\Cells;
44-
45-
class AlertMessage
46-
{
47-
public function show(array $params): string
48-
{
49-
return "<div class="alert alert-{$params['type']}">{$params['message']}</div>";
50-
}
51-
}
49+
.. literalinclude:: view_cells/004.php
5250

5351
You would call it from within a view like:
54-
::
5552

56-
<?= view_cell('AlertMessage::show', ['type' => 'success', 'message' => 'The user has been updated.']) ?>
53+
.. literalinclude:: view_cells/005.php
5754

5855
Additionally, you can use parameter names that match the parameter variables in the method for better readability.
59-
When you use it this way, all of the parameters must always be specified in the view cell call::
60-
61-
// In a View.
62-
<?= view_cell('Blog::recentPosts', 'category=codeigniter, limit=5') ?>
56+
When you use it this way, all of the parameters must always be specified in the view cell call:
6357

64-
// In a Cell.
65-
public function recentPosts(string $category, int $limit)
66-
{
67-
$posts = $this->blogModel->where('category', $category)
68-
->orderBy('published_on', 'desc')
69-
->limit($limit)
70-
->get();
58+
.. literalinclude:: view_cells/006.php
7159

72-
return view('recentPosts', ['posts' => $posts]);
73-
}
60+
.. literalinclude:: view_cells/007.php
7461

7562
.. _controlled-cells:
7663

@@ -93,33 +80,19 @@ Creating a Controlled Cell
9380
==========================
9481

9582
At the most basic level, all you need to implement within the class are public properties. These properties will be made available to the view file automatically. Implementing the AlertMessage from above as a Controlled Cell would look like this:
96-
::
97-
98-
// app/Cells/AlertMessageCell.php
99-
namespace App\Cells;
100-
101-
use CodeIgniter\View\Cells\Cell;
10283

103-
class AlertMessageCell extends Cell
104-
{
105-
public $type;
106-
public $message;
107-
}
84+
.. literalinclude:: view_cells/008.php
10885

109-
// app/Cells/alert_message.php
110-
<div class="alert alert-<?= esc($type, 'attr') ?>">
111-
<?= esc($message) ?>
112-
</div>
86+
.. literalinclude:: view_cells/009.php
11387

114-
// Called in main View:
115-
<?= view_cell('AlertMessageCell', 'type=warning, message=Failed.') ?>
88+
.. literalinclude:: view_cells/010.php
11689

11790
.. _generating-cell-via-command:
11891

11992
Generating Cell via Command
12093
===========================
12194

122-
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.
95+
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.
12396

12497
::
12598

@@ -128,181 +101,63 @@ You can also create a controlled cell via a built in command from the CLI. The c
128101
Using a Different View
129102
======================
130103

131-
You can specify a custom view name by setting the ``view`` property in the class. The view will be located like any view would be normally.
132-
133-
::
134-
135-
namespace App\Cells;
104+
You can specify a custom view name by setting the ``view`` property in the class. The view will be located like any view would be normally:
136105

137-
use CodeIgniter\View\Cells\Cell;
138-
139-
class AlertMessageCell extends Cell
140-
{
141-
public $type;
142-
public $message;
143-
144-
protected $view = 'my/custom/view';
145-
}
106+
.. literalinclude:: view_cells/011.php
146107

147108
Customize the Rendering
148109
=======================
149110

150-
If you need more control over the rendering of the HTML, you can implement a ``render()`` method. This method allows you to perform additional logic and pass extra data the view, if needed. The ``render()`` method must return a string. To take advantage of the full features of controlled Cells, you should use ``$this->view()`` instead of the normal ``view()`` helper function.
151-
::
152-
153-
namespace App\Cells;
154-
155-
use CodeIgniter\View\Cells\Cell;
111+
If you need more control over the rendering of the HTML, you can implement a ``render()`` method. This method allows you to perform additional logic and pass extra data the view, if needed. The ``render()`` method must return a string. To take advantage of the full features of controlled Cells, you should use ``$this->view()`` instead of the normal ``view()`` helper function:
156112

157-
class AlertMessageCell extends Cell
158-
{
159-
public $type;
160-
public $message;
161-
162-
public function render(): string
163-
{
164-
return $this->view('my/custom/view', ['extra' => 'data']);
165-
}
166-
}
113+
.. literalinclude:: view_cells/012.php
167114

168115
Computed Properties
169116
===================
170117

171-
If you need to perform additional logic for one or more properties you can use computed properties. These require setting the property to either ``protected`` or ``private`` and implementing a public method whose name consists of the property name surrounded by ``get`` and ``Property``.
172-
::
173-
174-
// In a View. Initialize the protected properties.
175-
view_cell('AlertMessageCell', ['type' => 'note', 'message' => 'test']);
176-
177-
// app/Cells/AlertMessageCell.php
178-
namespace App\Cells;
179-
180-
use CodeIgniter\View\Cells\Cell;
181-
182-
class AlertMessageCell extends Cell
183-
{
184-
protected $type;
185-
protected $message;
186-
private $computed;
118+
If you need to perform additional logic for one or more properties you can use computed properties. These require setting the property to either ``protected`` or ``private`` and implementing a public method whose name consists of the property name surrounded by ``get`` and ``Property``:
187119

188-
public function mount()
189-
{
190-
$this->computed = sprintf('%s - %s', $this->type, $this->message);
191-
}
120+
.. literalinclude:: view_cells/013.php
192121

193-
public function getComputedProperty(): string
194-
{
195-
return $this->computed;
196-
}
122+
.. literalinclude:: view_cells/014.php
197123

198-
public function getTypeProperty(): string
199-
{
200-
return $this->type;
201-
}
202-
203-
public function getMessageProperty(): string
204-
{
205-
return $this->message;
206-
}
207-
}
208-
209-
// app/Cells/alert_message.php
210-
<div>
211-
<p>type - <?= esc($type) ?></p>
212-
<p>message - <?= esc($message) ?></p>
213-
<p>computed: <?= esc($computed) ?></p>
214-
</div>
124+
.. literalinclude:: view_cells/015.php
215125

216126
.. important:: You can't set properties that are declared as private during cell
217127
initialization.
218128

219129
Presentation Methods
220130
====================
221131

222-
Sometimes you need to perform additional logic for the view, but you don't want to pass it as a parameter. You can implement a method that will be called from within the cell's view itself. This can help the readability of your views.
223-
::
224-
225-
// app/Cells/RecentPostsCell.php
226-
namespace App\Cells;
227-
228-
use CodeIgniter\View\Cells\Cell;
132+
Sometimes you need to perform additional logic for the view, but you don't want to pass it as a parameter. You can implement a method that will be called from within the cell's view itself. This can help the readability of your views:
229133

230-
class RecentPostsCell extends Cell
231-
{
232-
protected $posts;
134+
.. literalinclude:: view_cells/016.php
233135

234-
public function linkPost($post)
235-
{
236-
return anchor('posts/' . $post->id, $post->title);
237-
}
238-
}
239-
240-
// app/Cells/recent_posts.php
241-
<ul>
242-
<?php foreach ($posts as $post): ?>
243-
<li><?= $this->linkPost($post) ?></li>
244-
<?php endforeach ?>
245-
</ul>
136+
.. literalinclude:: view_cells/017.php
246137

247138
Performing Setup Logic
248139
======================
249140

250-
If you need to perform additional logic before the view is rendered, you can implement a ``mount()`` method. This method will be called just after the class is instantiated, and can be used to set additional properties or perform other logic.
251-
252-
::
253-
254-
namespace App\Cells;
255-
256-
use CodeIgniter\View\Cells\Cell;
141+
If you need to perform additional logic before the view is rendered, you can implement a ``mount()`` method. This method will be called just after the class is instantiated, and can be used to set additional properties or perform other logic:
257142

258-
class RecentPostsCell extends Cell
259-
{
260-
protected $posts;
143+
.. literalinclude:: view_cells/018.php
261144

262-
public function mount()
263-
{
264-
$this->posts = model('PostModel')->getRecent();
265-
}
266-
}
145+
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:
267146

268-
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.
269-
::
147+
.. literalinclude:: view_cells/019.php
270148

271-
// app/Cells/RecentPostsCell.php
272-
namespace App\Cells;
273-
274-
use CodeIgniter\View\Cells\Cell;
275-
276-
class RecentPostsCell extends Cell
277-
{
278-
protected $posts;
279-
280-
public function mount(?int $categoryId)
281-
{
282-
$this->posts = model('PostModel')
283-
->when($categoryId, function ($query, $category) {
284-
return $query->where('category_id', $categoryId);
285-
})
286-
->getRecent();
287-
}
288-
}
289-
290-
// Called in main View:
291-
<?= view_cell('RecentPostsCell', ['categoryId' => 5]) ?>
149+
.. literalinclude:: view_cells/020.php
292150

293151
************
294152
Cell Caching
295153
************
296154

297155
You can cache the results of the view cell call by passing the number of seconds to cache the data for as the
298-
third parameter. This will use the currently configured cache engine.
299-
::
156+
third parameter. This will use the currently configured cache engine:
300157

301-
// Cache the view for 5 minutes
302-
<?= view_cell('App\Cells\Blog::recentPosts', 'limit=5', 300) ?>
158+
.. literalinclude:: view_cells/021.php
303159

304160
You can provide a custom name to use instead of the auto-generated one if you like, by passing the new name
305-
as the fourth parameter::
161+
as the fourth parameter:
306162

307-
// Cache the view for 5 minutes
308-
<?= view_cell('App\Cells\Blog::recentPosts', 'limit=5', 300, 'newcacheid') ?>
163+
.. literalinclude:: view_cells/022.php
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// In a View.
2+
<?= view_cell('App\Cells\MyClass::myMethod', ['param1' => 'value1', 'param2' => 'value2']) ?>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// In a View.
2+
<?= view_cell('MyClass::myMethod', ['param1' => 'value1', 'param2' => 'value2']) ?>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// In a View.
2+
<?= view_cell('MyClass::myMethod', 'param1=value1, param2=value2') ?>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace App\Cells;
4+
5+
class AlertMessage
6+
{
7+
public function show(array $params): string
8+
{
9+
return "<div class=\"alert alert-{$params['type']}\">{$params['message']}</div>";
10+
}
11+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// In a View.
2+
<?= view_cell('AlertMessage::show', ['type' => 'success', 'message' => 'The user has been updated.']) ?>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// In a View.
2+
<?= view_cell('Blog::recentPosts', 'category=codeigniter, limit=5') ?>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
// In a Cell.
4+
5+
namespace App\Cells;
6+
7+
class Blog
8+
{
9+
// ...
10+
11+
public function recentPosts(string $category, int $limit): string
12+
{
13+
$posts = $this->blogModel->where('category', $category)
14+
->orderBy('published_on', 'desc')
15+
->limit($limit)
16+
->get();
17+
18+
return view('recentPosts', ['posts' => $posts]);
19+
}
20+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
// app/Cells/AlertMessageCell.php
4+
5+
namespace App\Cells;
6+
7+
use CodeIgniter\View\Cells\Cell;
8+
9+
class AlertMessageCell extends Cell
10+
{
11+
public $type;
12+
public $message;
13+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// app/Cells/alert_message.php
2+
<div class="alert alert-<?= esc($type, 'attr') ?>">
3+
<?= esc($message) ?>
4+
</div>

0 commit comments

Comments
 (0)