I hate templates like Smarty or http://williamsconcepts.com/ci/codeigniter/libraries/template/
Using my own implementation of a Zend-like layout.
Add hook / application / hooks / Layout.php:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); /** * */ class Layout { function render() { global $OUT; $CI = & get_instance(); $output = $CI->output->get_output(); if (!isset($CI->layout)) { $CI->layout = "default"; } if ($CI->layout != false) { if (!preg_match('/(.+).php$/', $CI->layout)) { $CI->layout .= '.php'; } $requested = BASEPATH . '../application/layouts/' . $CI->layout; $default = BASEPATH . '../application/layouts/default.php'; if (file_exists($requested)) { $layout = $CI->load->file($requested, true); } else { $layout = $CI->load->file($default, true); } $view = str_replace("{content}", $output, $layout); $view = str_replace("{title}", $CI->title, $view); $scripts = ""; $styles = ""; $metas = ""; if (count($CI->meta) > 0) { // - $metas = implode("\n", $CI->meta); } if (count($CI->scripts) > 0) { // foreach ($CI->scripts as $script) { $scripts .= "<script type='text/javascript' src='" . base_url() . "js/" . $script . ".js'></script>"; } } if (count($CI->styles) > 0) { // foreach ($CI->styles as $style) { $styles .= "<link rel='stylesheet' type='text/css' href='" . base_url() . "css/" . $style . ".css' />"; } } if (count($CI->parts) > 0) { // foreach ($CI->parts as $name => $part) { $view = str_replace("{" . $name . "}", $part, $view); } } $view = str_replace("{metas}", $metas, $view); $view = str_replace("{scripts}", $scripts, $view); $view = str_replace("{styles}", $styles, $view); $view = preg_replace("/{.*?}/ims", "", $view); // } else { $view = $output; } $OUT->_display($view); } } ?>
Then enable hooks in /application/config/config.php:
$config['enable_hooks'] = TRUE;
After that add to /application/config/hooks.php:
$hook['display_override'][] = array('class' => 'Layout', 'function' => 'render', 'filename' => 'Layout.php', 'filepath' => 'hooks' );
Create folder / application / layouts /
Create file /application/layouts/default.php:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>{title}</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> {metas} {scripts} {styles} <link rel="icon" href="<?= base_url() ?>favicon.ico" type="image/x-icon" /> <link rel="shortcut icon" href="<?= base_url() ?>favicon.ico" type="image/x-icon" /> </head> <body> <div class="head"> {head} </div> <div class="content"> {content} </div> </body> </html>
Use it from the controller:
class main extends MY_Controller { function __construct() { parent::Controller(); $this->parts[head] = $this->load->view("frontend/global/header.php", null, true); $this->scripts = array("JQuery/jquery-1.4.2.min", "JQuery/form", "Core", "Frontend"); $this->styles = array("style"); $this->title = "Blah blah"; } function index() { $this->load->view('frontend/index.php'); } }