Welcome Guest, Not a member yet? Register   Sign In
Using view_cell - I am not clearly getting how to do it
#1

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;

use 
CodeIgniter\Model;

class 
BlockModel extends Model
{
    protected $table 'blocks';
    protected $primaryKey 'id';
    protected $allowedFields = ['bloc_name''title_en''body_en''title_nl''body_nl'];

    public function getBlock($bloc_name false)
    {
        if ($bloc_name === false)
        {
            return $this->findAll();
        }

        return $this->asArray()
            ->where(['bloc_name' => $bloc_name])
            ->first();
    }


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

namespace App\Cells;

use 
CodeIgniter\View\Cells\Cell;

class 
BlockCell
{

    public function getBlock($bloc_name)
    {

        return $this->asArray()
            ->where(['bloc_name' => $bloc_name])
            ->first();
    }



I would really appreciate some help to get this working.
Reply
#2

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:
// ...
private $blogModel;

// ...
$this->blogModel model('BlogModel'); 
Reply
#3

So do you mean like this?
PHP Code:
<?php

namespace App\Cells;
use 
CodeIgniter\View\Cells\Cell;

class 
BlockCell
{

    private $blockModel;

    public function getBlock($bloc_name)
    {
        $this->blockModel model('BlockModel');
        
        
return $this->blockModel->asArray()
            ->where(['bloc_name' => $bloc_name])
            ->first();
    }



Then I run into
PHP Code:
CodeIgniter\View\Cell::renderSimpleClass(): Return value must be of type string, array returned  
Reply
#4

Because ViewCell must return a part of a view, that is a string (HTML) data .
Reply
#5

Cell should return part of the htmln. Execute foreach() and return the string.


PHP Code:
$blockHtml '';
foreach(
$rows as $block) {
    $blockHtml .= "<p>".$block['text']."</p>"// model array key
}

return 
$blockHtml
Simple CI 4 project for beginners codeigniter-expenses
Reply
#6

Thank you for the help, but I am not getting how I should organize my code and I am super confused with the examples  Huh

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>
<
div>$block['content']</div
 

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
Reply
#7

(This post was last modified: 08-31-2023, 03:31 AM by ozornick.)

All right.

PHP Code:
<?php

namespace App\Cells;

class 
BlockCell
{
    public function getBlock($bloc_name)
    {
        $blockFromModel = $this->asArray()
             ->where(['bloc_name' => $bloc_name])
             ->first();

         return view('Block/block', ['block' => $blockFromModel]);
    }
}

// file app/Views/Block/block.php with HTML
<h2><?= $block['title'?></h2>
<div><?= $block['content'?></div> 
Simple CI 4 project for beginners codeigniter-expenses
Reply
#8

Thanks a lot @ozornick for understanding my limitations  Tongue I feel I am almost there (I had a few errors still and corrected them like this:
PHP Code:
// BlockCell.php
class BlockCell
{

    private $blockModel;

    public function getBlock($bloc_name)
    {
        $this->blockModel model('BlockModel');

        $blockFromModel $this->blockModel->asArray()
            ->where(['bloc_name' => $bloc_name])
            ->first();
      
        
return view('Cells/block', ['block' => $blockFromModel]);
    }  


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.  Huh

PHP Code:
<div>From Views/Cells/block.php
    
<h2><?= $block['title_en']; ?></h2>

</div> 

Am I still doing things wrong?
Reply
#9

See output in Cell var_dump($blockFromModel);
Simple CI 4 project for beginners codeigniter-expenses
Reply
#10

(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 Heart Heart Heart
Is there a way I can "send you a coffee" somehow? I would not have made it without your help.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB