So so late post about : Using Zend-like Layout on Codeigniter

Just for my self-documentation : from this.

****

Using my own implementation of 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 your /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 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');
}
}

2 pemikiran pada “So so late post about : Using Zend-like Layout on Codeigniter

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s