PHP includes aren't working in CI |
[eluser]KeyStroke[/eluser]
Hi, I'm basically trying to include some common code in my View files (like the navigation, footer...etc), but every time I include them, I get an error saying that PHP couldn't open the file, even though it's actually there. If I do this out of CodeIgniter, it works just fine. Is there anything I should change in CI before I could use includes or something? I'm including files from the root directory of my site by the way. Appreciate your help ![]()
[eluser]Jamie Rumbelow[/eluser]
Don't. CodeIgniter hates sending out headers before the output class is processed. If you need to have some information, turn it into a library or a model. Thanks, Jamie
[eluser]KeyStroke[/eluser]
How does that work? I'd rather not have to include code in all of my classes and their methods again. I'd like to control the includes from the view files.
[eluser]Jamie Rumbelow[/eluser]
The whole system builds a string of all the things in your views, and sends that to the browser. You can manually override that with the $this->output->set_output() method. Do something like this as a hook maybe: Code: $this->output->set_output(file_get_contents("include.inc") . $this->output->get_output()); Read more about the output class here: http://ellislab.com/codeigniter/user-gui...utput.html
[eluser]KeyStroke[/eluser]
So, if I want to include a navigation somewhere in all my views, how do I do that through the controller without modifying all of my application's methods?
[eluser]Derek Allard[/eluser]
Another thing to consider... you can put views in views. At the top of your content view, you can include Code: $this->load->view('common_header');
[eluser]KeyStroke[/eluser]
Thanks Derek. This is just how I was hoping I could include components into my views. However, it appreas that the code of the views I include can't see the variables of the the view they're being added to. For example: Code: <?php
[eluser]Derek Allard[/eluser]
variables would still need to be created and passed to the views in the normal way. Code: $vars['the_date'] = "25/05/08";
[eluser]Michael Wales[/eluser]
controller/home.php Code: function index() { views/view.php Code: $this->load->view('_global/header'); All of those views (header, sidebar, footer, the view passed via a var (home/photos) will have access to the entire array of variables (they would be able to echo the date, perfectly fine).
[eluser]Unknown[/eluser]
I had a similar problem and a nice person in the IRC channel pointed me at the Django-like template inheritance helper (found here). Say if you have a view called 'base.php' with: Code: <html> and in your 'about.php' view you have: Code: <?php You can simply load the 'about.php' view and it will automatically inherit the html from base.php. Code: $vars['title'] = "About us"; Nice or what ![]() |
Welcome Guest, Not a member yet? Register Sign In |