CodeIgniter Forums
loading template only once ? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: loading template only once ? (/showthread.php?tid=67436)



loading template only once ? - Coool6 - 02-22-2017

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


RE: loading template only once ? - InsiteFX - 02-22-2017

Use a MY_Controller


RE: loading template only once ? - Coool6 - 02-22-2017

(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 ?


RE: loading template only once ? - ciadmin - 02-22-2017

Er, core/MY_Controller *is* how you use the CI_Controller for the purpose you describe.
https://www.codeigniter.com/user_guide/general/core_classes.html

See https://github.com/bcit-ci/codeigniter-website for an example.


RE: loading template only once ? - eduard.pantazi - 02-22-2017

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.


RE: loading template only once ? - InsiteFX - 02-23-2017

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
 * ------------------------------------------------------------------------
 */