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
@@ -16,61 +16,48 @@ CodeIgniter supports two types of View Cells: simple and controlled. Simple View
16
16
Calling a View Cell
17
17
*******************
18
18
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.
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:
23
22
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.
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:
28
33
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
+
======================================
30
38
31
39
You can also pass the parameters along as a key/value string:
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:
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.
123
96
124
97
::
125
98
@@ -128,181 +101,63 @@ You can also create a controlled cell via a built in command from the CLI. The c
128
101
Using a Different View
129
102
======================
130
103
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:
136
105
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
146
107
147
108
Customize the Rendering
148
109
=======================
149
110
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:
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.
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``:
.. important:: You can't set properties that are declared as private during cell
217
127
initialization.
218
128
219
129
Presentation Methods
220
130
====================
221
131
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:
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:
257
142
258
-
class RecentPostsCell extends Cell
259
-
{
260
-
protected $posts;
143
+
.. literalinclude:: view_cells/018.php
261
144
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:
267
146
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
270
148
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) {
0 commit comments