Welcome Guest, Not a member yet? Register   Sign In
Repeating piece of code
#1

[eluser]lopetzi[/eluser]
Hello world,

I am new to CI, but i really like it.

I'm having one small problem:

I have a piece of code that i will have to repeat in almost every controller. Is there any way to put that code in a file and execute it... ?

home.php
Code:
class Home extends Controller {

    function Home()
    {
        parent::Controller();    
    }
    
    function index()
    {
        // model is autoloaded
        $general = $this->general->get_general();
        $template['description'] = $general->description;
        $template['keywords'] = $general->keywords;
        $template['price'] = $general->price;
        $user = 'Guest';
        $template['welcome'] = 'Welcome, ' . $user;
        // all the code above will repeat in every controller
        
        $template['title'] = 'Titlu';
        $template['heading'] = 'HOME';
        $template['left'] = $this->load->view('home','',true);
        $template['right'] = $this->load->view('newsform','',true);
    
        $this->load->view('main', $template);
        
        $this->output->enable_profiler(TRUE);
    }
}

I've tried to copy-paste that code into a library and then load the library, but i get an error...

How do you handle the code that repeats in your controllers ?

Thanks,
George


Messages In This Thread
Repeating piece of code - by El Forum - 07-15-2008, 03:01 PM
Repeating piece of code - by El Forum - 07-15-2008, 03:06 PM
Repeating piece of code - by El Forum - 07-15-2008, 04:10 PM
Repeating piece of code - by El Forum - 07-15-2008, 04:17 PM
Repeating piece of code - by El Forum - 07-16-2008, 11:35 AM



Theme © iAndrew 2016 - Forum software by © MyBB