Welcome Guest, Not a member yet? Register   Sign In
Multi-level views with variables [SOLVED]
#1

[eluser]karlis_i[/eluser]
Hi, I'm new to CodeIgniter and whole MVC thing, and I hope you can help me clarify some things.

I'm working on an application, that has one main view (header, footer, menu, etc), some content views (tables with clients, bills, etc), and some sub-views for content (info about specific client, etc).

HTML is divided this way so that user can load views with Ajax, but I want to make this work with JavaScript turned off too.

I know that it is possible to call a view from another view, but I want also to pass variables for view and sub view from controller.
For now it looks like this-
Controller

Code:
function something()
{
$data['include'] = 'clients'; // for main view to call the right content view
$data['clients'] = $this->model->get_clients(); // gets the client array from DB
$data['client_info'] = $this->model->get_client_info($client_id);

$this->load->view('main', $data);
}

Views/Main.php
Code:
...some html
<?php
$data['clients'] = $clients; // passing variables further..
$data['client_info'] = $client_info;

$this->load->view($include, $data); // calling content view
?>
some more html...

Views/Clients.php
Code:
...some html, using client variables like this: <?=$client->id?>
<?php
if(!empty($client_info)){
$data['client_info'] = $client_info; // passing variables even further
$this->load->view('client_info', $data); // calling sub content view
}
?>
some more html...

Views/Client_info.php is simple, just html and
Code:
<?=$client_info->name?>
etc.

I hope I made it clear.
So, the question is- is it ok to do so, and if not, how should I do it?
Thanks in advance,

k_i
#2

[eluser]summery[/eluser]
Hi, karlis_i,

Instead of ponderously passing everything from one view to another, just use the $this->load->vars array, which is available to all views.

Controller:
Code:
function something()
{
$data['include'] = 'clients'; // for main view to call the right content view
$data['clients'] = $this->model->get_clients(); // gets the client array from DB
$data['client_info'] = $this->model->get_client_info($client_id);
$this->load->vars($data);
$this->load->view('main');  // No need to pass $data!
}

Views/Main.php
Code:
...some html
<?php
$this->load->view($include); // calling content view
?>
some more html...

Views/Clients.php
Code:
...some html, using client variables like this: <?=$client->id?>
<?php
if(!empty($client_info)){
  $this->load->view('client_info'); // calling sub content view
}
?>
some more html...

$this->load->vars is pretty awesome for getting rid of the problem of passing data among nested views. You can read more about it in the Loader Class documentation.
#3

[eluser]gino[/eluser]
hi,

you realy don't need to reset the data array for each view!!

Code:
//controller
function something()
{
   $data['foo'] = 'bar';
   $this->load->view('first_view', $data);
   //the data is now available in all your views you will load from first_view!!
}

//first_view.php
<div>
   &lt;?php
      echo $foo;// -> bar
      $this->load->view('sub_view');//no need to add the data again!
   ?&gt;
</div>

//sub_view.php
<div>
   &lt;?php
      echo $foo;// -> bar
      //even further sub views will have access to $foo!!
   ?&gt;
</div>

hope this helps Smile
#4

[eluser]karlis_i[/eluser]
Thank you, guys, for help!

Once again, RTFM proves to be the right way to do it - http://ellislab.com/forums/viewthread/44916




Theme © iAndrew 2016 - Forum software by © MyBB