Using templates in CodeIgiter

This post is for CodeIgniter beginners on how to use templates in CodeIngniter views.

If you are a beginner in CodeIgniter then you might be confused about how to use your favorite template in your application design. Because when loading views in CodeIgniter controller if you don't use templates then in all the views you might end up repeating the code for common views.

So here is a quick step by step guide on using templates with CodeIngiter views.

  1. First create a file called template.php inside your CodeIgniter view folder. Inside the template include all the common code for leading header and footer, and body part call the view again for the passed view name for loading the body part of the page. A sample code will be like shown below.
     
    <html>
    <head>
    <title>
    <?php echo $title; ?>
    </title>
    </head>
    <body>
    <?php $this->load->view('header.php'); ?>
    <?php $this->load->view($content); ?>
    <?php $this->load->view('footer.php'); ?>
    </body>
    </html>
    
    
  2. Inside the controller have code similar to below for passing the view name which needs to be loaded inside the template.
     
    
    $data = array();
    $data['title'] = 'My title';
    $data['content'] = 'blog/view';
    $this->load->view('template', $data);
    

That was easy. You can extend on this as per your requirement.

Website in minutes

Do you know that you can generate a complete PHP websites in minutes with PHP Website Generator?

Features

  • Free admin dashboard
  • Role based security
  • Login with Facebook, Google and more
  • Queue based email processing
  • Price at $5 for website
  • Uses CodeIgniter framework

Generate website now See Pricing



You can generate an entire website in CodeIgniter using our PHP Website Generator using easy to use wizards. PHP Website Generator provide many features including search with pagination, AJAX based grid data editors, Group based security and free Admin dashboard etc. Please connect with Google or Facebook and to generate your CodeIgniter website in minutes.