A webpage can have a sidebar, header, footer etc. The best way to load any common portion is to create a reusable block of code and load it into view files.
In the last tutorial, you have seen the concept of Creating master layout. Now, let’s say we have few sub views like header, footer etc. How to include those views into master layout.
Create any folder with your name say include inside /app/Views folder. You need to place common layouts into a single folder to keep application neat and clear.
Inside include folder we have few common layout files like header, footer.
Suppose we have a master.php layout file inside layout folder.
- header.php inside /app/Views/include
- footer.php inside /app/Views/include
- master.php inside /app/Views/layout
Include sub views to Master layout –
<html> <head> <title><?= $this->renderSection("pageTitle") ?></title> </head> <body> <?=$this->include("include/header")?> <!--Area for dynamic content --> <?= $this->renderSection("content") ?> <?=$this->include("include/footer")?> <body> </html>
$this->renderSection() Sections which renders dynamic content according to page views.
$this->include(“include/header”) Including header sub view from include folder to layout.
$this->renderSection(), $this->include() are template functions of CodeIgniter 4. To use them we don’t need to load anything.
Hi, I am Sanjay the founder of Online Web Tutor. I welcome you all guys here to join us. Here you can find the web development blog articles. You can add more skills in web development courses here.
I am a Web Developer, Motivator, Author & Blogger. I also used to take online classes including tech seminars over web development courses. We also handle our premium clients and delivered up to 50+ projects.