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?

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

1 Comment

I went with the helper option. Thanks!
0

how about using sessions?

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

or cookies

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

thanks.

1 Comment

Note that current Codeigniter sessions are Cookies.
0

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

Regards!

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.