Part of the EllisLab Network
This thread is a discussion for the wiki article: layout library
   
 
layout library
Posted: 07 November 2008 09:56 PM   [ Ignore ]  
Summer Student
Avatar
Total Posts:  23
Joined  11-07-2008

I am having trouble with this library.

I have set it up correctly as I can see my layout but I cannot get any of my dynamic content showing. I have no error messages or anything, I just have blank space where I ashould have content. I have included my code below, where am I going wrong?

Layout_main.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
html >
<
head>
<
meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<
title>Design</title>
<
link href="<?php echo base_url();?>styles.css" rel="stylesheet" type="text/css" />
<!--
[if IE]>
<
link href="<?php echo base_url();?>iestyles.css" rel="stylesheet" type="text/css" />
<!
[endif]-->


</
head>

<
body>
<
div id="wrapper">
    <
div id="header">
    <
div id="flashheader">
    
        <
object type="application/x-shockwave-flash"
        
data="flash/logo1.swf"
        
width="780" height="159">
        <
param name="movie"
        
value="<?php echo base_url();?>flash/logo.swf" />
        <
img src="images/logo.gif"
        
width="780" height="159" alt="design" />
        </
object>

      </
div>        
        
    <
div id="nav">
         <
ul class="navigation">
                <
li><a href=""><strong>Home</strong></a></li>
                <
li><a href=""><strong>About</strong></a></li>
                <
li><a href=""><strong>content</strong></a></li>
                <
li><a href=""><strong>Contact</strong></a></li>
            </
ul>
    </
div>
    </
div>
        
        <
div id="main">
        
<?php $content_for_layout ?>
        
</div>
    
</
div>
</
body>
</
html>

views/home.php

<div id="content">
<
h1>Design</h1>
<
p>Welcome to Design</p>
</
div>

<
div id="projects">
<
h1>Our Projects</h1>
<
p class="project"><a href=""><img src="" alt="" /></a>

</
p>
</
div>

controller/home.php

<?php

class home extends Controller {

    
function home()
    
{
        parent
::Controller();    
    
}
    
    
function index()
    
{
        
        $this
->layout->view('home');
    
}


}
?>
 Signature 

Web Design

Profile
 
 
Posted: 07 November 2008 10:15 PM   [ Ignore ]   [ # 1 ]  
Summer Student
Avatar
Total Posts:  23
Joined  11-07-2008

I found the problem, I missed an echo lol.

 Signature 

Web Design

Profile
 
 
Posted: 10 April 2009 04:12 AM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  1
Joined  04-10-2009

As the library() loader method accepts an array of parameters, the code should be something like:
/application/libraries/Layout.php

class Layout
{
    
var $obj;
    var
$layout = 'default';
    
    function
Layout($params)
    
{
        $this
->obj =& get_instance();
        if(!empty(
$params['layout'])) {
            $this
->layout = $params['layout'];            
        
}
    }
...

and to load the library, in the controller:

$this->load->library('layout', array('layout' => 'layouts/default'));
Profile
 
 
Posted: 13 June 2009 01:54 PM   [ Ignore ]   [ # 3 ]  
Summer Student
Total Posts:  3
Joined  06-13-2009

If you want your layouts to live outside the “views” folder, you can modify the code as such:

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

class
Layout
{
    
    
var $obj;
    var
$layout;
    var
$app_layout_path = 'layouts/';
    var
$content_var = 'content';
    
private $_ci_view_path_holder = '';

    function
Layout($layout = "default")
    
{
        $this
->obj =& get_instance();
        
$this->_ci_view_path_holder = $this->obj->load->_ci_view_path;
        
$this->layout = $layout;
    
}

    
function setLayout($layout)
    
{
      $this
->layout = $layout;
    
}
    
    
function view($view, $data=null, $return=false)
    
{
        $loadedData
= array();

        
$loadedData[$this->content_var] = $this->obj->load->view($view,$data,true);
        
        if(
$return)
        
{
            $this
->obj->load->_ci_view_path = APPPATH . $this->app_layout_path;
            
$output = $this->obj->load->view($this->layout, $loadedData, true);
            
$this->obj->load->_ci_view_path = $this->_ci_view_path_holder;
            return
$output;
        
}
        
else
        
{
            $this
->obj->load->_ci_view_path = APPPATH . $this->app_layout_path;
            
$this->obj->load->view($this->layout, $loadedData, false);
            
$this->obj->load->_ci_view_path = $this->_ci_view_path_holder;
        
}
    }
}
?>
 Signature 

Bitter Web Veteran

Profile
 
 
   
 
 
‹‹ Xajax perfect setup      SimpleLoginSecure ››
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: 120255 Total Logged-in Users: 39
Total Topics: 126391 Total Anonymous Users: 2
Total Replies: 664690 Total Guests: 327
Total Posts: 791081    
Members ( View Memberlist )