2

I'm repeatedly having to include one variable when I display my views:

$this->load->view('login', array('logged_in' => $this->auth->is_logged_in()));
$this->load->view('somepage', array('logged_in' => $this->auth->is_logged_in()));
$this->load->view('anotherpage', array('logged_in' => $this->auth->is_logged_in()));

How can I include this one variable across all of my view outputs? Is there a simpler method than extending the templating class?

jSherz
  • 927
  • 4
  • 14
  • 33

4 Answers4

3

One simpler way would be to make the array into a variable, so you dont have to type it out all the time, e.g.

$params = array('logged_in' => $this->auth->is_logged_in());
$this->load->view('login', $params);
$this->load->view('somepage', $params);
$this->load->view('anotherpage', $params);

An alternative would be to create a Helper that returns whether a user is logged in. Helpers are globally available in your controllers and views. See http://codeigniter.com/user_guide/general/helpers.html and also

Community
  • 1
  • 1
Gordon
  • 312,688
  • 75
  • 539
  • 559
0

how about using sessions?

$this->session->userdata($var);

or cookies

$this->input->cookie($var, TRUE);

thanks.

Adam
  • 1,684
  • 14
  • 18
0

Great solution, Gordon! But, depending on the case, it's also possible to use the Most Simple Template Library.

Regards!

Tárcio Zemel
  • 821
  • 10
  • 28
0

You can also access the class directly from within your view:

<?php if( $this->auth->is_logged_in() ): ?>
  Hello!
<?php endif; ?>

It's not the greatest solution but I find it works well with user conditionals.

Eddie
  • 12,898
  • 3
  • 25
  • 32