Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter and layouts? [closed]

What CodeIgniter library can provide this funcionality??

http://media.railscasts.com/videos/008_content_for.mov

It seems so simple on rails but I just cant find a simple way to achieve this on codeigniter.. please help.. I hate having to insert my styles or javascript in the body of the document

like image 826
nacho10f Avatar asked Jun 02 '10 10:06

nacho10f


1 Answers

I hate templates like Smarty or http://williamsconcepts.com/ci/codeigniter/libraries/template/

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');
    }


}
like image 114
eazery Avatar answered Sep 20 '22 22:09

eazery