course when you register on learnphalcon
The examples on this site are currently tested to work on Phalcon V3.4 and Phalcon Devtools V3.2 Some issues may arise when using later versions.

Please get in touch or post a comment below the post if you encounter a problem.

The simple three-tier View inheritance model which is implemented by default by Phalcon works well for most simple websites but what happens when the website has sections or groupings which don't map directly to one Controller? Programmers need to be able to group multiple controllers into one section and have them pull-in content which might reused specifically for that section.

For this we need to create a custom layout template and place it in the /app/views/layouts folder.

Then for each controller which will use that content we add an initialize function to the controller which sets a beforeLayout for that controller.

The following example will modify the tennisClub application so that a certain group of Controllers within the site will pull in a banner ad that should only be displayed on within those pages.

To create the image for the add download the file coachingAd.jpg and save it to the folder /tennisClub/public/img as coachingAd.jpg.

Next we need to create a custom layout which will display the banner ad across the top of the page. Save the following code to the file /app/views/layouts/coachingAd.phtml

<img class="img-responsive center-block" src="<?php echo $this->url->getBaseURI()?>img/coachingAd.jpg">
<div class="row center-block">
    <?php echo $this->getContent(); ?>

The div container below the image will pull in template and view files which are lower down in the hierarchy to the getContent() placeholder.

To add this custom layout file to a the Member controller - add the following function to the member Controller.

public function initialize()

This will ensure that the layout coachingAd.phtml is pulled in just above subsequent views whenever any action in the member controller is called.

It is also possible to be a bit more surgical and only add in the template for specific actions. To demonstrate this add the following line as the first line of the newAction() function within /app/controllers/BookingController.php


Now whenever any action of the MemberController is called a banner ad for coaching will displayed. If the searchAction of the BookingController is called the ad will be also displayed but only for the searchAction.