To be frank, generating an entire web document by sequentially loading the three views that you saw before is a no-brainer process that you’ll grasp in a snap. But how can it be done? Well, first it’s necessary to define a controller class that performs this specific task. In this case, since I’m attempting to create a web page, the controller will be called “WebPage” (my creativity with names sometimes blows me away) and it’ll look like this: <?php class WebPage extends Controller{ function WebPage(){ // load controller parent parent::Controller(); // load libraries here $this->load->database(); // load helpers here } // load views sequentially function index(){ // load 'header' view $this->load->view('header_view',array('header'=>'Header Section')); // load 'content' view and pass in database content $this->load->view('content_view',array('users'=>$this->db->get('users'))); // load 'footer' view $this->load->view('footer_view',array('footer'=>'Footer Section')); } } ?> See how simple it is to build a web page by loading views in a sequential manner? The controller defined above demonstrates this process pretty clearly, since it firsts loads the CI’s database class within its constructor, and then proceeds to build the different sections of the page by loading in turn each of the previous views. Simple and efficient. Now that you hopefully understood how the “WebPage” controller works, save it under the “/application/controllers/ folder and type the following URL on your browser’s address field to test it: http://localhost/codeigniter/index.php/webpage/ If everything goes well, you should get the following output on screen:
There you have it. At this stage you've learned how to sequentially load three different views to generate a basic web page. Logically, there’s plenty of room to improve the signature of the controller or even the views themselves. But guess what? This will be left as homework for you. Final thoughts In this first episode of the series, I explained how to build different sections of a basic web page with CodeIgniter by sequentially loading three independent view files. It could be said that this approach is possibly the simplest to implement when handling views with CI, but as I said in the beginning, it’s not the only one. In the next article, I’ll be polishing the visual appearance of the web page generated previously to make it look slightly more appealing. Now that you’ve been warned about the topic that will be covered in the forthcoming part of the series, don’t miss it!
blog comments powered by Disqus |
|
|
|
|
|
|
|