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.