Home arrow PHP arrow Page 4 - Using Nested Views with CodeIgniter

Embedding actual data into views with a web page controller class - PHP

One of the most common tasks that a PHP programmer has to perform when developing web applications with CodeIgniter is loading a bunch of views to generate different sections of a web document. As with many other features provided by this framework, there are several ways to work with view files. This seven-part article series shows you many different ways to handle views. In this third part, we'll focus on nested views and other methods.

  1. Using Nested Views with CodeIgniter
  2. Review: loading views sequentially with CodeIgniter
  3. Preparing a nested view for displaying database contents
  4. Embedding actual data into views with a web page controller class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 10
April 02, 2009

print this article



In the previous section, you learned how to build a view file that also includes other partial views. However, it’s necessary to create a controller class that implements the logic required to generate a complete web page, which will be filled in with some database contents.

In this specific case, the controller that I’m going to define below will use the “$this->load->vars()” method included with CI’s loader class to replace the variables included within the views with actual data.

That being explained, the web page controller looks like this:


class WebPage extends Controller{

function WebPage(){

// load parent controller


// load libraries here


// load helpers here


// generate web page with nested views

function index(){

// generate header, content and footer sections

$data=array('header'=>'Header Section','users'=>$this->db->get('users'),'footer'=>'Footer Section');

// load variables in views


// load 'main_page' view





Short to code and even simpler to read. Basically, all that the above “WebPage” controller does is  generate a simple web page whose main area has been populated with the contents of a “users” MySQL table. As I explained before, the replacement of variables included in the views is achieved with the $this->load->vars()” method, before loading the view that renders the main web document.

In summary, if all has gone well, if you type the URL below into your browser’s address field:


Then you should get the following output:

Hopefully with this simple example, you learned how to build a dynamic web page with CodeIgniter by using nested views along with the “$this->load->vars()” method. As usual with my articles on PHP web development, feel free to edit all of the code samples shown in this tutorial, so you can practice handling views with CodeIgniter.

Final thoughts

In this third installment of the series, I explained how to build a dynamic web page using a combination of CodeIgniter’s “$this->load->vars()” method and nested views. Hopefully you won’t have major trouble implementing this approach successfully when building your own PHP applications with CI.

In the next chapter, I’m going to extend the concept of nested views a bit further by using a third parameter within the “$this->load->view()” method shown before.

Now that you know what the upcoming article will be about, you can’t miss it!

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: