Reputation: 453
How to/Can I use HTML Helper/initialize() in Cell CakePHP 3.0?
/src/View/Cell/NavCell.php
namespace App\View\Cell;
use Cake\View\Cell;
use Cake\ORM\TableRegistry;
use Cake\View\Helper\HtmlHelper;
class NavCell extends Cell
{
public function initialize() {
parent::initialize();
$this->loadHelper('Html');
}
public function display() {
}
}
/src/Template/Cell/Nav/display.ctp
<?php $this->Html->script('script', ['block' => 'scriptBottom']); ?>
Upvotes: 2
Views: 1183
Reputation: 60463
Cells do neither have an initialize()
, nor a loadHelper()
method, you can't just throw code together and hope that it works. Always have a look in Cookbook and the API docs first, if necessary check the source code.
Cells supply a $helpers
property where you can define the helpers to use/load.
API > \Cake\View\Cell::$helpers
class NavCell extends Cell
{
public $helpers = [
'Html'
];
// ...
}
However this shouldn't even be necessary in case you don't want to apply any configuration, as views do lazy load possible helpers when non existent properties are being accessed,
ie using $this->Html->script()
in a cell would work out of the box.
It should also be noted that, as mentioned (though anything but detailed) in the API docs, $helpers
is one of the properties that is being fed with the corresponding properties value of the class that makes use of \Cake\View\CellTrait
, ie the helpers config that is set for the view (wich in turn stems from the corresponding controller) where you are calling $this->cell()
is being copied to the cell.
However, cells are using separate view class instances, so the blocks that you define/access in your cell, do not affect the view that is used by other templates like for example layouts!
Upvotes: 3