Welcome Guest, Not a member yet? Register   Sign In
Newbie question - loading views in multiple functions
#1

[eluser]Dave S[/eluser]
I have just started picking up php in the past month. A friend recommended CI to me today so I have spent the day reading through as much of the User Guide as possible. This looks great!

Anyway, my question is regarding views. Suppose I have a controller called Members which has various functions that each display a different page (sign up, log in, account info, etc). If I have a default header and footer that I want to load on each of these pages, is it best to load them within each function? I tried creating a construct that loaded the header and that worked fine, but left me wondering what to do with the footer since I can't load it until after any given function. Is there a better way to do this?

Thanks in advance for any help!
#2

[eluser]gtech[/eluser]
you could create a library, create function in a library that takes in a view name and data aray as parameters. you the get the CI instance ($CI =& get_instance()Wink and then load the views...
eg:
Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
class Template {
    function dotemplate($template, $data)
    {
        $CI =& get_instance();
        $CI->load->view('header');
        $CI->load->view($template, $data);
        $CI->load->view('footer');
    }
}
?>

in the controller you load the library (or autoload it) and the instead of loading the view you call the dotemplate function. If you don't like that approach, then search on the forums as this is a common topic.

good luck!

I suppose you could also create a base controller that has a method which loads the header content and footer. when you create a new controller you extend from the base controller and then the template method will be available. I have not tried this approach but it should work.
#3

[eluser]Dave S[/eluser]
Great! Thanks so much!
#4

[eluser]gtech[/eluser]
no probs glad to help.




Theme © iAndrew 2016 - Forum software by © MyBB