cakephp: use view blocks generated by cell within main layout/vew/request

This is a (multiple allowed):

  • bug

  • enhancement

  • feature-discussion (RFC)

  • CakePHP Version: 3.4.x

What you did

A way to render blocks(like js making with scriptBlock() ) within main layout

//cell
$this->Html->scriptBlock('alert("hi")', ['block' => 'blockName']);
//layout : outside of cell
$this->fetch('blockName');

I can explain more with sample if you need

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 3
  • Comments: 32 (28 by maintainers)

Most upvoted comments

You could also pass the current view into the cell as an argument. You’d then be able to use the ‘parent’ view’s helpers. Something like:

class SharedStateCell
{
  public function share($parentView)
  {
    $this->set('parentView', $parentView);
  }
}

// cell template
$parentView->Html->scriptBlock('alert("hi")', ['block' => 'blockName']);

// Rendering in the parent view
<?= $this->cell('SharedState::share', [$this]) ?>

I’m not convinced that we should break the encapsulation/isolation when there are existing solutions.

Oh I figured it out! You can do it without any changes. We just need to document this better.

An example index.ctp using a cell:

   $cached = Cache::remember('myCell', function() {
        $view = $this->cell('Example')->createView();
        return [
            'html' => $view->render(),
            'script' => $view->Blocks->get('script')
        ];
   });

  echo $cached['html'];
  $this->Blocks->concat('script', $cached['script']);

I think the above would solve the problem. We need some way of making this easy for people to do.

I was thinking we could add a ‘cache’ parameter to the options array when calling View::cell but the problem there is that the cell method returns an object.

I’ll think on this for a bit.

This needs to be fixed without introducing special new methods that a developer needs to know about. Otherwise, it will be impossible for plugin developers to use the script block from inside their plugin’s cells.

You have one call render the html for the cell, and a second call render the Javascript for the cell.

if js vars/Html created by model data( DB/… ) we need access 2 time to DB(for example) I know we can using cache But … is not fast way for developing 😃
and using of 2 cells is not clear

I do not think the cells are designed for this, they should be small independent sub-renderings so to speak.

I agree with you but for some usage we need for that , like when using scriptBlock() within Cell and we need render it after other js in mail view element cannot support total of usages, because we need access to model (for example)

I think he wants to share view blocks between cells and the main view rendering process.