Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] practice with views
#6

[eluser]pickupman[/eluser]
I also have a few extra properties in MY_Controller

Code:
class MY_Controller extends Controller{
  
   public $view    = ''; //view file to set an explicit view to override magic
   public $jquery  = ''; //jquery file to add a jquery script from a view
   public $title   = ''; //page title
   public $content = ''; //page content set explicit content if not needing a view
   public $data; //store for every view to use

  public function __construct(){
    parent::__construct();
  }

  protected function _display(){
    //Guess view file
    $method = $this->uri->segment(1,'home'); //set default controller as 2nd parameter
    $action = $this->uri->segment(2,'index');

    //Pass data to all views
    $this->load->vars($this->data);

    $this->load->view('header_view');
    

    //Look for application/views/(controller name)/tpl.(action name).php
    if(empty($this->view) && file_exists(APPPATH . 'views/' . strtolower($method) . '/tpl.'. $action.'.php')) {
        $this->load->view(strtolower($method) . '/tpl.'. $action .'.php');
    //Look for application/views/($this->view)
    }elseif(!empty($this->view)){
        $this->load->view($this->view);
    //No views found lets just send something out from $content property
    }else{
        echo $this->content;
    }

    $this->load->view('sidebar_view');
    $this->load->view('footer_view');  
  }
}

Now if you name your views to match your action names like you have /blog/view create a views with a structure like:
/application
/views
/blog
tpl.view.php

This will autoload the view for you by using $this->_display(); You could a add a sidebar property to it, and try to autoguess it, explicitly load it, or have a default sidebar using the exact code above.

Storing all the data under $this->data would be the most efficient as it is one object, rather than having $data_header, $data_sidebar, etc. Either way PHP is having to store the same amount of data in its memory regardless of how many variables, but PHP doesn't have to keep track of multiple variables.


Messages In This Thread
[SOLVED] practice with views - by El Forum - 12-06-2010, 02:01 PM
[SOLVED] practice with views - by El Forum - 12-08-2010, 03:46 PM
[SOLVED] practice with views - by El Forum - 12-09-2010, 02:49 AM
[SOLVED] practice with views - by El Forum - 12-09-2010, 04:47 AM
[SOLVED] practice with views - by El Forum - 12-10-2010, 10:37 AM
[SOLVED] practice with views - by El Forum - 12-10-2010, 01:17 PM
[SOLVED] practice with views - by El Forum - 12-11-2010, 02:17 AM



Theme © iAndrew 2016 - Forum software by © MyBB