views:

449

answers:

3

I am trying to find the best way to only have to load my view one time and re-use them on different pages.

For the home page for example I have this code:

function index()
    {

     /* Load Includes for all pages */
     $header = $this->load->view('includes/header','',true);
     $scripts = $this->load->view('includes/scripts','',true);
     $navigation = $this->load->view('includes/navigation','',true);
     $footer = $this->load->view('includes/footer','',true);

     /* Load widgets for Home Page*/
     $rotator = $this->load->view('widgets/home_feature','',true);

     $login = $this->load->view('widgets/login','',true);

     $cal = $this->load->view('widgets/calendar_home','',true);
     $gallery = $this->load->view('widgets/photos_scroll','',true);
     $tags = $this->load->view('widgets/tags_view','',true);
     $spotlight = $this->load->view('widgets/spotlight','',true);
     $recent = $this->load->view('widgets/activityfeed','',true);

     $data=array(
        'title'=>'Philly2Night.com',
        'MetaDesc'=>'Cities2Night new social network',
        'MetaKeywords'=>'Social, Party, Plan, Events',
        //Load Includes
        'header'=>$header,
        'scripts'=>$scripts,
        'navigation'=>$navigation,
        'footer'=>$footer,
        //Load Widgets
        'feature'=>$rotator,
        'login'=>$login,
        'calendar'=>$cal,
        'photos'=>$gallery,
        'tags'=>$tags,
        'spotlight'=>$spotlight,
        'recent'=>$recent
        );
     $this->load->vars($data);    
     $this->load->view('pages/home_view');



    }

How do I create new pages, but referencing these views? I tried making the var global, ideally I want to use switch, and define cases but that alas did not work either.

+1  A: 

First of all you shouldn't be loading so many pages. Each file system request takes it's toll on performance. Rather than having "header" and "footer" just combine them into the "home_view" page.

Second, your question sounds like you are stating that once you load the views you want them to remain loaded for each page request afterward. That isn't possible with PHP as each request is a whole new load. However, if you are doing a lot of database queries or calculations and then loading widgets you can cache the widgets for 5 mins or so and save the queries each page. Look in the CodeIgniter forums for cache classes.

Xeoncross
this was done to load the segments in different spots on my home view.
matthewb
+1  A: 

If I understand correctly, I think you just want to have one common place for all the load code, so that you do not have to copy/paste that part for each action in the controller. If that's the case...

Create a constructor in your controller, move the load->views there, and store them into variables inside the class:

function __construct() {
    parent::__construct();
    /* Load Includes for all pages */
    $this->header = $this->load->view('includes/header','',true);
    $this->scripts = $this->load->view('includes/scripts','',true);
    $this->navigation = $this->load->view('includes/navigation','',true);
    $this->footer = $this->load->view('includes/footer','',true);
}

Be sure to change any $header, $scripts, etc references to $this->header, $this->scripts, etc.

Also, replace __construct with your class name if you're using PHP4.

jimyi
A: 

This may or may not be of some use: http://codeigniter.com/forums/viewthread/77279/

I use this on ALL of my CodeIgniter projects, it makes templating very very easy.

amr