CodeIgniter Forums
Default layout where I can load the content - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Default layout where I can load the content (/showthread.php?tid=7579)



Default layout where I can load the content - El Forum - 04-15-2008

[eluser]Marian[/eluser]
Hello people!
My name is Marian and I am new to CI.
I have a big big question:

How do I make a layout where I can load the content:
Example:
Code:
<html>
<head>
  <title>Example</title>
</head>
<body>
  -- top --
  -- left column --
  -- [b]content goes here[/b] --
  -- right column --
  -- footer --
</body>
</html>

So only the content will change from page to page.
Is there any way of doing this... a default layout with a default controller?


Default layout where I can load the content - El Forum - 04-15-2008

[eluser]Pascal Kriete[/eluser]
The first option would be to extend your controller, but what I would do is this:

Controller:
Code:
$data['content_view'] = 'someview';
$this->load->view('template', $data);

Template View:
Code:
<?php $this->load->view('header'); ?>
<?php $this->load->view($content_view); ?>
<?php $this->load->view('footer'); ?>



Default layout where I can load the content - El Forum - 04-15-2008

[eluser]Marian[/eluser]
Also... there are elements in the layout which get generated dymanically, for example categories. Where should I write the php for all those elements (left, right etc)?


Default layout where I can load the content - El Forum - 04-15-2008

[eluser]Pascal Kriete[/eluser]
It depends on how specific it is, but generally you could just add $this->load->view('left') to the template view, and add any data that view needs to the data array. Nested views are flattened before the variables are replaced so you don't need to pass them again.


Default layout where I can load the content - El Forum - 04-15-2008

[eluser]Marian[/eluser]
Do you or anyone else know a good tutorial about this?


Default layout where I can load the content - El Forum - 04-15-2008

[eluser]drewsmiff[/eluser]
Try This:

http://codeigniter.com/wiki/layout_library/

It's very Rails-esque.


Default layout where I can load the content - El Forum - 04-15-2008

[eluser]obobo[/eluser]
inparo ... can you give a quick example of how to extend the controller to include the base template?


Default layout where I can load the content - El Forum - 04-15-2008

[eluser]Pascal Kriete[/eluser]
The way I do it:
Code:
class BaseController extends Controller {

    /* Store footer for destruct */
    var $footer;
    
    function BaseController()
    {
        parent::Controller();

        // $this->load isn't available in the destructor => get the footer string
        $this->footer = $this->load->view('footer', NULL, TRUE);    
    }
    
    function __destruct()
    {
        echo $this->footer;
    }
}



Default layout where I can load the content - El Forum - 04-15-2008

[eluser]obobo[/eluser]
thanks inparo.