Having demonstrated how simple it is to remove (at least partially) the presentation logic from a view file and delegate part of this task to the controller, here are all the source files corresponding to this sample PHP application, after we've introduced the changes that you learned in the prior section. Take a look at them, please: (‘webpage.php’ file – located at /application/controllers/ folder) <?php class WebPage extends Controller{ function WebPage(){ // load controller parent parent::Controller(); // load libraries here $this->load->database(); // load helpers here } // generate web page using partial sections function index(){ // generate headers section $data['header']=$this->load->view('header_view',array('header'=>'Header Section'),TRUE); $query=$this->db->get('users'); if($query->num_rows > 0){ // generate content section $data['content']=NULL; foreach($query->result() as $user){ $data['content'].=$this->load->view('users_view',array('user'=>$user),TRUE); } } // generate footer section $data['footer']=$this->load->view('footer_view',array('footer'=>'Footer Section'),TRUE); // generate full web page $this->load->view('main_page',$data); } } ?> (‘main_page.php’ file - located at /application/views/ folder) <!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> <style type="text/css"> body{ padding: 0; margin: 0; background: #999; } #container{ width: 600px; margin: 0 auto; } #header{ padding: 10px; background: #eee; } #content{ padding: 10px; background: #9cf; } #footer{ padding: 10px; background: #eee; } h1{ font: bold 2em Arial, Helvetica, sans-serif; margin: 0 0 18px 0; color: #039; } h2{ font: bold 1.5em Arial, Helvetica, sans-serif; margin: 0 0 18px 0; } p{ font: normal .8em Arial, Helvetica, sans-serif; margin: 0 0 18px 0; } </style> </head> <body> <div id="container"> <?php echo $header.$content.$footer; ?> </div> </body> </html> (‘users_view.php’ file - located at /application/views/ folder) <p><strong>First Name: </strong><?php echo $user->firstname;?></p> <p><strong>Last Name: </strong><?php echo $user->lastname;?></p> <p><strong>Email: </strong><?php echo $user->email;?></p> <hr /> (‘footer_view.php’ file - located at /application/views/ folder) <div id="footer"> <h2><?php echo $footer;?></h2> <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut.</p> </div> If you already have a fresh installation of CodeIgniter working on your web server, then when you type the following URL on your browser: http://localhost/codeogniter/index.php/webpage You should get the following output echoed to the screen:
Regardless of the simple structure of this sample PHP application, it’s useful for demonstrating another approach when it comes to handling views with CodeIgniter. As usual with other articles of this series, feel free to tweak all of the code samples shown in this article. This will help you understand more quickly how to create view files that include only basic presentation logic. Final thoughts In this fifth part of the series, I demonstrated yet another approach that can be used for partially removing presentation logic from views. Despite the fact that this method isn’t as intuitive as others utilized in previous articles, it might be appealing to you. Again, it’s valid to say that there’s not an official way to work with CodeIgniter, and it depends on you what approach to use when developing your own PHP applications. Now, returning to the examples developed so far in this series, you may have noticed that none of them have utilized a model to access the “users” MySQL table. It’s not mandatory to code a model with CI, particularly if the program you’re planning to build is fairly basic or your implementation of the MVC pattern is loose. However, in the next chapter of this series I’ll be defining a model, which will be incorporated into the PHP program developed earlier. Want to see how this will be done? Then, don’t miss the forthcoming part!
blog comments powered by Disqus |
|
|
|
|
|
|
|