tags:

views:

37

answers:

1

In Codeigniter I want to know the best practice for using the same views with various functions in a controller.

For eg in the index function I have
$locals['somevar'] = "some thing";

    $this->load->view('welcome_message', $locals);

in my view I have something a bit like this:

<?php if($somevar):?>       
    <?=$somevar?>
<?php endif;?>

Attempting to do a Ruby on Rails thing where I can check the existence of Flash/notice before showing it.

However in the test function (i.e not passing a variable to the view this time)

   $this->load->view('welcome_message')

the view seems to need a $somevar value and errors.

My question is this: Do I have to declare (repeat) the variables and set them to something on every function in a controller tnat wants to use that particular view? I am probably missing something obvious and there is probably a better way of approaching this. Thanks for you help in advance.

+4  A: 
<?php if (isset($somevar)): ?>       
    <?php echo $somevar; ?>
<?php endif; ?>
Petah