ramesh
ramesh

Reputation: 4082

Code igniter Themes' library.

I need 3 different templates for my Codeigniter application. I had read about Themes' library. But still I didn't get any idea about how to add a template to Codeignier ..

I got about how to involke template in Controller .

Please help

Upvotes: 1

Views: 3027

Answers (2)

Eduardo Reveles
Eduardo Reveles

Reputation: 2175

I'm using this template library, is really simple and works well for me.

application/libraries/Template.php

<?php
class Template {
    var $template_data = array();
    var $use_template  = '';

    /**
     * Set variable for using in the template
     */
    function set($name, $value)
    {
        $this->template_data[$name] = $value;
    }

    /**
     * Set template name
     */
    function set_template($name)
    {
        $this->use_template = $name;
    }

    /**
     * Load view
     */
    function load($view = '' , $view_data = array(), $template = '', $return = FALSE)
    {
        $this->CI =& get_instance();

        if (empty($template)) {
            $template = $this->CI->config->item('template_master');
        }

        if (!empty($this->use_template)) {
            $template = $this->use_template;
        }

        $this->set($this->CI->config->item('data_container'), $this->CI->load->view($view, array_merge($view_data, array ('template' => $this->template_data)), true));
        return $this->CI->load->view($this->CI->config->item('template_folder') . '/' . $template, $this->template_data, $return);
    }
}

application/config/template.php

<?php
$config['template_master']  = 'main';
$config['template_folder']  = 'templates';
$config['data_container']   = 'content';

application/views/templates/main.php

Header<br />
<?php echo $content; ?></br>
Footer

application/controllers/welcome.php

<?php
class Welcome extends CI_Controller
{
    public function index()
    {
        $this->load->config('template');
        $this->load->library('template');
        $this->template->load('welcome', array('view' => 'data'));
    }
}

I usually put the config/library files on autoload, and you can use anytime $this->template->set_template('other_template'); to use another one :)

Hope it helps.

Upvotes: 2

sikander
sikander

Reputation: 2286

I've used the following setup in a CodeIgniter project:

The different templates along with stylesheets and images are in the following folder:

/templates/1/header.php
/templates/1/footer.php
/templates/1/images/*
/templates/1/style/*
/templates/2/header.php
/templates/2/footer.php
/templates/2/images/*
/templates/2/style/*

In your Controllers determine which template you want to load and pass the path to that template as a variable ( templatepath in this case ) to your View files. Inside the view files you do the following:

<?php include($templatepath.'/header.php'); ?>

at the top and

<?php include($templatepath.'/footer.php'); ?>

at the bottom.

Upvotes: 1

Related Questions