Welcome Guest, Not a member yet? Register   Sign In
No need for templating libraries
#1

[eluser]Benjamin Midget[/eluser]
This is a walkthrough of how to do proper templating with CodeIgniter without using any extra libraries.

First, create your template:
Code:
<html>
<head>
  <title><?=$title?></title>
</head>
<body>
   <div class="header">
  Header Text here
</div>
   <div class="content">
      &lt;?=$content?&gt;
    </div>
  <div class="footer">
    Footer Text here
  </div>
&lt;/body&gt;
&lt;/html&gt;

Next, extend Controller:
Code:
class MY_Controller extends Controller{

protected $template = 'my/template/path';
protected $views = array();
protected $title = 'This is my site';

public function __construct()
{
   parent::Controller();
}

// you have to use this method now to load views as variables to the template.
protected function addview($path, $data, $name='content')
{
  $this->views[$name] = $this->load->view($path, $data, TRUE);
}

// this function does all the magic
public function _output($output)
{
  $data = $this->views;
  // this simply gives the template the proper title
  $data['title'] = $this->title;

  echo $this->load->view($this->template, $this->views, TRUE);
}


}

Finally, create your controller:
Code:
class Thispage extends Controller
{
public function __construct()
{
  parent::__construct();
  $this->title = 'This is the Thispage section of the site';
}

public function index()
{
   $data = array(); // get data from a model here
   $this->addview('thisview', $data);
}
}

That's it. You if you have multiple parts to the template, simply use something like:

Code:
$this->addview('some/view', $data, 'leftcolumn');
$this->addview('another/view', $data, 'rightcolumn');

I've also done this another layer deep by having multiple templates, but in order to do that you have to run an include at the top of your controller class.

If anyone wants to know how to do that, let me know.


Messages In This Thread
No need for templating libraries - by El Forum - 01-14-2009, 06:17 PM
No need for templating libraries - by El Forum - 01-14-2009, 06:37 PM
No need for templating libraries - by El Forum - 01-14-2009, 06:44 PM
No need for templating libraries - by El Forum - 01-14-2009, 06:46 PM



Theme © iAndrew 2016 - Forum software by © MyBB