Home arrow PHP arrow Page 2 - Handling Views with CodeIgniter

Preparing views to be rendered on screen - PHP

The Code Igniter framework makes it easy for developers to implement a Model-View-Controller design pattern in PHP. This concept can be difficult for newcomers to grasp, so this seven-part series of articles will focus on one aspect of it: views, and the many clever ways you can handle them with Code Igniter. Welcome to the first part.

TABLE OF CONTENTS:
  1. Handling Views with CodeIgniter
  2. Preparing views to be rendered on screen
  3. Creating a content view
  4. Building a web page controller class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 9
March 19, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

The first approach that I’m going to explore in this article involves loading views sequentially. To implement this specific method, I’m going to recreate a fictional scenario where an entire web page needs to be generated using the structure mentioned in the introduction. In this particular case, the header and footer sections will be static view files, while the main area will be filled in with dynamic content pulled out from a MySQL database.

Based on this scheme, it’s necessary to build the static files that will comprise this sample web page, that is, the header and footer parts. So, here are the two files that perform this task:


(definition of ‘header_view.php’ file)


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Sample Web Page</title>

</head>

<body>

<div id="container">

<div id="header">

<h1><?php echo $header;?></h1>

<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.</p>

<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.</p>

</div>



(definition of ‘footer_view.php’ file)


<div id="footer">

<h2><?php echo $footer;?></h2>

<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.</p>

<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.</p>

</div>

</div>

</body>

</html>


So far, nothing unexpected, right? As you can see, the structure of the above two view files is pretty easy to follow. In the first case, the “header_view.php” file embeds only a $footer PHP variable into the markup, while the second file does something similar, since the “footer_view.php” view includes another variable called $footer.

As I explained before, these views could be considered the static sections of the web page that I plan to generate, even though each of them will contain some data that will be passed along via a still-undefined controller class.

Assuming that the latest version of CodeIgniter has already been installed on your testing web server, then the two previous view files should be saved to the /application/views/ folder of CI, so they can be found by its loader class at a later time.

Well, at this point I demonstrated how to build the header and footer views of a basic web page, which will be loaded via a sequential method. But, actually I’m getting ahead of myself, since first we need to define the view file that corresponds to the main area of the page. As I mentioned previously, this file will be populated with some database content.

Therefore, it’s time to build this additional view file. This process will be discussed in depth in the section to come, so click on the link that appears below and read the following segment.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

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