CodeIgniter Forums

Full Version: loading template only once ?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello to the nice community !

I'm a beginner in CI (but not in PHP), i saw the CI logic is to load the template files in the controller in each method. 
At the end you load the same files all the time, and it makes heavy controller files for nothing. 

Code:
$this->load->view('templates/header', $data);
$this->load->view('news/create');
$this->load->view('templates/footer');

There is an easier solution to just load it once in your controller or even in your project (for example if you use the same template for the whole project ? 
Or maybe just to have one template.php file and loading the content inside ? 

If you can help me, you will be lovely ! 
Thanks a lot Smile
Use a MY_Controller
(02-22-2017, 11:10 AM)InsiteFX Wrote: [ -> ]Use a MY_Controller

Thanks ! 
But if i use Code Igniter, it's better to use the CI_controller.
So what could be the solution with the CI_controller ?
Er, core/MY_Controller *is* how you use the CI_Controller for the purpose you describe.
https://www.codeigniter.com/user_guide/g...asses.html

See https://github.com/bcit-ci/codeigniter-website for an example.
For me worked very well with new Controller, like:

Create a MY_Controller.php in application/core with this:
PHP Code:
<?php

class MY_Controller extends CI_Controller {

function 
__construct() {
parent::__construct();
}

function 
template($pagename$data null) {
$this->load->view('header'$data);
$this->load->view($pagename$data);
$this->load->view('footer'$data);
}



And this is it. To work with it, in your controllers use like this:

PHP Code:
....
class 
MyController extends MY_Controller {

function 
index() {
$data[""hello"] = "Hello passed trough the template() function with the variable $data in template function";
$this->template("pagename", $data);
}

}

... 

Now you have a template function that load the header and footer everytime and also load the page content and you can pass trough data to show on the header,page content and footer. Hope is usefull.


Have a nice day,
Eduard.
PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

/**
 * ------------------------------------------------------------------------
 * Editor   : PhpStorm 2016.3
 * Date     : 1/10/2017
 * Time     : 7:45 AM
 * Authors  : Raymond L King Sr.
 * ------------------------------------------------------------------------
 *
 * Class        MY_Controller
 *
 * @project     citest
 * @author      Raymond L King Sr.
 * @link        http://www.procoversfx.com
 * @copyright   Copyright (c) 2009 - 2017 Pro Covers FX, LLC.
 * @license     http://www.procoversfx.com/license
 * ------------------------------------------------------------------------
 */

/**
 * Class MY_Controller
 *
 * This file should be saved as:
 * ./application/core/MY_Controller.php
 *
 */
class MY_Controller extends CI_Controller {

    
/**
     * Class variables - public, private, protected and static.
     * --------------------------------------------------------------------
     */

    /**
     * @var  array
     */
    
protected $data = array();

    
/**
     * --------------------------------------------------------------------
     *  __construct
     * --------------------------------------------------------------------
     *
     * Class Constructor    PHP 5+
     *
     * @access    public
     */
    
public function __construct()
    {
        
parent::__construct();

        
/**
         * check to see if we want to use the CI profiler.
         * Requires the below in the ./application/config/config.php
         * //$config['profiler'] = TRUE;
         * $config['profiler'] = FALSE;
         */
        
$this->output->enable_profiler($this->config->item('profiler'));

        
log_message('debug''CI: MY_Controller class loaded');
    }

}    
// End of MY_Controller Class.

/**
 * Class Admin_Controller
 */
class Admin_Controller extends MY_Controller
{
    
/**
     * Class variables - public, private, protected and static.
     * --------------------------------------------------------------------
     */

    /**
     *  __construct ()
     * --------------------------------------------------------------------
     *
     * Class    Constructor
     */
    
public function __construct()
    {
        
parent::__construct();

    }


}    
// End of Admin_Controller Class.

/**
 * Class Public_Controller
 */
class Public_Controller extends MY_Controller
{
    
/**
     * Class variables - public, private, protected and static.
     * --------------------------------------------------------------------
     */

    /**
     *  __construct ()
     * --------------------------------------------------------------------
     *
     * Class    Constructor
     */
    
public function __construct()
    {
        
parent::__construct();

    }

}    
// End of Public_Controller Class.

/**
 * ------------------------------------------------------------------------
 * Filename: MY_Controller.php
 * Location: ./application/core/MY_Controller.php
 * ------------------------------------------------------------------------
 */