CI 4 Include Subviews to Master

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.

Step #1

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.

Step #2

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.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more