Using view_cell - I am not clearly getting how to do it |
Hi everyone,
I have been reading the documentation on view_cells and following some tutorials, but I am getting confused as to what to put where and was hoping to get some clarity asking here. I do have some blocs of content that are re-used on multiple pages so I want to use a view_cell to be able to call their content in the right locale where i need them. I had started to create a model BlockModel.php like this: PHP Code: <?php namespace App\Models; I used the php spark make:cell Block command that created 2 files inside app/Cells BlockCell.php and block.php. I understand that block.php is where I should put my html code for rendering it on the view, but I am confused as to how to handle things inside BlockCell.php. I was aiming to be able to call a block from inside a view like this: PHP Code: <?= view_cell("\App\Cells\BlockCell::getBlock", ['bloc_name'=>'contact']); ?> I thought I had to move the function getBock inside BlockCell.php but then I get: PHP Code: Call to undefined method App\Cells\BlockCell::asArray() I have tried a lot of things, getting more and more confused. Here's my attempt of BlockCell.php file so far PHP Code: <?php I would really appreciate some help to get this working.
The BlockCell does not have the asArray() method.
So "Call to undefined method App\Cells\BlockCell::asArray()". It seems you are confusing the Cell class with the Model class. If you want to get the result of the Model, you need to call the Model's method in the Cell class. See the sample code: https://codeigniter4.github.io/CodeIgnit...mple-cells The $this->blogModel is used in that code. Of course you need to assign the model instance by yourself like: PHP Code: // ...
So do you mean like this?
PHP Code: <?php Then I run into PHP Code: CodeIgniter\View\Cell::renderSimpleClass(): Return value must be of type string, array returned
Because ViewCell must return a part of a view, that is a string (HTML) data .
Cell should return part of the htmln. Execute foreach() and return the string.
PHP Code: $blockHtml = '';
Simple CI 4 project for beginners codeigniter-expenses
Thank you for the help, but I am not getting how I should organize my code and I am super confused with the examples
From what you are explaining, should I keep the getBlock($bloc_name) function inside my BlockModel? From that function I get an array with the values I need to display. I have just one record coming from the database, so the html inside the file responsible for rendering the html would be something like: block.php PHP Code: <h2>$block['title']</h2> But what goes then inside the BlockCell.php then? I thought I should there make the call for the record I need, and call for the block.php to render the html. How are you supposed to do this? I am sorry if that looks obvious to you and clearly explained on the documentation... I have been trying to understand it for days and I am not getting there. Thanks again for your help
All right.
PHP Code: <?php
Simple CI 4 project for beginners codeigniter-expenses
Thanks a lot @ozornick for understanding my limitations I feel I am almost there (I had a few errors still and corrected them like this:
PHP Code: // BlockCell.php The block.php file is loaded correctly, and I can see in the debug bar that the $block variable is populated as I need it, But I still get an ErrorException undefined array key "title_en" when I try to echo $block['title_en'] (while in the debug var, it is there. PHP Code: <div>From Views/Cells/block.php Am I still doing things wrong?
See output in Cell var_dump($blockFromModel);
Simple CI 4 project for beginners codeigniter-expenses
(08-31-2023, 05:52 AM)ozornick Wrote: See output in Cell var_dump($blockFromModel);Oh wow the dump made it clear to me that I had a typo on a field name and I could not see it looking at the debug var (it was there but my brain was correcting it). It works fine. Thank you so much @ozornick for your help and your patience with my limited skills Is there a way I can "send you a coffee" somehow? I would not have made it without your help. |
Welcome Guest, Not a member yet? Register Sign In |