Welcome Guest, Not a member yet? Register   Sign In
Template system + class / method based views
#1

[eluser]Clifford James[/eluser]
Hi,

I've wrote a little library (hook) to auto load a template file + view without writing $this->load->view('index'); in every method of your controller.

/application/config/template.php
1) AUTOLOAD this config in /application/config/autoload.php
Code:
<?php

/*
|--------------------------------------------------------------------------
| Default Template
|--------------------------------------------------------------------------
|
| This determines which set of view files should be used. Make sure
| there is an available template if you intend to use something other
| than the default template.
|
*/
$config['template']    = "default";

/application/libraries/Template.php
Code:
<?php

class Template {

    var $CI;
    
    function __construct() {
        $this->CI =& get_instance();
    }

    function set() {
        if (!isset($this->CI->_template)) {
            $this->CI->_template = $this->CI->config->item('template');
        }
        
        if (!isset($this->CI->_view)) {
            $this->CI->_view = $this->CI->router->class.'/'.$this->CI->router->method;
        }
        
        if ($this->CI->_template) {
            $this->CI->data['_view'] = $this->CI->_template.'/'.$this->CI->_view;
            $this->CI->load->view($this->CI->_template.'/template', $this->CI->data);
        } elseif ($this->CI->_view) {
            $this->CI->load->view($this->CI->_view, $this->CI->data);
        }
    }
}

/application/config/hooks.php
1) ENABLE hooks in /application/config/config.php
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
| -------------------------------------------------------------------------
| Hooks
| -------------------------------------------------------------------------
| This file lets you define "hooks" to extend CI without hacking the core
| files.  Please see the user guide for info:
|
|    http://ellislab.com/codeigniter/user-guide/general/hooks.html
|
*/

$hook['post_controller'][] = array(
    'class' => 'Template',
    'function' => 'set',
    'filename' => 'Template.php',
    'filepath' => 'libraries',
    'params' => array()
);

/* End of file hooks.php */
/* Location: ./system/application/config/hooks.php */

Data for views should put in:
Code:
$this->data['persons'] = $persons;

Put this line in your template file:
1) See below for the folder structure
Code:
<? $this->load->view($_view) ?>

Example template.php
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
&lt;html &gt;
&lt;head&gt;
    &lt;title&gt;Template&lt;/title&gt;
    &lt;meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /&gt;
    &lt;link rel="stylesheet" type="text/css" href="/assets/stylesheets/default.css" media="all" /&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;? $this->load->view($_view) ?&gt;
&lt;/body&gt;
&lt;/html&gt;

Your folder structure for the views in default setup:
/application/views/default/ <- template folder
/application/views/default/template.php <- the template
/application/views/default/home/ <- the controller folder containing the controller views by method
/application/views/default/home/index.php <- the method view

URLS:
/ -> loads /default/home/index.php <- depending on your routes
/home -> loads /default/home/index.php
/home/start -> loads /default/home/start.php

You do not need to use $this->load->view() anymore. When you want to switch from template you can use this in your controller:
Code:
$this->_template = 'mobile';
/* no template at all */
$this->_template = FALSE;
/* if you want to set a different view then the method or controller */
$this->_view = 'class/method'; /* loads /default/class/method.php */
/* or */
$this->_view = 'person/edit'; /* loads /default/person/edit.php */
/* no view */
$this->_view = FALSE;
/* no template and view (blank page) */
$this->_template = $this->_view = FALSE;

Now my contact controller looks like this:
Code:
class Contact extends Controller {

    function __construct() {
        parent::Controller();
        
        $this->load->model('contact_model');
    }
    
    function index() {
        $this->data['contacts'] = $this->contact_model->get();
    }
    
    function add() {
        $this->_view = 'contact/edit';
    }

    function edit($id = NULL) {
        if ($id) {
            $this->data['contact'] = $this->contact_model->get_by_id($id);
        } else {
            redirect('contact/add');
        }
    }
}

index() loads /default/contact/index.php
add() loads /default/contact/edit.php
edit() loads /default/contact/edit.php

If you found anything missing please contact me and I'll update this post. I'm still sleepdrunk Tongue




Theme © iAndrew 2016 - Forum software by © MyBB