CodeIgniter Forums
PHP includes aren't working in CI - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: PHP includes aren't working in CI (/showthread.php?tid=8632)



PHP includes aren't working in CI - El Forum - 05-25-2008

[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 Smile


PHP includes aren't working in CI - El Forum - 05-25-2008

[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


PHP includes aren't working in CI - El Forum - 05-25-2008

[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.


PHP includes aren't working in CI - El Forum - 05-25-2008

[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-guide/libraries/output.html


PHP includes aren't working in CI - El Forum - 05-25-2008

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


PHP includes aren't working in CI - El Forum - 05-25-2008

[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');
$this->load->view('common_menu');
... // rest of site
$this->load->view('common_footer');
if you wanted.


PHP includes aren't working in CI - El Forum - 05-25-2008

[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
$date = '25/05/08';
// the 'something' page needs to use the $date variable, but gets an "Undefined Variable" notice
$this->load->view('includes/something');
How do I avoid that?


PHP includes aren't working in CI - El Forum - 05-25-2008

[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";
$this->load->view('includes/something', $vars);
This could (and probably should) also be passed from the controller.


PHP includes aren't working in CI - El Forum - 05-25-2008

[eluser]Michael Wales[/eluser]
controller/home.php
Code:
function index() {
  $vars['the_date'] = date('Y-m-d');
  $vars['view'] = 'home/photos';
  $this->load->view('view', $vars);
}

views/view.php
Code:
$this->load->view('_global/header');
$this->load->view($view);
$this->load->view('_global/sidebar');
$this->load->view('_global/footer');

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).


PHP includes aren't working in CI - El Forum - 05-25-2008

[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>
<head>
    <title>Wooshy -  <? start_block_marker('title') ?>Home<? end_block_marker() ?></title>
</head>

<body>
<h1>&lt;? start_block_marker('heading') ?&gt;Home&lt;? end_block_marker() ?&gt;</h1>
<ul id="navigation">
    <li>&lt;?=anchor('/','Home')?&gt;</li>
    <li>&lt;?=anchor('/about','About')?&gt;</li>
</ul>
&lt;? start_block_marker('content') ?&gt;
    <h2>Welcome</h2>
    <p>Filler</p>
&lt;? end_block_marker() ?&gt;
&lt;/body&gt;
&lt;/html&gt;

and in your 'about.php' view you have:
Code:
&lt;?php
extend('base.php');

startblock('title');
echo $title;
endblock();

startblock('heading');
echo $heading;
endblock();

startblock('content');
echo "<p>Some text</p>"
endblock();


end_extend();
?&gt;

You can simply load the 'about.php' view and it will automatically inherit the html from base.php.
Code:
$vars['title'] = "About us";
$vars['heading'] = "All about our nice team";
$this->load->view('about.php',$vars);

Nice or what Smile Hope this may help.