Skip to content

Commit fe59a2f

Browse files
authored
Merge pull request #7460 from kenjis/docs-Computed-Properties
docs: improve View Cells Computed Properties sample
2 parents 90762f3 + 3f243ee commit fe59a2f

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

user_guide_src/source/outgoing/view_cells.rst

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ Computed Properties
164164
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``.
165165
::
166166

167+
// In a View. Initialize the protected properties.
168+
view_cell('AlertMessageCell', ['type' => 'note', 'message' => 'test']);
169+
170+
// app/Cells/AlertMessageCell.php
167171
namespace App\Cells;
168172

169173
use CodeIgniter\View\Cells\Cell;
@@ -172,6 +176,17 @@ If you need to perform additional logic for one or more properties you can use c
172176
{
173177
protected $type;
174178
protected $message;
179+
private $computed;
180+
181+
public function mount()
182+
{
183+
$this->computed = sprintf('%s - %s', $this->type, $this->message);
184+
}
185+
186+
public function getComputedProperty(): string
187+
{
188+
return $this->computed;
189+
}
175190

176191
public function getTypeProperty(): string
177192
{
@@ -184,6 +199,16 @@ If you need to perform additional logic for one or more properties you can use c
184199
}
185200
}
186201

202+
// app/Cells/alert_message_cell.php
203+
<div>
204+
<p>type - <?= esc($type) ?></p>
205+
<p>message - <?= esc($message) ?></p>
206+
<p>computed: <?= esc($computed) ?></p>
207+
</div>
208+
209+
.. important:: You can't set properties that are declared as private during cell
210+
initialization.
211+
187212
Presentation Methods
188213
====================
189214

0 commit comments

Comments
 (0)