You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Cell method must return a string, which will be inserted into the view where the ``view_cell()`` function was called.
27
26
@@ -30,57 +29,35 @@ Namespace Omission
30
29
31
30
.. versionadded:: 4.3.0
32
31
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:
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:
@@ -138,181 +101,63 @@ You can also create a controlled cell via a built in command from the CLI. The c
138
101
Using a Different View
139
102
======================
140
103
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:
142
105
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
156
107
157
108
Customize the Rendering
158
109
=======================
159
110
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:
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.
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``:
202
119
203
-
public function getComputedProperty(): string
204
-
{
205
-
return $this->computed;
206
-
}
120
+
.. literalinclude:: view_cells/013.php
207
121
208
-
public function getTypeProperty(): string
209
-
{
210
-
return $this->type;
211
-
}
122
+
.. literalinclude:: view_cells/014.php
212
123
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
225
125
226
126
.. important:: You can't set properties that are declared as private during cell
227
127
initialization.
228
128
229
129
Presentation Methods
230
130
====================
231
131
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:
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:
261
142
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
277
144
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:
285
146
286
-
class RecentPostsCell extends Cell
287
-
{
288
-
protected $posts;
147
+
.. literalinclude:: view_cells/019.php
289
148
290
-
public function mount(?int $categoryId)
291
-
{
292
-
$this->posts = model('PostModel')
293
-
->when($categoryId, function ($query, $category) {
0 commit comments