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.
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>