Home arrow PHP arrow Page 4 - Returning Strings from Views with Code Igniter

Putting all of the views together - PHP

When it comes to generating both static and dynamic web pages, CodeIgniter gives PHP developers the liberty of working with different approaches that can be easily adapted to suit the requirements of a huge variety of web applications. Building web pages using CI demands that you always deal with one or multiple views, generally represented in the form of HTML files that contain embedded PHP variables. Therefore, if you’re a CI user who want to learn to manipulate views in some clever ways, keep reading. In this series of articles, you’ll find a comprehensive guide to several methods you can implement within your own CI-based applications to handle view files in a truly efficient manner.

  1. Returning Strings from Views with Code Igniter
  2. Review: using nested views along with the load-vars() method
  3. Generating partial sections of a web page
  4. Putting all of the views together
By: Alejandro Gervasio
Rating: starstarstarstarstar / 6
April 09, 2009

print this article



In order to build a complete web page with the views that you saw in the previous section, it’s necessary to define a controller class that first stores the strings returned from each of them, and then passes this content to the layout view. If this sounds a bit confusing to you, the following web page controller should help to clarify any issues that you might have. Here it is:


class WebPage extends Controller{

function WebPage(){

// load controller parent


// load libraries here


// load helpers here


// generate web page using partial sections

function index(){

// generate header section

$data['header']=$this->load->view('header_view',array('header'=>'Header Section'),TRUE);

// generate content section


// generate footer section

$data['footer']=$this->load->view('footer_view',array('footer'=>'Footer Section'),TRUE);

// generate full web page





Do you see how simple it is to generate a dynamic web page by returning strings with the “$this->load->view()” method? I guess you do. In the above example, each of the sections that make up the page are stored temporally on the $data array, and then it’s passed to the “main_page” view for displaying purposes.

Hopefully, this code sample should give you a clear idea of how to return contents from a view after they've been loaded, via the third “TRUE” argument as shown before.

And finally, assuming that all of this has been set up correctly, if you type the following URL into your browser:


Then you should get the following output:

Here you have it. At this point, you've learned how to use the “$this->load->view()” method in a slightly different way to generate a simple database-driven web application with CodeIgniter. All of the examples developed in this tutorial can be enhanced. For example, you might want to create a users model and utilize it within the controller to fetch database rows, instead of using the database class directly.

Whatever you decide to do, one thing is certain: CodeIgniter will keep you entertained for hours!

Final thoughts

In this fourth part of this series, I explained how to build a simple, yet dynamic, web page by returning strings from some views. As was illustrated earlier, this approach permits us to generate partial sections of a web document in a pretty intuitive and flexible fashion, and I’m sure that you’ll find even more useful ways to use it.

In the following article, I’ll be discussing how to make views even more "dumb" (if the term is actually applicable) by removing certain presentation logic from them. If you wish to learn how this will be done, don’t miss the upcoming tutorial!

>>> 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: