Welcome Guest, Not a member yet? Register   Sign In
Default argument values for a view
#1

[eluser]tokyotech[/eluser]
How do you define default argument values for a view? It's not so easy for two reasons:

1) Naming conflict.

If a previously loaded view had the same parameter name, that parameter value is carried on to the current view.

controllers/test.php
Code:
$this->load->view('foo', array('param' => 'value'));
$this->load->view('bar');

views/foo.php
Code:
<?=$param?>

views/bar.php
Code:
<?php
  // Failed attempt to set a default value for $param.
  // $param will already be defined if the foo view was previously loaded.
  $param = isset($param) ? $param : 'default';
?>
<?=$param?>

Output:
Code:
valuevalue


2) Multiple loads of the same view.

This is related to the previous point. Even you're very careful in not defining the same parameter name across all your views, you're still going to get conflicts when loading the same view over and over.

controllers/reuse.php
Code:
$this->load->view('hello', array('world' => 'qbasic'));
$this->load->view('hello');
$this->load->view('hello');

view/hello.php
Code:
<?php
$world = isset($world) ? $world : 'default';
?>
<?=$word?>

Output:
Code:
qbasicqbasicqbasic




Theme © iAndrew 2016 - Forum software by © MyBB