开发者

check existence of variable in view via code igniter

In Codeigniter I want to know the best practice for using the same views with various functions 开发者_如何学JAVAin 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.


<?php if (isset($somevar)): ?>       
    <?php echo $somevar; ?>
<?php endif; ?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜