Welcome Guest, Not a member yet? Register   Sign In
Loading Header and Footer in codeigniter
#8

[eluser]Unknown[/eluser]
[quote author="PeterGreffen" date="1289494726"]Here is how I do that:

1. The template library

Create a template.php in your system/applications/libraries/ folder.

template.php =

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class template {
        var $template_data = array();
        
        function set($name, $value)
        {
            $this->template_data[$name] = $value;
        }
    
        function load($template = '', $view = '' , $view_data = array(), $return = FALSE)
        {              
            $this->CI =& get_instance();
            $this->set('contents', $this->CI->load->view($view, $view_data, TRUE));            
            return $this->CI->load->view($template, $this->template_data, $return);
        }
}

/* End of file Template.php */
/* Location: ./system/application/libraries/template.php */


2. Load the library in you controller(s):

Code:
$this->load->library('template');

or you can autoload it in your config/autoload.php:

Code:
$autoload['libraries'] = array('template');

3. Create a new template.php file in your system/applications/views folder. The contents of that template.php can be something like this:

Code:
<html>
<head>
   <!-- add your javascript includes here -->
   <!-- add your css includes here -->
   <!-- etc -->
</head>

<body>

<div id='header'>
    &lt;!-- include your header view here --&gt;
    &lt;?php $this->load->view('header'); ?&gt;
</div>

<div id='whateverworks'>
   &lt;?= $contents ?&gt;
</div>

<div id='footer'>
    &lt;!-- include your footer view here --&gt;
    &lt;?php $this->load->view('footer'); ?&gt;
</div>

&lt;/body&gt;

&lt;/html&gt;

Everytime you load a view, you will load this template view and and another where the $content is. Here's how, in your Controller, instead of using:

Code:
$this->load->view('blog/show_post's);

you can now use

Code:
$this->template->load('template', 'blog/show_posts');
or
Code:
$this->template->load('template', 'blog/show_posts', $data); // :)

That will load the template view file with the view (blog/show_posts) included.

NOTE: make sure the variable name (here that is '$contents' in your template view, is the same as the one you use in the load() function in your template library in:

Code:
$this->set('contents', $this->CI->load->view($view, $view_data, TRUE));

Hope that helps!
P.[/quote]

I'm trying the same code with a few changes... but it give me an error..
I follow all the first 3 steps, and of course i use 'contents' in both template.php in libraries and views..
it says

Severity: Notice

Message: Undefined variable: contents

Filename: views/template.php

Line Number: 16

one thing that i change is the
$this->template->load('template', 'blog/show_posts'); part
i change it with
$this->template->load('template','failure');

i put my failure.php in application/views and only consist on word "failure", but it don't work.. Did i miss something?


Messages In This Thread
Loading Header and Footer in codeigniter - by El Forum - 11-10-2010, 11:04 PM
Loading Header and Footer in codeigniter - by El Forum - 11-11-2010, 01:49 AM
Loading Header and Footer in codeigniter - by El Forum - 11-11-2010, 04:58 AM
Loading Header and Footer in codeigniter - by El Forum - 01-26-2011, 05:46 AM
Loading Header and Footer in codeigniter - by El Forum - 01-26-2011, 08:20 AM
Loading Header and Footer in codeigniter - by El Forum - 01-27-2011, 10:44 AM
Loading Header and Footer in codeigniter - by El Forum - 01-27-2011, 01:24 PM
Loading Header and Footer in codeigniter - by El Forum - 02-07-2011, 09:59 AM



Theme © iAndrew 2016 - Forum software by © MyBB