Skip to content

Commit 99ae8bd

Browse files
committed
docs: extract source files and fix some bugs
1 parent da06727 commit 99ae8bd

23 files changed

Lines changed: 230 additions & 188 deletions

File tree

user_guide_src/source/outgoing/view_cells.rst

Lines changed: 33 additions & 188 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@ Calling a View Cell
1818

1919
No matter which type of View Cell you are using, you can call it from any view by using the ``view_cell()`` helper function.
2020

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.
22-
::
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-
<?= view_cell('App\Cells\MyClass::myMethod', ['param1' => 'value1', 'param2' => 'value2']) ?>
23+
.. literalinclude:: view_cells/001.php
2524

2625
The Cell method must return a string, which will be inserted into the view where the ``view_cell()`` function was called.
2726

@@ -30,57 +29,35 @@ Namespace Omission
3029

3130
.. versionadded:: 4.3.0
3231

33-
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.
34-
::
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:
3533

36-
<?= view_cell('MyClass::myMethod', ['param1' => 'value1', 'param2' => 'value2']) ?>
34+
.. literalinclude:: view_cells/002.php
3735

3836
Passing Parameters as Key/Value String
3937
======================================
4038

4139
You can also pass the parameters along as a key/value string:
42-
::
4340

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

4643
************
4744
Simple Cells
4845
************
4946

5047
Simple Cells are classes that return a string from the chosen method. An example of a simple Alert Message cell might look like this:
51-
::
52-
53-
namespace App\Cells;
5448

55-
class AlertMessage
56-
{
57-
public function show(array $params): string
58-
{
59-
return "<div class="alert alert-{$params['type']}">{$params['message']}</div>";
60-
}
61-
}
49+
.. literalinclude:: view_cells/004.php
6250

6351
You would call it from within a view like:
64-
::
6552

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

6855
Additionally, you can use parameter names that match the parameter variables in the method for better readability.
69-
When you use it this way, all of the parameters must always be specified in the view cell call::
70-
71-
// In a View.
72-
<?= 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:
7357

74-
// In a Cell.
75-
public function recentPosts(string $category, int $limit)
76-
{
77-
$posts = $this->blogModel->where('category', $category)
78-
->orderBy('published_on', 'desc')
79-
->limit($limit)
80-
->get();
58+
.. literalinclude:: view_cells/006.php
8159

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

8562
.. _controlled-cells:
8663

@@ -103,26 +80,12 @@ Creating a Controlled Cell
10380
==========================
10481

10582
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:
106-
::
107-
108-
// app/Cells/AlertMessageCell.php
109-
namespace App\Cells;
110-
111-
use CodeIgniter\View\Cells\Cell;
11283

113-
class AlertMessageCell extends Cell
114-
{
115-
public $type;
116-
public $message;
117-
}
84+
.. literalinclude:: view_cells/008.php
11885

119-
// app/Cells/alert_message.php
120-
<div class="alert alert-<?= esc($type, 'attr') ?>">
121-
<?= esc($message) ?>
122-
</div>
86+
.. literalinclude:: view_cells/009.php
12387

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

12790
.. _generating-cell-via-command:
12891

@@ -138,181 +101,63 @@ You can also create a controlled cell via a built in command from the CLI. The c
138101
Using a Different View
139102
======================
140103

141-
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.
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:
142105

143-
::
144-
145-
namespace App\Cells;
146-
147-
use CodeIgniter\View\Cells\Cell;
148-
149-
class AlertMessageCell extends Cell
150-
{
151-
public $type;
152-
public $message;
153-
154-
protected $view = 'my/custom/view';
155-
}
106+
.. literalinclude:: view_cells/011.php
156107

157108
Customize the Rendering
158109
=======================
159110

160-
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.
161-
::
162-
163-
namespace App\Cells;
164-
165-
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:
166112

167-
class AlertMessageCell extends Cell
168-
{
169-
public $type;
170-
public $message;
171-
172-
public function render(): string
173-
{
174-
return $this->view('my/custom/view', ['extra' => 'data']);
175-
}
176-
}
113+
.. literalinclude:: view_cells/012.php
177114

178115
Computed Properties
179116
===================
180117

181-
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``.
182-
::
183-
184-
// In a View. Initialize the protected properties.
185-
view_cell('AlertMessageCell', ['type' => 'note', 'message' => 'test']);
186-
187-
// app/Cells/AlertMessageCell.php
188-
namespace App\Cells;
189-
190-
use CodeIgniter\View\Cells\Cell;
191-
192-
class AlertMessageCell extends Cell
193-
{
194-
protected $type;
195-
protected $message;
196-
private $computed;
197-
198-
public function mount()
199-
{
200-
$this->computed = sprintf('%s - %s', $this->type, $this->message);
201-
}
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``:
202119

203-
public function getComputedProperty(): string
204-
{
205-
return $this->computed;
206-
}
120+
.. literalinclude:: view_cells/013.php
207121

208-
public function getTypeProperty(): string
209-
{
210-
return $this->type;
211-
}
122+
.. literalinclude:: view_cells/014.php
212123

213-
public function getMessageProperty(): string
214-
{
215-
return $this->message;
216-
}
217-
}
218-
219-
// app/Cells/alert_message.php
220-
<div>
221-
<p>type - <?= esc($type) ?></p>
222-
<p>message - <?= esc($message) ?></p>
223-
<p>computed: <?= esc($computed) ?></p>
224-
</div>
124+
.. literalinclude:: view_cells/015.php
225125

226126
.. important:: You can't set properties that are declared as private during cell
227127
initialization.
228128

229129
Presentation Methods
230130
====================
231131

232-
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.
233-
::
234-
235-
// app/Cells/RecentPostsCell.php
236-
namespace App\Cells;
237-
238-
use CodeIgniter\View\Cells\Cell;
239-
240-
class RecentPostsCell extends Cell
241-
{
242-
protected $posts;
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:
243133

244-
public function linkPost($post)
245-
{
246-
return anchor('posts/' . $post->id, $post->title);
247-
}
248-
}
134+
.. literalinclude:: view_cells/016.php
249135

250-
// app/Cells/recent_posts.php
251-
<ul>
252-
<?php foreach ($posts as $post): ?>
253-
<li><?= $this->linkPost($post) ?></li>
254-
<?php endforeach ?>
255-
</ul>
136+
.. literalinclude:: view_cells/017.php
256137

257138
Performing Setup Logic
258139
======================
259140

260-
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.
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:
261142

262-
::
263-
264-
namespace App\Cells;
265-
266-
use CodeIgniter\View\Cells\Cell;
267-
268-
class RecentPostsCell extends Cell
269-
{
270-
protected $posts;
271-
272-
public function mount()
273-
{
274-
$this->posts = model('PostModel')->getRecent();
275-
}
276-
}
143+
.. literalinclude:: view_cells/018.php
277144

278-
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.
279-
::
280-
281-
// app/Cells/RecentPostsCell.php
282-
namespace App\Cells;
283-
284-
use CodeIgniter\View\Cells\Cell;
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:
285146

286-
class RecentPostsCell extends Cell
287-
{
288-
protected $posts;
147+
.. literalinclude:: view_cells/019.php
289148

290-
public function mount(?int $categoryId)
291-
{
292-
$this->posts = model('PostModel')
293-
->when($categoryId, function ($query, $category) {
294-
return $query->where('category_id', $categoryId);
295-
})
296-
->getRecent();
297-
}
298-
}
299-
300-
// Called in main View:
301-
<?= view_cell('RecentPostsCell', ['categoryId' => 5]) ?>
149+
.. literalinclude:: view_cells/020.php
302150

303151
************
304152
Cell Caching
305153
************
306154

307155
You can cache the results of the view cell call by passing the number of seconds to cache the data for as the
308-
third parameter. This will use the currently configured cache engine.
309-
::
156+
third parameter. This will use the currently configured cache engine:
310157

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

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

317-
// Cache the view for 5 minutes
318-
<?= 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)