CakePHP Extending Views

cakephp

In CakePHP, you can extend a view template by using the extend method in your view template. This allows you to create a base layout template that defines the overall structure of your application, and then extend it with specific content templates for each page.

Here’s an example of how to extend a view template in CakePHP:

Layout template (src/Template/Layout/default.php):

<html>
<head>
    <title>My App</title>
</head>
<body>
    <?= $this->fetch('content') ?>
</body>
</html>

Content template (src/Template/Posts/view.php):

<?php $this->extend('Layout/default'); ?>

<h1><?= $post->title ?></h1>
<p><?= $post->body ?></p>

In this example, the view template extends the default layout template and defines a content block that is inserted into the layout at the fetch('content') ?> placeholder.

For more information about extending views in CakePHP, you can refer to the documentation at https://book.cakephp.org/4/en/views/helpers/extend.html.