CodeIgniter and layouts?

What is the CodeIgniter library can provide this functionality?

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

It seems to be that simple on rails, but I just can't find a simple way to achieve this on codeigniter .. please help .. I really don't like pasting my styles or javascript into the body of the document

+6
layout codeigniter
source share
4 answers

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

I have used this Template Library in the past. From the documentation:

The template library written for the CodeIgniter PHP framework is a wrapper for implementing the CIs View implementation. A template is a response to the numerous questions of the CI community regarding how one could display several views for one controller and how to implement “views in views” in a standardized mode. In addition, the Template provides additional types of loading capabilities, the ability to use any template analyzer (for example, Smarty) and shortcuts to include CSS, JavaScript and other common elements in your final rendered HTML.

In particular, browse the library for additional utilities . This allows you to add something like this to your controller:

 $this->template->add_js('js/YourJavascriptFile.js'); 

or

 $this->template->add_js('alert("Hello!");', 'embed'); 

Echo $_scripts in the template (preferably in the <head> section) to use the JavaScript added through this Method.

Similarly, you can do the same with your CSS:

 $this->template->add_css('css/main.css'); $this->template->add_css('#logo {display: block}', 'embed', 'print'); 

.. and just echo $_styles in the <head> section.

Hope this helps.

+4
source share

Check out Twig - it supports "inheritance" (instead of the traditional include header, footer, etc.)

http://www.twig-project.org/

CodeIgniter Helper:

http://github.com/jamiepittock/codeigniter-twig or http://github.com/MaherSaif/codeigniter-twig

(also did not try, but the last one seems to be a fork with the latest commits)

+2
source share

I had the same problem finding a layout library in advance, so I wrote my own version, which also fits your needs (using the YAML file).

Features

  • Manage layouts, page titles, metas, css and js with a YAML file for cleaner and lighter controllers.
  • The default configurations can be overwritten in the controller configurations. So you can split your css and js into smaller files for a more flexible design.
  • Combine and reduce css and js files in production mode for faster page loading.

Using

  // in the controller $this->view->render(); 

This lib will get the layout, view, page title, css, js, meta from your YAML configurations

Documentation

download

  • Download source code from Github
0
source share

All Articles