Part of the EllisLab Network
   
1 of 9
1
Embedding Views within Views
Posted: 17 April 2006 10:23 AM   [ Ignore ]  
Administrator
Avatar
RankRankRankRankRank
Total Posts:  2512
Joined  12-21-2001

There have been a few discussions in these boards regarding embedding views within other views (what RoR apparently calls partials), if say, you want certain elements like headers, footers, etc. to be used automatically with your pages.  

There seems to be some confusion as to how this might be accomplished so I thought I would try to clear it up by describing one way to do this.  There are others, but this to me is the simplest.

CI has a very handy function called:  $this->load->vars()

This function lets you set variables, which will be available to any of your views.

Let’s say you have a view called container, with this in it:

<?php
$this
->load->view('header');
$this->load->view('sidebar');
$this->load->view('content');
$this->load->view('footer');
?>

Each of those views, in turn, contains variables or other dynamic structures.  For example, the header template might look like this:

<html>
<
head>
<
title><?=$heading?></title>
</
head>
<
body>

And the content template might expect a database result:

<div id="content">

<?php foreach($content->result() as $row): ?>

<div class="news">
    <
h3><?=$row->title?></h3>
    <
p><?=$row->information?></p>
</
div>

<?php endforeach; ?>

</div>

 

In your controller you can set the data for every single one of the views simultaneously just by doing this:

$data = array(
            
'heading' => 'My News Page',
            
'sidebar' => $this->db->get('sidebar'),
            
'content' => $this->db->get('content')
            );

$this->load->vars($data);

$this->load->view('container');
 Signature 
Profile
MSG
 
 
Posted: 19 April 2006 07:15 AM   [ Ignore ]   [ # 1 ]  
Summer Student
Total Posts:  9
Joined  04-18-2006

Excellent post Rick! Just what I was looking for.

Profile
 
 
Posted: 19 April 2006 08:10 AM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  9
Joined  04-18-2006

Oh, or maybe not :(

I just wish CI to have something like Django “inheritance” in templates… You can read about that here.

I’m not saying to copy from Django or even to use any templating engine, what I want is just some simple and clean way of loading chunks of code in the “master” template.

Let’s say I have “layout.php” view that shares common structure like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<
html xmlns="http://www.w3.org/1999/xhtml">
<
head>
    <
meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <
title><?=$title?></title>
    <
link rel="stylesheet" href="/css/screen.css" type="text/css" media="screen" />
</
head>
<
body>
    
<?=$this->load->view(hello_view)?>
</body>
</
html>

Now, you see there that I included hello_view, but I want to have only one master layout.php view and include there other views dynamically in a clean and easy way…

Hello controller to load hello_view, About controller to load about_view etc… What’s the best way to achieve this in the CI fashion?

Profile
 
 
Posted: 19 April 2006 10:23 AM   [ Ignore ]   [ # 3 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  545
Joined  03-08-2006

@sime:

Controller

$data['sidebar'] = $this->load->view('sidebar', '', true);  // load the sidebar view
$data['menu'] = $this->load->view('menu', '', true);  // load the menu view

$data['body'] = $this->load->view('hello/hello_view', $db, true);  // Load the Hello view with some DB stuff
$data['title'] = 'Page Title';

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

layout.php

...
<
title><?php echo $title ?></title>
...
<
body>
...
<?php echo $sidebar ?>
...
<?php echo $body ?>
...
<?php echo $menu ?>
...
</
body>
...
 Signature 

Twitter | Flickr | Last.fm | Del.icio.us

Profile
 
 
Posted: 20 April 2006 12:45 PM   [ Ignore ]   [ # 4 ]  
Summer Student
Total Posts:  8
Joined  04-20-2006

Hi!

After a couple of hours with CI this was one the first things that I looked up.

With a very small change to Ricks original example I got an easy way to do quick templates for my applications different page styles. Just add a page variable to your contoller and then get the template according to that variable. Like this:

controller:

function foo() {
  $data[
'page'] = 'foo';
  
$this->load->view('container', $data);
}

function bar() {
  $data[
'page'] = 'bar';
  
$this->load->view('container', $data);
}


container view:

<?php
$this
->load->view('header');
$this->load->view('content_'. $page);
$this->load->view('footer');
?>

And then you would have content_foo.php and content_bar.php for these views.

Maybe there is an easier way to do this, but this was my €0.02 :)

Profile
 
 
Posted: 20 April 2006 01:41 PM   [ Ignore ]   [ # 5 ]  
Grad Student
Avatar
Rank
Total Posts:  88
Joined  03-12-2006

That’s neat! I will save my time and code will be more clean. Thanks

 Signature 


Jan Onesork
Czech SEO consultant

Profile
 
 
Posted: 24 April 2006 06:39 PM   [ Ignore ]   [ # 6 ]  
Summer Student
Total Posts:  11
Joined  04-17-2006

I think I am missing something here, this method was working great for me until I starting working with the Database. I have a table with the name customers. Here is the code I am using.

Controler

$data = array(
    
'heading' => 'Customers',
'page' => 'listcust',
'content' => $this->db->get('customers')
);

$this->load->vars($data);
$this->load->view('layout/layout');

In the embeded content view I have the following code.

<?php foreach($content->results() as $row): ?>

    
<p><?php echo $row->lastname; ?></p>

<?php endforeach; ?>

I have been using this code and it worked fine until I added the line to get the database table. Though I am inseting data in another controler and it is working fine. So I know the database is working. I have also verified that there is records to be returned. I know I am simply typing something wrong. Any help anyone can provide would be great.

Profile
 
 
Posted: 24 April 2006 06:48 PM   [ Ignore ]   [ # 7 ]  
Administrator
Avatar
RankRankRankRankRank
Total Posts:  2512
Joined  12-21-2001

Shouldn’t “result” be singular?

<?php foreach($content->result() as $row): ?>
 Signature 
Profile
MSG
 
 
Posted: 24 April 2006 06:53 PM   [ Ignore ]   [ # 8 ]  
Summer Student
Total Posts:  11
Joined  04-17-2006
Rick Ellis - 24 April 2006 06:48 PM

Shouldn’t “result” be singular?

<?php foreach($content->result() as $row): ?>

Thank you Rick! You are awsome. That created another question though. I copied your code example from up top. Is your code pulling something different or is it a typo.

Profile
 
 
Posted: 24 April 2006 09:51 PM   [ Ignore ]   [ # 9 ]  
Administrator
Avatar
RankRankRankRankRank
Total Posts:  2512
Joined  12-21-2001

Oops!  grin

 Signature 
Profile
MSG
 
 
Posted: 26 April 2006 07:16 PM   [ Ignore ]   [ # 10 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  130
Joined  04-14-2006

Craig’s method above is a great one: it works well, and it keeps semantic chunks of html together (and makes it easy to manage—with seperate header/footer views, if you add a <div> to the header, you have to be sure to add the corresponding </div> to the footer).

 Signature 

CIForge.com - CodeIgniter Community Source Hosting

Profile
 
 
Posted: 30 April 2006 01:07 AM   [ Ignore ]   [ # 11 ]  
Summer Student
Total Posts:  4
Joined  04-30-2006

I was looking exactly for this kind of post and answers.
Thanks all, and to Craig particularly.

Profile
 
 
Posted: 07 May 2006 10:06 AM   [ Ignore ]   [ # 12 ]  
Summer Student
Total Posts:  8
Joined  03-20-2006

You can make it even more dynamic by using an array containing the templates you want to use. Take a look at these code snippets:

Controller

<?php
$data[
'templates'] = array("header", "content", "footer");
$this->load->vars($data);
$this->load->view('container', $data);
?>

View: container

<?php
foreach ($templates as $tpl) {
$this
->load->view($tpl);
}
?>

oh btw: Code Igniter is great grin

Profile
 
 
Posted: 24 May 2006 05:43 PM   [ Ignore ]   [ # 13 ]  
Summer Student
Total Posts:  1
Joined  05-24-2006

That was step 1 of getting dug in here.  Thanks…

Profile
 
 
Posted: 09 June 2006 11:35 AM   [ Ignore ]   [ # 14 ]  
Summer Student
Total Posts:  26
Joined  04-16-2006

BTW:

Is it possible to load models within the controller?

$this->load->model(“model”);

Profile
 
 
Posted: 09 June 2006 11:42 AM   [ Ignore ]   [ # 15 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  886
Joined  03-06-2006
codix - 09 June 2006 11:35 AM

BTW:

Is it possible to load models within the controller?

$this->load->model(“model”);

Yes. See:
Models
in the User Guide.

 Signature 

Corozal, Belize | Linux.bz | Using Kubuntu Linux 7.10 | CodeIgniter 1.5.3

Profile
 
 
   
1 of 9
1
 
‹‹ Problem with "orderby"      Session Trouble ››
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 721, on January 06, 2010 09:38 AM
Total Registered Members: 114967 Total Logged-in Users: 50
Total Topics: 122423 Total Anonymous Users: 2
Total Replies: 647238 Total Guests: 483
Total Posts: 769661    
Members ( View Memberlist )