CodeIgniter Forums

Full Version: Send variable to a template view
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
HI , in my project i divide the html code into 3 parts :

-view/template/header
-view/template/menu
-view/template/footer

To send variable to heder menu or footer i must set   $_SESSION variables .

There is a better way ?
@pippuccio76,

It depends on what type of variables you are storing in the session variables. What type of variables are you storing (User specific or system specific variables)?
From where are you calling these different views?

I find what makes most sense for me on current projects is we built extended core Controller class with "render" method I call last thing in my controller that handles the output.

We have $this->data array variable that we pass to view in render method, that loads all the views and passes the data when it needs to.

Your controller:
PHP Code:
class Welcome extends MY_Controller
{
   public function 
index()
   {
       
$this->data['myvar'] = 'value from controller';
       
$this->render('mainviewfile');
   }


Controller extension:
PHP Code:
class MY_Controller extends CI_Controller
{
   public 
$data = [];

   public function 
render($viewFile)
   {
       
$this->load->view('view/template/header'$this->data);
       
$this->load->view($viewFile$this->data);
       
$this->load->view('view/template/footer'$this->data);
   }

(08-06-2018, 08:21 AM)Pertti Wrote: [ -> ]From where are you calling these different views?

I find what makes most sense for me on current projects is we built extended core Controller class with "render" method I call last thing in my controller that handles the output.

We have $this->data array variable that we pass to view in render method, that loads all the views and passes the data when it needs to.

Your controller:
PHP Code:
class Welcome extends MY_Controller
{
 
  public function index()
 
  {
 
      $this->data['myvar'] = 'value from controller';
 
      $this->render('mainviewfile');
 
  }


Controller extension:
PHP Code:
class MY_Controller extends CI_Controller
{
 
  public $data = [];

 
  public function render($viewFile)
 
  {
 
      $this->load->view('view/template/header'$this->data);
 
      $this->load->view($viewFile$this->data);
 
      $this->load->view('view/template/footer'$this->data);
 
  }


i do this :


PHP Code:
class MY_Controller extends CI_Controller
{
 

 
  public function index()
 
  {
 
       $this->load->view('templates/header'$data);
 
       $this->load->view('templates/menu'$data);

 
       $this->load->view('my_controller/index'$data);

 
       $this->load->view('templates/footer'$data);
 
  }



But i cannot set in every function a variable to show in menu (f.e. username logged)
Indeed, writing the fetch code for user details for every function would be annoying.

If you don't want to extend Controller code, which gives you single function to handle user data, you could add it to controller __construct() method.

PHP Code:
class MY_Controller extends CI_Controller
{
    public 
$data = [];
    public 
$user false;

    public function 
__construct()
    {
        
parent::__construct();
        
$this->user = new User_model($this->session->userdata('user_id');
        
$this->data['username'] = $this->user->username;
    }

    public function 
index()
    {
        
// handle app logic

        
$this->data['foo'] = 'bar';
      
        
// do views as the last thing in controller
        
$this->load->view('templates/header'$this->data);
        
$this->load->view('templates/menu'$this->data);
        
$this->load->view('my_controller/index'$this->data);
        
$this->load->view('templates/footer'$this->data);
   }


Same logic would apply if you use controller extension. You could either add it to core controller __construct method so it gets loaded before CodeIgniter hits your controller method.