DEV Community

Cover image for Create cell【CakePHP】
sachiko-kame
sachiko-kame

Posted on

Create cell【CakePHP】

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);
    }

}

Enter fullscreen mode Exit fullscreen mode

[templates/cell/Inbox/display.php]

<div>
    message✨ <?= $sample ?> 
</div>
Enter fullscreen mode Exit fullscreen mode

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);
    }

}
Enter fullscreen mode Exit fullscreen mode

[templates/cell/Inbox/display.php]

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

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)
Enter fullscreen mode Exit fullscreen mode

reference

Top comments (0)