DEV Community

loading...
Cover image for Create cell【CakePHP】

Create cell【CakePHP】

sachikokame profile image sachiko ・1 min read

Introduction

Please watch over gently🙇‍♀️
I will write about cakePHP "cell"!

How to use

  • Create 'class' and 'template'

[src/View/Cell/InboxCell.php]

<?php
namespace App\View\Cell;

use Cake\View\Cell;

class InboxCell extends Cell
{

    public function display()
    {
      $hello = "hello!!!(*´ω`*)";
      $this->set('sample', $hello);
    }

}

[templates/cell/Inbox/display.php]

<div>
    message✨ <?= $sample ?> 
</div>

use

On the "/templates/????/index.php"

The following description

<?=$this->cell('Inbox::display');?>

display

http://〇〇/????
スクリーンショット 2020-09-06 17.44.56

A little more

Show what you get in the model

Assumption: You have a created model.This time 'tags'

[src/View/Cell/InboxCell.php]

<?php
namespace App\View\Cell;

use Cake\View\Cell;

class InboxCell extends Cell
{

    public function display()
    {
      $this->loadModel('Tags');
      $tags = $this->Tags->find();
      $this->set('tags', $tags);
    }

}

[templates/cell/Inbox/display.php]

<table>
  <?php foreach ($tags as $tag): ?>
    <tr>
      <td><?= $tag->title ?></td>
    </tr>
  <?php endforeach; ?>
</table>

Where you want to use↓

<?=$this->cell('Inbox::display');?>

display

スクリーンショット 2020-09-06 18.15.20

Overall picture

スクリーンショット 2020-09-06 18.31.39

Don't forget the escape

I forgot,
So I will not forget

[templates/cell/Inbox/display.php]

h($tag->title)

reference

Discussion (0)

pic
Editor guide