Part of the EllisLab Network
   
3 of 9
3
Embedding Views within Views
Posted: 01 November 2006 08:47 PM   [ Ignore ]   [ # 31 ]  
Summer Student
Total Posts:  10
Joined  10-28-2006

alright, for whatever stupid reason it just started working…

controllers/main.php
    
function index()
    
{
        
//$template['body'] = "main/main";
        
$template['body'] = $this->load->view('main/main',null,true);
        
//$template['navigation'] = $this->load->view('global/navigation',null,true);
        
$this->load->view('global/layout',$template,'true');
    
}

views/global/layout.php

    <?php
                
if (isset($body)) {
                    
//$this->load->view($body);
                    
echo $body;
                    
//print_r($vars);
                
} else {
                    
echo "No Body Defined";
                
}  
            ?>

now all i have to do is make sure it works everywhere else…

Profile
 
 
Posted: 01 November 2006 08:50 PM   [ Ignore ]   [ # 32 ]  
Summer Student
Total Posts:  10
Joined  10-28-2006

Next question, is there a fast way to test if a view exists before loading it?  Right now my if/else statment is worthless because the var is always being set, except if its an error it gets populated with the error page :\

Profile
 
 
Posted: 02 November 2006 04:16 AM   [ Ignore ]   [ # 33 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  547
Joined  03-08-2006

I use this in one of my controllers:

$view = 'myview.php';
if(
file_exists('system/application/views/'.$file)){
  $layout[
'body'] = $this->load->view($view, NULL, True);
} else {
  $layout[
'body'] = '';
}

If you did this a lot throughout your app, you could create a model/library containing that code and return the view if it exists and an empty string if not.

 Signature 

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

Profile
 
 
Posted: 02 November 2006 10:51 AM   [ Ignore ]   [ # 34 ]  
Summer Student
Total Posts:  10
Joined  10-28-2006

Thanks!  THat should work for my purposes, i’ll probably go ahead and make it a method of my ‘general’ model/class (model for the moment, class whenever i get around to changing it…)

Yeah.  Be nice if that was a quick and dirty method of the loader class smile

Profile
 
 
Posted: 10 November 2006 05:18 AM   [ Ignore ]   [ # 35 ]  
Summer Student
Total Posts:  8
Joined  10-21-2006

I’m using another way to nest views in views and I wanted to submit it to you, CI gurus, as I’m not sure if it’s a good practice.

Better than a long and complicated explanation, here is some sample code:

First, you have to set up a post_controller hook.

The post-controller hook is the layout manager:

define('DEFAULT_LAYOUT', 'mainLayout');

function
layout()
{
    
global $CI, $OUT;
    
$layout = isset($CI->data->layout) ? $CI->data->layout : DEFAULT_LAYOUT;
    
$layout();
    
$OUT->set_output($CI->load->view($layout, $CI->data, TRUE));
}

function mainLayout()
{
    
global $CI;

    
/* Layout data */

    // window title
    
if (isset($CI->data->windowTitle)) {
        $CI
->data->windowTitle = ' - ' . $CI->data->windowTitle;
    
} else {
        $CI
->data->windowTitle = '';
    
}

    
/* Content data */

    // Page title
    
if (!isset($CI->data->breadcrumbs)) {
        $CI
->data->breadcrumbs = array();
    
}
    
if (isset($CI->data->pageTitle)) {
        $CI
->data->pageTitle = $CI->load->view('titleView', $CI->data, TRUE);
    
} else {
        $CI
->data->pageTitle = '';
    
}

    
// Content
    
if (isset($CI->data->content)) {
        $CI
->data->content = $CI->load->view($CI->data->content.'View', $CI->data, TRUE);
    
} else {
        $CI
->data->content = '';
    
}

}

The controller just fetches data:

class Home extends Controller
{
    
...
    
    function
contact()
    
{
        $this
->data->pageTitle = $this->data->windowTitle = 'contact';
        
$this->data->active = '/contact';
        
$this->data->content = 'contact';
    
}
    
    
...
}

Finally, views:

* the main one:

<html>
    <
head>
        <
title>mysite.com<?=$windowTitle?></title>
    </
head>

    <
body>
        <
div id="content">
        
<?=$content?>
        
</div>
    </
body>

</
html>

* the content view which contains an another embedded view

<div id="left">
<?=$pageTitle?>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas sed augue.</p>
</
div>

* the title view

<h1><?php
foreach($breadcrumbs as $url => $label) {
    
if ($label != $pageTitle) {
        
echo "<a title=\"$label\" href=\"$url\">$label</a> >&nbsp;";
    
}
}
echo $pageTitle
?>
</h1>;

Sorry for this long post but I wanted to be the more explicit I could.

The concept is to use the post-controller hook as a layout manager. This way, no need to repeat the same code for each controller managing views.

Comments and feedback would be appreciated.

Profile
 
 
Posted: 14 November 2006 06:44 PM   [ Ignore ]   [ # 36 ]  
Grad Student
Rank
Total Posts:  72
Joined  11-02-2006

Here my two pence worth for managing layouts I extend the Controller and then use it again and again

<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class
TemplateEngine extends Controller {
    
    private $template
;

    
/**
     * Constructor
     */
    
function TemplateEngine()
    
{
        parent
::Controller();    
                                    
        
// Page Footer
        
$this->template['footer'] = $this->load->view('templates/layouts/footerview', '', true);
    
}
    
    
/**
     * Builds the template using all the passed data
     */
    
function build_template($view = 'templates/default_view')
    
{
        $this
->load->view($view, $this->template);
    
}
    
    
function set_section($item_name, $item)
    
{
        $this
->template[$item_name] = $item;
    
}

}
?>

then from my controller

require_once('templateengine.php');

class
News extends TemplateEngine {

    
function News()
    
{
    parent
::TemplateEngine();
        
parent::set_section('sidebar', 'news/search');
    
}
    
    
/**
     *
     */
    
function index()
    
{
        
// Load Header
       
$header = $this->load->view('news/news_view', '', true);

        
// Last 10 news items
        
$this->load->model('news_model','NewsModel',true);
        
$data['rows']  = $this->NewsModel->get_last_ten_entries();
        
$contents = $this->load->view('news/news_view', $data, true);
        
        
parent::set_section('contents', $header);
        
parent::set_section('contents', $contents);
        
parent::build_template();
    
}
}

this works great for me would appreciate any feedback

 Signature 

Apache 2 PHP 5 Ubuntu Sellersrank.com [TextMate CodeIgniter Bundle]

Profile
 
 
Posted: 16 November 2006 02:55 PM   [ Ignore ]   [ # 37 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  310
Joined  08-16-2006

I came looking for this exact information. The examples provided by both Rick & Craig are very instructive—thanks!

A quick question. What’s the third parameter in the view function used for (as in Craig’s example)?

It’s not mentioned on the view page of the User Guide.

Profile
 
 
Posted: 16 November 2006 05:18 PM   [ Ignore ]   [ # 38 ]  
Research Assistant
RankRankRank
Total Posts:  407
Joined  02-14-2007

The third paramater is used to return the view, so you can assign it to a variable, rather than being tossed into the output class at that moment.  It’s key to Criag’s system, as then the variable containing the content of the view is able to be passed to the master view.  smile

Profile
 
 
Posted: 16 November 2006 05:49 PM   [ Ignore ]   [ # 39 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  310
Joined  08-16-2006

Great—thanks for the clarification.

I intended to use the syntax as shown, but it’s nice to understand what everything means.

Maybe this should be added to the User Guide (?)

Profile
 
 
Posted: 16 November 2006 07:51 PM   [ Ignore ]   [ # 40 ]  
Summer Student
Total Posts:  10
Joined  10-28-2006

I’m thinking maybe there should be some reference to that that third variable in the ‘view’ section of the docs.  seems like it’d cut down on some of the traffic smile

Profile
 
 
Posted: 16 November 2006 08:25 PM   [ Ignore ]   [ # 41 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  647
Joined  09-30-2006
Luci3n - 14 November 2006 06:44 PM

Here my two pence worth for managing layouts I extend the Controller and then use it again and again

this works great for me would appreciate any feedback

Perfectly valid. I use a baseclass very similar to yours to handle this.

 Signature 

sitesquad.net | < insert catchy tagline here />

Profile
 
 
Posted: 16 November 2006 09:45 PM   [ Ignore ]   [ # 42 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  310
Joined  08-16-2006

Incidentally, I just converted some ugly inline HTML to an embedded view (as per Craig’s example) and now it’s so much cleaner. Schweet!

Profile
 
 
Posted: 17 November 2006 03:41 AM   [ Ignore ]   [ # 43 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  547
Joined  03-08-2006

The information about the third parameter is in the usergudie, but it is tucked away in the Loader class page; but I do admit it would also benefit from being mentioned and/or including in the Views page grin

Glad you liked my method of loading views, Bacteria Man grin

 Signature 

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

Profile
 
 
Posted: 17 November 2006 10:55 AM   [ Ignore ]   [ # 44 ]  
Summer Student
Total Posts:  10
Joined  10-28-2006
Craig - 17 November 2006 03:41 AM

The information about the third parameter is in the usergudie, but it is tucked away in the Loader class page; but I do admit it would also benefit from being mentioned and/or including in the Views page grin

Glad you liked my method of loading views, Bacteria Man grin

I’m quite fond of it as well Craig smile  Makes a templating engine like Smarty or something seem like a horrible amount of overkill, especially for simpler sites that still need dynamic content.

Profile
 
 
Posted: 20 November 2006 11:21 PM   [ Ignore ]   [ # 45 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  147
Joined  09-24-2006
Luci3n - 14 November 2006 06:44 PM

Here my two pence worth for managing layouts I extend the Controller and then use it again and again

Thanks for the contribution Lucien, but a few questions:

1. 

parent::set_section('sidebar', 'news/search');

is this supposed to load a view? Because looking at your set_section() function this won’t happen because there is no load->view()
2.

$header = $this->load->view('news/news_view', '', true);

What does the news_view look like? Is this just a title i.e. ‘<h1>News</h1>’  or is it actually ‘<html><head><title></title></head>’ stuff ?

Would appreciate some explanation thanks! smile

Profile
 
 
   
3 of 9
3
 
‹‹ 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 819, on March 11, 2010 11:15 AM
Total Registered Members: 120561 Total Logged-in Users: 29
Total Topics: 126605 Total Anonymous Users: 1
Total Replies: 665583 Total Guests: 341
Total Posts: 792188    
Members ( View Memberlist )