As I stated in the previous segment, it’s possible to pass a third Boolean argument to the $this->load->view()” method to return its output as a string after it has been parsed. By using this approach, it is also very simple to build sections of a web page independently, in a way similar to the method shown in the preceding article. To demonstrate how this approach can be successfully implemented, first I’m going to create some new view files, which are listed below: (definition of ‘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> (definition of ‘header_view.php’ file - located at /application/views/ folder) <div id="header"> <h1><?php echo $header;?></h1> <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut.</p> </div> (definition of ‘content_view.php’ file - located at /application/views/ folder) <div id="content"> <?php if($users->num_rows > 0):?> <?php foreach($users->result() as $user):?> <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 /> <?php endforeach;?> <?php endif;?> </div> (definition of ‘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> Hopefully, at this point the group of view files coded above should be fairly familiar to you, right? Naturally, these are responsible for generating different sections of a web page, plus there’s one that acts like a general layout, called “main_page.php,” which loads the values returned from the other views. With this handy view structure at our disposal, now it’s time to see how it can be used in conjunction with the strings returned by the “$this->load->view()” method to build an entire web document. This task will be performed by a controller, and its definition will be shown in the next few lines. So, jump ahead and read the following section, please.
blog comments powered by Disqus |
|
|
|
|
|
|
|