Welcome Guest, Not a member yet? Register   Sign In
loading template only once ?
#1

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
Reply
#2

Use a MY_Controller
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(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 ?
Reply
#4

(This post was last modified: 02-22-2017, 10:07 PM by ciadmin.)

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.
Reply
#5

(This post was last modified: 02-23-2017, 03:19 PM by eduard.pantazi.)

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.

Quote:I love web development. I love web design. I love photography.

Reply
#6

(This post was last modified: 03-01-2017, 05:52 AM by InsiteFX. Edit Reason: Fixed spelling error )

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
 * ------------------------------------------------------------------------
 */ 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB