In the previous section, I created the three views that will be used for generating independently several section of a web page. Therefore, the last step that I’m going to take will consist of building the layout view, which was called “main_page.php.” Obviously, this particular file is tasked with concatenating the contents of the other views, meaning that its signature will be very simple to code. Below you'll see the entire source code corresponding to this layout file. Have a look at it, please: <!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> If you were expecting to see a lengthy view, you might feel a little disappointed. Apart from including a few basic CSS styles, the above layout file contains only one “echo” PHP statement that appends the contents of the other views to generate the header, main area and footer section of the web page. Of course, this isn’t the only approach that can be utilized for building front-ends dynamically with CodeIgniter, but it certainly is a common one. Finally, now that you've learned how to handle views in a few clever ways, feel free to tweak all of the code samples developed in this tutorial. Doing this should give you a more solid understanding of how to create web pages with CI. Whatever you do from this point onward, one thing is for sure: fun is already guaranteed! Final thoughts It’s really hard to believe, but we’ve come to the end of this series. If you’ve been a patient reader and examined in depth all of the code samples included in these tutorials, then by this time should have a clear idea of how to process views with CodeIgniter. As you can see, there is not just one right way to handle views; CI offers several approaches that can be used to suit the requirements of a specific PHP application. From my point of view as a web developer, I find returning strings from views a very useful method for building web pages where static and dynamic contents are generated independently, but again, this is only a matter of personal preference. See you in the next PHP development tutorial!
blog comments powered by Disqus |
|
|
|
|
|
|
|