Views

  • class Cake\View\View
  • Views are the V in MVC. Views are responsible for generating the specificoutput required for the request. Often this is in the form of HTML, XML, orJSON, but streaming files and creating PDF’s that users can download are alsoresponsibilities of the View Layer.

CakePHP comes with a few built-in View classes for handling the most commonrendering scenarios:

  • To create XML or JSON webservices you can use the JSON and XML views.
  • To serve protected files, or dynamically generated files, you can useSending Files.
  • To create multiple themed views, you can use Themes.

The App View

AppView is your application’s default View class. AppView itself extendsthe Cake\View\View class included in CakePHP and is defined insrc/View/AppView.php as follows:

  1. <?php
  2. namespace App\View;
  3.  
  4. use Cake\View\View;
  5.  
  6. class AppView extends View
  7. {
  8. }

You can use your AppView to load helpers that will be used for every viewrendered in your application. CakePHP provides an initialize() method thatis invoked at the end of a View’s constructor for this kind of use:

  1. <?php
  2. namespace App\View;
  3.  
  4. use Cake\View\View;
  5.  
  6. class AppView extends View
  7. {
  8.  
  9. public function initialize()
  10. {
  11. // Always enable the MyUtils Helper
  12. $this->loadHelper('MyUtils');
  13. }
  14.  
  15. }

View Templates

The view layer of CakePHP is how you speak to your users. Most of the time yourviews will be rendering HTML/XHTML documents to browsers, but you might alsoneed to reply to a remote application via JSON, or output a CSV file for a user.

CakePHP template files have a default extension of .ctp (CakePHP Template)and utilize the alternative PHP syntaxfor control structures and output. These files contain the logic necessary toprepare the data received from the controller into a presentation format that isready for your audience.

Alternative Echos

Echo, or print a variable in your template:

  1. <?php echo $variable; ?>

Using Short Tag support:

  1. <?= $variable ?>

Alternative Control Structures

Control structures, like if, for, foreach, switch, and whilecan be written in a simplified format. Notice that there are no braces. Instead,the end brace for the foreach is replaced with endforeach. Each of thecontrol structures listed above has a similar closing syntax: endif,endfor, endforeach, and endwhile. Also notice that instead of usinga semicolon after each structure (except the last one), there is acolon.

The following is an example using foreach:

  1. <ul>
  2. <?php foreach ($todo as $item): ?>
  3. <li><?= $item ?></li>
  4. <?php endforeach; ?>
  5. </ul>

Another example, using if/elseif/else. Notice the colons:

  1. <?php if ($username === 'sally'): ?>
  2. <h3>Hi Sally</h3>
  3. <?php elseif ($username === 'joe'): ?>
  4. <h3>Hi Joe</h3>
  5. <?php else: ?>
  6. <h3>Hi unknown user</h3>
  7. <?php endif; ?>

If you’d prefer using a templating language likeTwig, a subclass of View will bridge yourtemplating language and CakePHP.

Template files are stored in src/Template/, in a folder named after thecontroller that uses the files, and named after the action it corresponds to.For example, the view file for the Products controller’s view() action, wouldnormally be found in src/Template/Products/view.ctp.

The view layer in CakePHP can be made up of a number of different parts. Eachpart has different uses, and will be covered in this chapter:

  • templates: Templates are the part of the page that is unique to the actionbeing run. They form the meat of your application’s response.
  • elements: small, reusable bits of view code. Elements are usually renderedinside views.
  • layouts: template files that contain presentational code that wraps manyinterfaces in your application. Most views are rendered inside a layout.
  • helpers: these classes encapsulate view logic that is needed in manyplaces in the view layer. Among other things, helpers in CakePHP can help youbuild forms, build AJAX functionality, paginate model data, or serve RSSfeeds.
  • cells: these classes provide miniature controller-like features forcreating self contained UI components. See the View Cellsdocumentation for more information.

View Variables

Any variables you set in your controller with set() will be available inboth the view and the layout your action renders. In addition, any set variableswill also be available in any element. If you need to pass additional variablesfrom the view to the layout you can either call set() in the view template,or use a Using View Blocks.

You should remember to always escape any user data before outputting it asCakePHP does not automatically escape output. You can escape user content withthe h() function:

  1. <?= h($user->bio); ?>

Setting View Variables

  • Cake\View\View::set(string $var, mixed $value)
  • Views have a set() method that is analogous to the set() found inController objects. Using set() from your view file will add the variables tothe layout and elements that will be rendered later. SeeSetting View Variables for more information on using set().

In your view file you can do:

  1. $this->set('activeMenuButton', 'posts');

Then, in your layout, the $activeMenuButton variable will be available andcontain the value ‘posts’.

Extending Views

View extending allows you to wrap one view in another. Combining this withview blocks gives you a powerful way to keep your viewsDRY. For example, your application has a sidebar that needs to changedepending on the specific view being rendered. By extending a common view file,you can avoid repeating the common markup for your sidebar, and only define theparts that change:

  1. <!-- src/Template/Common/view.ctp -->
  2. <h1><?= h($this->fetch('title')) ?></h1>
  3. <?= $this->fetch('content') ?>
  4.  
  5. <div class="actions">
  6. <h3>Related actions</h3>
  7. <ul>
  8. <?= $this->fetch('sidebar') ?>
  9. </ul>
  10. </div>

The above view file could be used as a parent view. It expects that the viewextending it will define the sidebar and title blocks. The contentblock is a special block that CakePHP creates. It will contain all theuncaptured content from the extending view. Assuming our view file has a$post variable with the data about our post, the view could look like:

  1. <!-- src/Template/Posts/view.ctp -->
  2. <?php
  3. $this->extend('/Common/view');
  4.  
  5. $this->assign('title', $post->title);
  6.  
  7. $this->start('sidebar');
  8. ?>
  9. <li>
  10. <?php
  11. echo $this->Html->link('edit', [
  12. 'action' => 'edit',
  13. $post->id
  14. ]);
  15. ?>
  16. </li>
  17. <?php $this->end(); ?>
  18.  
  19. // The remaining content will be available as the 'content' block
  20. // In the parent view.
  21. <?= h($post->body) ?>

The post view above shows how you can extend a view, and populate a set ofblocks. Any content not already in a defined block will be captured and put intoa special block named content. When a view contains a call to extend(),execution continues to the bottom of the current view file. Once it is complete,the extended view will be rendered. Calling extend() more than once in aview file will override the parent view that will be processed next:

  1. $this->extend('/Common/view');
  2. $this->extend('/Common/index');

The above will result in /Common/index.ctp being rendered as the parent viewto the current view.

You can nest extended views as many times as necessary. Each view can extendanother view if desired. Each parent view will get the previous view’s contentas the content block.

Note

You should avoid using content as a block name in your application.CakePHP uses this for uncaptured content in extended views.

You can get the list of all populated blocks using the blocks() method:

  1. $list = $this->blocks();

Using View Blocks

View blocks provide a flexible API that allows you to define slots or blocks inyour views/layouts that will be defined elsewhere. For example, blocks are idealfor implementing things such as sidebars, or regions to load assets at thebottom/top of the layout. Blocks can be defined in two ways: either as acapturing block, or by direct assignment. The start(), append(),prepend(), assign(), fetch(), and end() methods allow you towork with capturing blocks:

  1. // Create the sidebar block.
  2. $this->start('sidebar');
  3. echo $this->element('sidebar/recent_topics');
  4. echo $this->element('sidebar/recent_comments');
  5. $this->end();
  6.  
  7. // Append into the sidebar later on.
  8. $this->start('sidebar');
  9. echo $this->fetch('sidebar');
  10. echo $this->element('sidebar/popular_topics');
  11. $this->end();

You can also append into a block using append():

  1. $this->append('sidebar');
  2. echo $this->element('sidebar/popular_topics');
  3. $this->end();
  4.  
  5. // The same as the above.
  6. $this->append('sidebar', $this->element('sidebar/popular_topics'));

If you need to clear or overwrite a block there are a couple of alternatives.The reset() method will clear or overwrite a block at any time. Theassign() method with an empty content string can also be used to clear thespecified block.:

  1. // Clear the previous content from the sidebar block.
  2. $this->reset('sidebar');
  3.  
  4. // Assigning an empty string will also clear the sidebar block.
  5. $this->assign('sidebar', '');

New in version 3.2: View::reset() was added in 3.2

Assigning a block’s content is often useful when you want to convert a viewvariable into a block. For example, you may want to use a block for the pagetitle, and sometimes assign the title as a view variable in the controller:

  1. // In view file or layout above $this->fetch('title')
  2. $this->assign('title', $title);

The prepend() method allows you to prepend content to an existing block:

  1. // Prepend to sidebar
  2. $this->prepend('sidebar', 'this content goes on top of sidebar');

Displaying Blocks

You can display blocks using the fetch() method. fetch() will output ablock, returning ‘’ if a block does not exist:

  1. <?= $this->fetch('sidebar') ?>

You can also use fetch to conditionally show content that should surround ablock should it exist. This is helpful in layouts, or extended views where youwant to conditionally show headings or other markup:

  1. // In src/Template/Layout/default.ctp
  2. <?php if ($this->fetch('menu')): ?>
  3. <div class="menu">
  4. <h3>Menu options</h3>
  5. <?= $this->fetch('menu') ?>
  6. </div>
  7. <?php endif; ?>

You can also provide a default value for a block if it does not exist.This allows you to add placeholder content when a block does not exist.You can provide a default value using the second argument:

  1. <div class="shopping-cart">
  2. <h3>Your Cart</h3>
  3. <?= $this->fetch('cart', 'Your cart is empty') ?>
  4. </div>

Using Blocks for Script and CSS Files

The HtmlHelper ties into view blocks, and its script(), css(), andmeta() methods each update a block with the same name when used with theblock = true option:

  1. <?php
  2. // In your view file
  3. $this->Html->script('carousel', ['block' => true]);
  4. $this->Html->css('carousel', ['block' => true]);
  5. ?>
  6.  
  7. // In your layout file.
  8. <!DOCTYPE html>
  9. <html lang="en">
  10. <head>
  11. <title><?= h($this->fetch('title')) ?></title>
  12. <?= $this->fetch('script') ?>
  13. <?= $this->fetch('css') ?>
  14. </head>
  15. // Rest of the layout follows

The Cake\View\Helper\HtmlHelper also allows you to control whichblock the scripts and CSS go to:

  1. // In your view
  2. $this->Html->script('carousel', ['block' => 'scriptBottom']);
  3.  
  4. // In your layout
  5. <?= $this->fetch('scriptBottom') ?>

Layouts

A layout contains presentation code that wraps around a view. Anything you wantto see in all of your views should be placed in a layout.

CakePHP’s default layout is located at src/Template/Layout/default.ctp.If you want to change the overall look of your application, then this is theright place to start, because controller-rendered view code is placed inside ofthe default layout when the page is rendered.

Other layout files should be placed in src/Template/Layout. When you createa layout, you need to tell CakePHP where to place the output of your views. Todo so, make sure your layout includes a place for $this->fetch('content')Here’s an example of what a default layout might look like:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title><?= h($this->fetch('title')) ?></title>
  5. <link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
  6. <!-- Include external files and scripts here (See HTML helper for more info.) -->
  7. <?php
  8. echo $this->fetch('meta');
  9. echo $this->fetch('css');
  10. echo $this->fetch('script');
  11. ?>
  12. </head>
  13. <body>
  14.  
  15. <!-- If you'd like some sort of menu to
  16. show up on all of your views, include it here -->
  17. <div id="header">
  18. <div id="menu">...</div>
  19. </div>
  20.  
  21. <!-- Here's where I want my views to be displayed -->
  22. <?= $this->fetch('content') ?>
  23.  
  24. <!-- Add a footer to each displayed page -->
  25. <div id="footer">...</div>
  26.  
  27. </body>
  28. </html>

The script, css and meta blocks contain any content defined in theviews using the built-in HTML helper. Useful for including JavaScript and CSSfiles from views.

Note

When using HtmlHelper::css() or HtmlHelper::script() in templatefiles, specify 'block' => true to place the HTML source in a block withthe same name. (See API for more details on usage).

The content block contains the contents of the rendered view.

You can set the title block content from inside your view file:

  1. $this->assign('title', 'View Active Users');

Empty values for the title block will be automatically replaced with arepresentation of the current template path, such as 'Admin/Articles'.

You can create as many layouts as you wish: just place them in thesrc/Template/Layout directory, and switch between them inside of yourcontroller actions using the controller or view’s $layout property:

  1. // From a controller
  2. public function view()
  3. {
  4. // Set the layout.
  5. $this->viewBuilder()->setLayout('admin');
  6.  
  7. // Before 3.4
  8. $this->viewBuilder()->layout('admin');
  9.  
  10. // Before 3.1
  11. $this->layout = 'admin';
  12. }
  13.  
  14. // From a view file
  15. $this->layout = 'loggedin';

For example, if a section of my site included a smaller ad banner space, I mightcreate a new layout with the smaller advertising space and specify it as thelayout for all controllers’ actions using something like:

  1. namespace App\Controller;
  2.  
  3. class UsersController extends AppController
  4. {
  5. public function viewActive()
  6. {
  7. $this->set('title', 'View Active Users');
  8. $this->viewBuilder()->setLayout('default_small_ad');
  9.  
  10. // or the following before 3.4
  11. $this->viewBuilder()->layout('default_small_ad');
  12.  
  13. // or the following before 3.1
  14. $this->layout = 'default_small_ad';
  15. }
  16.  
  17. public function viewImage()
  18. {
  19. $this->viewBuilder()->setLayout('image');
  20.  
  21. // Output user image
  22. }
  23. }

Besides a default layout CakePHP’s official skeleton app also has an ‘ajax’layout. The Ajax layout is handy for crafting AJAX responses - it’s an emptylayout. (Most AJAX calls only require a bit of markup in return, rather than afully-rendered interface.)

The skeleton app also has a default layout to help generate RSS.

Using Layouts from Plugins

If you want to use a layout that exists in a plugin, you can use pluginsyntax. For example, to use the contact layout from the Contacts plugin:

  1. namespace App\Controller;
  2.  
  3. class UsersController extends AppController
  4. {
  5. public function viewActive()
  6. {
  7. $this->viewBuilder()->setLayout('Contacts.contact');
  8. // or the following before 3.4
  9. $this->viewBuilder()->layout('Contacts.contact');
  10. // or the following before 3.1
  11. $this->layout = 'Contacts.contact';
  12. }
  13. }

Elements

  • Cake\View\View::element(string $elementPath, array $data, array $options = [])
  • Many applications have small blocks of presentation code that need to berepeated from page to page, sometimes in different places in the layout. CakePHPcan help you repeat parts of your website that need to be reused. These reusableparts are called Elements. Ads, help boxes, navigational controls, extra menus,login forms, and callouts are often implemented in CakePHP as elements. Anelement is basically a mini-view that can be included in other views, inlayouts, and even within other elements. Elements can be used to make a viewmore readable, placing the rendering of repeating elements in its own file. Theycan also help you re-use content fragments in your application.

Elements live in the src/Template/Element/ folder, and have the .ctpfilename extension. They are output using the element method of the view:

  1. echo $this->element('helpbox');

Passing Variables into an Element

You can pass data to an element through the element’s second argument:

  1. echo $this->element('helpbox', [
  2. "helptext" => "Oh, this text is very helpful."
  3. ]);

Inside the element file, all the passed variables are available as members ofthe parameter array. In the above example, thesrc/Template/Element/helpbox.ctp file can use the $helptext variable:

  1. // Inside src/Template/Element/helpbox.ctp
  2. echo $helptext; // Outputs "Oh, this text is very helpful."

Keep in mind that in those view vars are merged with the view vars from the viewitself. So all view vars set using Controller::set() in the controller andView::set() in the view itself are also available inside the element.

The View::element() method also supports options for the element.The options supported are ‘cache’ and ‘callbacks’. An example:

  1. echo $this->element('helpbox', [
  2. "helptext" => "This is passed to the element as $helptext",
  3. "foobar" => "This is passed to the element as $foobar",
  4. ],
  5. [
  6. // uses the "long_view" cache configuration
  7. "cache" => "long_view",
  8. // set to true to have before/afterRender called for the element
  9. "callbacks" => true
  10. ]
  11. );

Element caching is facilitated through the Cache class. You can configureelements to be stored in any Cache configuration you’ve set up. This gives you agreat amount of flexibility to decide where and for how long elements arestored. To cache different versions of the same element in an application,provide a unique cache key value using the following format:

  1. $this->element('helpbox', [], [
  2. "cache" => ['config' => 'short', 'key' => 'unique value']
  3. ]
  4. );

If you need more logic in your element, such as dynamic data from a datasource,consider using a View Cell instead of an element. Find out more about ViewCells.

Caching Elements

You can take advantage of CakePHP view caching if you supply a cache parameter.If set to true, it will cache the element in the ‘default’ Cacheconfiguration. Otherwise, you can set which cache configuration should be used.See Caching for more information on configuringCache. A simple example of caching an element would be:

  1. echo $this->element('helpbox', [], ['cache' => true]);

If you render the same element more than once in a view and have cachingenabled, be sure to set the ‘key’ parameter to a different name each time. Thiswill prevent each successive call from overwriting the previous element() call’scached result. For example:

  1. echo $this->element(
  2. 'helpbox',
  3. ['var' => $var],
  4. ['cache' => ['key' => 'first_use', 'config' => 'view_long']]
  5. );
  6.  
  7. echo $this->element(
  8. 'helpbox',
  9. ['var' => $differenVar],
  10. ['cache' => ['key' => 'second_use', 'config' => 'view_long']]
  11. );

The above will ensure that both element results are cached separately. If youwant all element caching to use the same cache configuration, you can avoid somerepetition by setting View::$elementCache to the cache configuration youwant to use. CakePHP will use this configuration when none is given.

Requesting Elements from a Plugin

If you are using a plugin and wish to use elements from within the plugin, justuse the familiar plugin syntax. If the view is being rendered for aplugin controller/action, the plugin name will automatically be prefixed ontoall elements used, unless another plugin name is present.If the element doesn’t exist in the plugin, it will look in the main APPfolder:

  1. echo $this->element('Contacts.helpbox');

If your view is a part of a plugin, you can omit the plugin name. For example,if you are in the ContactsController of the Contacts plugin, the following:

  1. echo $this->element('helpbox');
  2. // and
  3. echo $this->element('Contacts.helpbox');

are equivalent and will result in the same element being rendered.

For elements inside subfolder of a plugin(e.g., plugins/Contacts/Template/Element/sidebar/helpbox.ctp), use thefollowing:

  1. echo $this->element('Contacts.sidebar/helpbox');

Requesting Elements from the App

If you are within a plugin’s template file and want to renderan element residing in your main application rather than thisor another plugin, use the following:

  1. echo $this->element('some_global_element', [], ['plugin' => false]);
  2. // or...
  3. echo $this->element('some_global_element', ['localVar' => $someData], ['plugin' => false]);

Routing prefix and Elements

New in version 3.0.1.

If you have a Routing prefix configured, the Element path resolution can switchto a prefix location, as Layouts and action View do.Assuming you have a prefix “Admin” configured and you call:

  1. echo $this->element('my_element');

The element first be looked for in src/Template/Admin/Element/. If such afile does not exist, it will be looked for in the default location.

Caching Sections of Your View

  • Cake\View\View::cache(callable $block, array $options = [])
  • Sometimes generating a section of your view output can be expensive because ofrendered View Cells or expensive helper operations. To help make yourapplication run faster CakePHP provides a way to cache view sections:
  1. // Assuming some local variables
  2. echo $this->cache(function () use ($user, $article) {
  3. echo $this->cell('UserProfile', [$user]);
  4. echo $this->cell('ArticleFull', [$article]);
  5. }, ['key' => 'my_view_key']);

By default cached view content will go into the View::$elementCache cacheconfig, but you can use the config option to change this.

View Events

Like Controller, view trigger several events/callbacks that you can use toinsert logic around the rendering life-cycle:

Event List

  • View.beforeRender
  • View.beforeRenderFile
  • View.afterRenderFile
  • View.afterRender
  • View.beforeLayout
  • View.afterLayout
    You can attach application event listeners tothese events or use Helper Callbacks.

Creating Your Own View Classes

You may need to create custom view classes to enable new types of data views, oradd additional custom view-rendering logic to your application. Like mostcomponents of CakePHP, view classes have a few conventions:

  • View class files should be put in src/View. For example:src/View/PdfView.php
  • View classes should be suffixed with View. For example: PdfView.
  • When referencing view class names you should omit the View suffix. Forexample: $this->viewBuilder()->setClassName('Pdf');.
    You’ll also want to extend View to ensure things work correctly:
  1. // In src/View/PdfView.php
  2. namespace App\View;
  3.  
  4. use Cake\View\View;
  5.  
  6. class PdfView extends View
  7. {
  8. public function render($view = null, $layout = null)
  9. {
  10. // Custom logic here.
  11. }
  12. }

Replacing the render method lets you take full control over how your content isrendered.

More About Views