Home > Article > Backend Development > How does CakePHP perform template inheritance?
With the continuous development of web applications, more and more open source frameworks have appeared in our sights, among which CakePHP is one of the PHP frameworks that has attracted much attention. In CakePHP, template inheritance is a very commonly used technology, which can help us better organize pages and reduce repetitive code writing. So, how does CakePHP perform template inheritance?
1. The concept of template inheritance
Template inheritance is a technology that applies a master view or template to a subview or template. Through this technology, we can extract the common content in the page, organize it into a master view, and reference the master view in the subviews. This makes it easier for us to manage and maintain pages, and also reduces redundant code writing.
2. Implementation of template inheritance in CakePHP
CakePHP provides a powerful and flexible template engine based on PHP and supports template inheritance. Next we will explain how to implement template inheritance in CakePHP.
First, we need to create a master view. The master view generally contains public content required by each page, such as the website's head navigation, bottom copyright and other information.
In the app/View/Layouts/ directory, create a new default.ctp file.
<html> <head> <title><?php echo $this->fetch('title'); ?></title> </head> <body> <header> <h1>CakePHP模板继承示例</h1> <nav> <ul> <li><a href="#">首页</a></li> <li><a href="#">关于我们</a></li> <li><a href="#">联系我们</a></li> </ul> </nav> </header> <div id="content"> <?php echo $this->fetch('content'); ?> </div> <footer> <p>©2019 CakePHP模板继承示例</p> </footer> </body> </html>
In the app/View/ directory, create a new demo.ctp file as a subview. In the demo.ctp file, we only need to write the page content that is different from the master view.
<?php $this->extend('default'); ?> <?php $this->assign('title', '这是子视图页面标题'); ?> <h2>这是子视图页面内容</h2> <p>这是一个CakePHP模板继承的示例</p>
At the top of the subview, we use $this->extend('default')
to reference the master view, so that the subview inherits the layout of the master view and structure. At the same time, we can use $this->assign('title', 'This is the subview page title')
to pass the page title.
In the content part of the subview, we can write page content that is different from the master view, and these contents will replace the corresponding placeholders in the master view. For example, in the demo.ctp file above, we use c1a436a314ed609750bd7c7d319db4da
and e388a4556c0f65e1904146cc1a846bee
tags to write the content of the subview page.
Now that we have created the master view and subviews, we can use CakePHP to render the page. In the controller, we can use $this->render('demo')
or $this->render('/Controller/demo')
to render the demo. ctp subview.
class DemoController extends AppController { public function index() { $this->render('demo'); } }
When we access the index function, CakePHP will automatically render the demo.ctp subview and embed it into the master view default.ctp.
3. Summary
In this article, we learned some basic knowledge about template inheritance in CakePHP, including creating master views, creating subviews and rendering pages, etc. Template inheritance is a very commonly used technology, which can help us better organize pages and reduce repetitive code writing. When developing with CakePHP, template inheritance is also a very useful technology that can help us manage and maintain pages more conveniently.
The above is the detailed content of How does CakePHP perform template inheritance?. For more information, please follow other related articles on the PHP Chinese website!