multiple views

#1
[eluser]home158[/eluser]
Hi everyone,
When I load multiple views,like this.
Code:
$this->load->view('head');
$this->load->view('foot');
It always show the last view "foot", not "head" and "foot".
Do I forgot somethings?
How could use multiple views in my system?

Thanks.

#2
[eluser]Michael Wales[/eluser]
I assume you are placing that code within your controller. Load a single view from your controller and then load your others views from that main view. I usually do something like this:

controller
Code:
function index() {
  $this->data->partial = 'home/index';
  $this->load->view('layout', $this->data);
}

views/layout.php
Code:
$this->layout->view('header');
$this->layout->view($partial);
$this->layout->view('footer');

#3
[eluser]mglinski[/eluser]
Multiple views inside controllers works just fine. Make sure there are no php errors in footer view, as if there are errors in any view only the view with errors will be shown. Check the source output for any hidden errors.
-Matt

#4
[eluser]Jesse Schutt[/eluser]
Michael,

Are you saying that it is ok to load additional views from within a view? I'm just trying to get my head around "embedding" (Can you hear the EE-speak?) things like my footer, header, and nav views so one change will ripple through the site...

Thanks!

Jesse

#5
[eluser]John_Betong[/eluser]
 
I prefer to use the Loader Class in the Controllers. The CodeIgniter Help file has details.
 
 
controller/home.php
Code:
...
  function index{$data=array()) {

    // ESSENTIAL to setup variables here which are passed to the sub-sections.
    $data['title'] = 'Johns-Jokes.com';

    // My convention: All parts of a web-page are prefixed with '_'
    $data['doctype'] = $this->load->view('_doctype',   $data, TRUE); // TRUE ensures item is loaded into memory
    $data['header']  = $this->load->view('_header',    $data, TRUE);
    $data['content'] = $this->load->view('_homepage',  $data, TRUE);
    $data['footer']  = $this->load->view('_footer',    $data, TRUE);

    $output = $this->load->view('home', $data, TRUE);
    echo $output;

}//endfunc index
  ...
  ...
 
 
view/home.php
Code:
<?= $doctype ?>
<head>
<?= $header ?>
  <style type="text/css">
    div {border:dotted solid 0px #f00} /* for debugging */
  </style>
</head>

<body>
  <div id='container'>&lt;?php /* width=856, */ ?&gt;
    &lt;?= $title ?&gt;
    
    <div  id='box_center'>
      &lt;?= $content ?&gt;
    </div>  
    
    <div id='footer'>
      &lt;?=$footer?&gt;
    </div>  
    
  </div>&lt;?php /* container */ ?&gt;
&lt;/body&gt;
&lt;/html&gt;
&nbsp;
This technique maybe long winded but I find it easy to go read the code, debug and to modify the view files to ensure they have no Html validation errors.
&nbsp;
&nbsp;

#6
[eluser]Jesse Schutt[/eluser]
Hey!

Thanks for writing this out. This makes a lot of sense!

I probably should know this, but why do you have some of the comments wrapped in php tags? Is it so they don't render to the browser?

Again, thanks for helping with this!

Jesse

#7
[eluser]John_Betong[/eluser]
&nbsp;
I am pleased that the code was useful and easy to understand.
&nbsp;
Yes the comments wrapped in the PHP tags can only be seen in development. Just a little less code to be displayed in the browser to help bandwidth.
&nbsp;
&nbsp;
&nbsp;


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.