Welcome Guest, Not a member yet? Register   Sign In
Send variable to a template view
#1

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 ?
Reply
#2

@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)?
Reply
#3

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);
   }

Reply
#4

(This post was last modified: 08-07-2018, 12:40 AM by pippuccio76.)

(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)
Reply
#5

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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB