Welcome Guest, Not a member yet? Register   Sign In
Default layout where I can load the content
#1

[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?
#2

[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'); ?>
#3

[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)?
#4

[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.
#5

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

[eluser]drewsmiff[/eluser]
Try This:

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

It's very Rails-esque.
#7

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

[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;
    }
}
#9

[eluser]obobo[/eluser]
thanks inparo.




Theme © iAndrew 2016 - Forum software by © MyBB