Welcome Guest, Not a member yet? Register   Sign In
[answered] How to render cell passing variables to cell view?
#1

(This post was last modified: 04-19-2023, 11:13 AM by Corsari.)

Hello
I have created this cell view in app/Cells

PHP Code:
        <div class="card shadow">
            <div class="card-body text-start">
                <h5 class="card-title"><?= $cardTitle?></h5>
                <p class="card-text"><i class="bi <?= $cardIcon?> mr-3"></i><?= $counter?></p>
                <a href="#" class="btn btn-primary"><?= $btnText?></a>
            </div>
        </div> 


and the respective controller in app/Cells

PHP Code:
<?php

namespace App\Cells;

use 
CodeIgniter\View\Cells\Cell;

class 
SummaryCard1Btn extends Cell
{




I'm reading the guide but I cannot find a suitable manner to pass those variables when it is time to view/render the cell
I'd like to use something like

PHP Code:
<?= view_cell('SummaryCard1Btn::render', (passing here those 4 variables); ?>


Can you kindly help with this?
Thank you
Reply
#2

(This post was last modified: 04-19-2023, 11:18 AM by Corsari.)

Well, I ended up self answering my question

The guide is a bit messy in this chapter https://codeigniter4.github.io/CodeIgnit...cells.html,
so for the sake of giving an hint to those that could sink (as I did) in that dis-order :-) here you an ordered example of
How To Render a Cell View Passing It Variables



for the Codeigniter's Guide Publisher

I had to concentrate so much on the various parts of the guide and what I ended up to do is not written (if am I not wrong) in the guide

- here https://codeigniter4.github.io/CodeIgnit...mple-cells it shown this code

PHP Code:
<?= view_cell('AlertMessage::show', ['type' => 'success''message' => 'The user has been updated.']); ?>


- but then the show() method is used only in the simple cells where html strings are supposed to be returned and not a view file to be rendered (the topic of this post)



- then here https://codeigniter4.github.io/CodeIgnit...olled-cell is used again the above alert message example, but this time without an hint on how to call the cell' view with the data passed to that view





So, here it is an ordered example for the question expressed in this topic

How to render a cell view passing variables to that cell view



just to recall the alert message example of the guide, create the cell and the cell' view

(note: with CodeIgniter 4.3+ this can be done altogether using a spark command in the terminal to be run in the root of the codeigniter app

Code:
#php spark make:cell MakeDivAlert
)


so, either with spark or manually the following two files must be created

/app/Cells/MakeDivAlert.php

/app/Cells/make_div_alert.php (if you used the spark command, the view file must be renamed because of a bug, remove the string "_cell" from the filename)


with the following content


MakeDivAlert.php

PHP Code:
<?php

namespace App\Cells;

use 
CodeIgniter\View\Cells\Cell;

class 
MakeDivAlert extends Cell
{
    public $type;
    public $message;



make_div_alert.php

PHP Code:
<div>
    <?= $message?> - <?= $type?>
</div> 


then in any view of your layout

PHP Code:
<?= view_cell('MakeDivAlert', ['type' => 'success''message' => 'The user has been updated.']); ?>


NOTE: since MakeDivAlert has no methods in it, the first parameter in the view_cell() is just 'MakeDivAlert', without any call to any method
.
.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB