Welcome Guest, Not a member yet? Register   Sign In
Login
#2

[eluser]CroNiX[/eluser]
What I do is create a Template library that gets autoloaded. I won't write the whole thing out, but you can simply do things like:

Code:
class Template {
  private $CI;

  function __construct()
  {
    $this->CI =& get_instance();  //bring CI into the library
  }

  function make_page($title = '', $content = '')
  {
    //First load the header and menu, pass page title to header
    $data['title'] = $title;  //pass the page title to the header
    $header = $this->CI->load->view('header', $data, TRUE);

    $menu = $this->CI->load->view('menu', NULL, TRUE);

    //footer
    $footer = $this->CI->load->view('footer', NULL, TRUE);

    //set final output (uses CI buffering, allows for caching), just pass $content through
    $this->CI->output->set_output($header . $menu . $content . $footer);
  }
}

Then in your controllers, all you have to do is pass the template the main view you are using for the content and it will add the header, menu and footer every time. you can also create various methods to display different types of pages. This keeps your controllers clean and DRY. If you have a login system, for instance, where it displays the users info and some links if they are logged in and shows the login form if they aren't, the library can do all of that stuff for you so you don't have to pollute your controllers, and then there is only one place to update it if it needs updating.

Controller:

Code:
//...blah, do some work, manipulate some data...

//now only load the view for this content and send to the template
$content = $this->load->view('content', $some_data, TRUE);
$this->template->make_page('my page title', $content);

This is obviously a very simple example, which can be infinitely more complex and do a whole lot more.


Messages In This Thread
Login - by El Forum - 07-11-2012, 09:58 AM
Login - by El Forum - 07-11-2012, 10:24 AM
Login - by El Forum - 07-11-2012, 11:16 AM
Login - by El Forum - 07-11-2012, 11:41 AM
Login - by El Forum - 07-11-2012, 11:55 AM
Login - by El Forum - 07-11-2012, 12:08 PM
Login - by El Forum - 07-11-2012, 12:25 PM
Login - by El Forum - 07-11-2012, 12:33 PM
Login - by El Forum - 07-11-2012, 12:39 PM
Login - by El Forum - 07-11-2012, 01:42 PM
Login - by El Forum - 07-11-2012, 01:46 PM
Login - by El Forum - 07-12-2012, 04:19 AM
Login - by El Forum - 07-12-2012, 10:52 AM
Login - by El Forum - 07-12-2012, 12:05 PM
Login - by El Forum - 07-12-2012, 12:12 PM
Login - by El Forum - 07-12-2012, 12:31 PM



Theme © iAndrew 2016 - Forum software by © MyBB