Part of the EllisLab Network
This thread is a discussion for the wiki article: layout library
   
 
layout library
Posted: 07 November 2008 10:56 PM   [ Ignore ]  
Summer Student
Avatar
Total Posts:  28
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>
<
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 11:15 PM   [ Ignore ]   [ # 1 ]  
Summer Student
Avatar
Total Posts:  28
Joined  11-07-2008

I found the problem, I missed an echo lol.

 Signature 

Web Design

Profile
 
 
Posted: 10 April 2009 05: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 02: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$loadedDatatrue);
            
$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$loadedDatafalse);
            
$this->obj->load->_ci_view_path $this->_ci_view_path_holder;
        
}
    }
}
?> 
 Signature 

Bitter Web Veteran

Profile
 
 
Posted: 14 April 2010 02:19 AM   [ Ignore ]   [ # 4 ]  
Summer Student
Total Posts:  3
Joined  04-09-2010

The layout_main.php example given at http://codeigniter.com/wiki/layout_library/ provides a parameter for the title, specifically,

<title><?=$title_for_layout?></title>


I am doing the same, but the implementation is requiring the controller to set ‘title_for_layout’ in the data array to passed in to $this->layout->view().  Ideally, I want the title information to be in the view file, but this doesn’t seem possible.  Is there a workaround?

Profile
 
 
Posted: 23 July 2010 06:32 AM   [ Ignore ]   [ # 5 ]  
Summer Student
Total Posts:  6
Joined  07-07-2010

Hello,

See advanced Codeigniter Library here. and also see weakness of current layout libraries here.

Profile
 
 
Posted: 24 July 2010 11:10 AM   [ Ignore ]   [ # 6 ]  
Summer Student
Total Posts:  3
Joined  07-24-2010

Ive been looking for this solution for a long time, many thanks.

 Signature 

http://www.fatlossfactortips.com/

Profile
 
 
Posted: 18 October 2010 05:14 AM   [ Ignore ]   [ # 7 ]  
Summer Student
Total Posts:  1
Joined  10-18-2010
tahsin352 - 23 July 2010 10:32 AM

Hello,

See advanced Codeigniter Library here. and also see weakness of current layout libraries here.

Where can i find the advanced layout library ? Download from link above does not work anymore ...


Thank you.

Profile
 
 
Posted: 29 October 2010 01:49 AM   [ Ignore ]   [ # 8 ]  
Summer Student
Total Posts:  5
Joined  10-27-2010

hey am newbie can you tell me what does exactly means layout library how and where does it functions?

Profile
 
 
Posted: 12 November 2010 04:24 PM   [ Ignore ]   [ # 9 ]  
Summer Student
Total Posts:  1
Joined  03-11-2010

Hi to everybody,

I was facing some limitations with Layout library:
- Placeholder support (without putting nasty variables on CI_Loader)
- Selective javascript/css loading on layout, specified in the view

So I added some easy methods on the Layout.php library.

To add a new javascript or css to loaded IN the layout (ex. the <head> tag).. You can do anywhere

//do it in the controller, in the view, etc.
//single js file
$this->layout->load_js("application.js");
//many js files
$this->layout->load_js(array("jquery.js""application.js"));

//single css file
$this->layout->load_css("application.css");
//many css files
$this->layout->load_css(array("application.css""jqui.css")); 

And then add this to your layout

echo $this->layout->css();
echo 
$this->layout->js(); 

For doing placeholders (ex. setting the page title, or an h1 value)

//do it in the controller, in the view, etc.
$this->layout->placeholder("title""My Custom Page Title);
$this->layout->placeholder("message", "Piece of cake!"); 

And then in you layout

echo $this->layout->placeholder("title"); //displays "My Custom Page Title"
echo $this->layout->placeholder("message"); //displays "Piece of cake!" 

Here is my complete Layout.php class for everyone to use:

<?php

/**
 * Layout management library based on:
 * http://codeigniter.com/wiki/layout_library/
 *
 * Extended layout placeholders and javascript and css files inclussion.
 * Author: gbrunacci <gbrunacci *at siamsoft *dot com *dot ar>
 */
class Layout {
    
var $obj;
    var 
$layout;
    var 
$js;
    var 
$css;
    var 
$placeholder;

    function 
Layout($layout "layout")
    
{
        $this
->obj =& get_instance();
        
$this->layout $layout;
        
$this->js $this->css $this->placeholder = array();
    
}

    
function setLayout($layout)
    
{
      $this
->layout $layout;
    
}

    
function view($view$data=null$return=false)
    
{
        $loadedData 
= array();
        
$loadedData['content_for_layout'$this->obj->load->view($view,$data,true);

        if(
$return)
        
{
            $output 
$this->obj->load->view($this->layout$loadedDatatrue);
            return 
$output;
        
}
        
else
        
{
            $this
->obj->load->view($this->layout$loadedDatafalse);
        
}
    }

    
function load_js($file=null)
    
{
        
if(is_array($file)){
            
foreach($file as $f)
                
$this->js[] $f;
        
}else $this->js[] $file;
    
}

    
function js()
    
{
        $stream 
"";
        foreach(
$this->js as $js)
            
$stream .= '[removed][removed]' "\n";
        
        return 
$stream;
    
}

    
function load_css($file=null)
    
{
        
if(is_array($file)){
            
foreach($file as $f)
                
$this->css[] $f;
        
}else $this->css[] $file;
    
}

    
function css()
    
{
        $stream 
"";
        foreach(
$this->css as $css){
            $stream 
.= '<link href="'url_file($css) .'" rel="stylesheet" type="text/css" media="screen" />' "\n";
        
}

        
return $stream;
    
}

    
function placeholder($key$value=null){
        
if($value==null)
            return 
$this->placeholder[$key];
        else
            
$this->placeholder[$key] $value;        
    
}

Any comment (good and bug ones) are welcome
Thanks

Profile
 
 
Posted: 20 November 2010 06:09 AM   [ Ignore ]   [ # 10 ]  
Summer Student
Total Posts:  2
Joined  11-20-2010

look for these answers for so long. thanks for information

Profile
 
 
Posted: 23 November 2010 05:08 AM   [ Ignore ]   [ # 11 ]  
Summer Student
Total Posts:  3
Joined  11-23-2010

Well coding explanation. I think the optimization would yield too small a performance boost to justify the additional time to try and optimize it.I was thinking about a library that returns a different result for a different locale.

Profile
 
 
Posted: 06 April 2011 12:21 PM   [ Ignore ]   [ # 12 ]  
Summer Student
Total Posts:  1
Joined  04-04-2011

You might want to take a look at this View library.
http://dreamerslab.com/blog/en/codeigniter-view-library/
It supports layout, action view, partial and asset management.

Features


- Manage layouts, page title, metas, css and js with YAML file for cleaner and lighter controllers.
- Default configs can be overwritten in controller configs. So that you can split your css and js into smaller files for more flexible design.
- Combine and minify css and js files in production mode for faster page loading.

Example code

layout example

<?=doctype('xhtml1-trans')?>
  
<head>
    
<?$this->view->metas()?>
    <?$this
->view->title()?>
    <?$this
->view->asset('css')?>
    <?
=link_tagbase_url().'favicon.png''shortcut icon''image/ico')?>
  
</head>
  <
body>
    <
div id="wrap">
      <
div id="header">
        <
h1><?=$title?></h1>
        <
p>A real simple layout example</p>
      </
div>
      <
div id="content">
        <!-- 
yield this block for action view -->
        
<?=$yield?>
      
</div>
      <
div id="footer">
        
Your footer goes here
      
</div>
    </
div>
    <!-- 
js file at the bottom for faster loading page -->
    
<?$this->view->asset('js')?>
  
</body>
</
html

In the controller

// this will render the action view the layout set in the config file
  
$this->view->render(); 

Full documentation please take a look at this post
http://dreamerslab.com/blog/en/codeigniter-view-library/

or check out the source code on github
https://github.com/dreamerslab/ci.view

Profile
 
 
   
 
 
‹‹ Xajax perfect setup      SimpleLoginSecure ››