Book Image

CakePHP 2 Application Cookbook

Book Image

CakePHP 2 Application Cookbook

Overview of this book

Table of Contents (20 chapters)
CakePHP 2 Application Cookbook
Credits
Foreword
About the Authors
About the Reviewer
www.PacktPub.com
Preface
Index

Using blocks


View blocks are snippets of content, which are captured at runtime, and then manipulated and rendered to a view.

In this recipe, we'll look at how to extend a view, and then use view blocks to populate certain areas of it.

Getting ready

For this recipe, we'll reuse the existing controller, which is ProductsController. If you don't have it, create a file named ProductsController.php in app/Controller/, and introduce the following content:

<?php
App::uses('AppController', 'Controller');

class ProductsController extends AppController {
}

Then, we'll create a base view, which we'll extend with our view. Create a file named base.ctp in app/View/Common/, and add the following content:

<h2><?php echo h($this->fetch('name')); ?></h2>
<div>
  <?php echo $this->fetch('content'); ?>
</div>

How to do it...

Perform the following steps:

  1. First, add a details() method to ProductsController with the following content:

    public function details() {
      $this-&gt...