Welcome Guest, Not a member yet? Register   Sign In
Creating a template in CodeIgniter
#1

[eluser]Unknown[/eluser]
Hello,

I'm new to CodeIgniter. Something work, but some things don't work.
I've created some views:
- header
- navigation
- mainheader
- maindata
- footer

On every page, 'header', 'navigation' and 'footer' are the same.
Only mainheader (page title) and maindata (page data) will change.

I want to create a template, so i don't need to load all of these views everytime

I've created a class with this function
Code:
Class Main extends Controller {

    function __construct() {
        
        parent::Controller();
    
    }
        
    function index() {
    
        $controller = $this->uri->segment(1);
        $function = $this->uri->segment(2);
    
        if( !$controller && !$function ) {
            $viewToLoad = 'vereniging';
        } else if( $controller && !$function ) {
            $viewToLoad = $controller;
        } else if( $controller && $function ) {
            $viewToLoad = $controller.'/'.$function;
        } else {}
        
    
        $data['title'] = 'Wielervereniging HSK Trias';
    
        $data['header'] = $this->load->view('header', $data, TRUE);
        $data['navigation'] = $this->load->view('navigatie' , $data, TRUE);
        $data['mainheader'] = $this->load->view('mainheader' , $data, TRUE);
        $data['maindata'] = $this->load->view($viewToLoad , $data, TRUE);
        $data['footer'] = $this->load->view('footer' , $data, TRUE);
    
        $output = $this->load->view('mainview', $data, TRUE);
        
        print $output;
        
        
    }
    
}

My idea was: load an URL like http://www.website.com/index/aController/aFunction/ and this function pass the controller name (aController) and function name (aFunction) to the variable $viewToLoad, load the views and everything looks fine in the webbrowser.

But, this is only working when the controller Main is always called from everywhere.

How can i do this?
#2

[eluser]tonanbarbarian[/eluser]
sounds like what you need is a layout library

try one of these
layout_library

Template Library

I have use the first one successfully in a couple of projects now.

Do not try to have a main controller that calls other controllers, it just does not work that way. Several people have tried with limited success I believe.
If you feel you MUST do it that way then build what you are trying to do as Libraries instead.
#3

[eluser]Unknown[/eluser]
Thank you, tonanbarbarian!
That is exactly what i want.




Theme © iAndrew 2016 - Forum software by © MyBB