Welcome Guest, Not a member yet? Register   Sign In
Problem loading external view
#1

[eluser]xzela[/eluser]
Hi all,

I'm having an issue loading a view from within a view. I continue to get a "An Error Was Encountered" error message when viewing any of the pages.
Here is the source code to the view I'm trying to use:
main_view.php
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
&lt;html &gt;
&lt;head&gt;
    &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
    &lt;link rel="icon" href="&lt;?php echo base_url();?&gt;favicon.ico" type="image/x-icon" /&gt;
    &lt;link rel="shortcut icon" href="&lt;?php echo base_url();?&gt;favicon.ico" type="image/x-icon" /&gt;    
    &lt;?php echo snappy_style('styles.css'); //autoloaded ?&gt;    
    &lt;title&gt;Mango - Main&lt;/title&gt;    
&lt;/head&gt;
&lt;body&gt;
&lt;?php
    $this->load->view('_global\header');
    $this->load->view('_global\menu');
?&gt;
<h1>Welcome to Mango</h1>
<p>Mango is Awesome</p>
&lt;?php
    $this->load->view('_global\footer');
?&gt;
&lt;/body&gt;
&lt;/html&gt;

header.php
Code:
&lt;?php
?&gt;
&lt;!-- START HEADER_VIEW --&gt;
<div id="header">
        <h1>Mango &copy; version 1.428</h1>
        <div style="float: right; height: 25px;">
            Welcome &lt;?php echo $user_data['user_name']; ?&gt; - &lt;?php echo $user_data['ip_address']; ?&gt;
        </div>        
</div>
&lt;!-- END HEADER_VIEW --&gt;
&lt;?php
/* End of file header_view.php */
/* Location: ./system/application/views/header_view.php */
?&gt;
When I load the view within the browser I get the "An Error Was Encountered" and the page stops rendering.

Quote:An Error Was Encountered
Unable to load the requested file: _global\header.php
If i comment out the external views, everything seems to be working. And if i just view the header view by itself, it renders properly. Am I loading these view incorrectly? Is there a better (best practices) way to this?

Let me know what you guys think.

Thanks
#2

[eluser]Derek Allard[/eluser]
Is the header in a folder called "_global"? Based on your code comment
Quote:/* Location: ./system/application/views/header_view.php */
It doesn't look like it. Try creating a _global folder and copying header and footer into there and see what happens. Views can be nested into subfolders if you wish, and I use this technique myself often for organizational purposes.
#3

[eluser]xzela[/eluser]
Hi,

Actually that comment in the code is incorrect. I will need to update that to reflect the most recent changes. The location of the header_view is actually at:
Code:
./system/application/views/_global/header_view.php

main_view is located at:
Code:
./system/application/views/main_view.php
Is there any way to get a more detail error message? Any way to get debug information when a view doesn't load correctly?

Let me know what you guys think,

Thanks again for the help
#4

[eluser]m4rw3r[/eluser]
ummm, header != header_view.
You should call it header.php
#5

[eluser]xzela[/eluser]
Actually, I have an interesting update regarding this issue.
If I change the location of the header.php file from:

Code:
./system/application/views/_global/
to:
Code:
./system/application/views/
and update the main_view.php file to reflect the changes:
Code:
&lt;?php
...
    $this->load->view('header');
...
?&gt;
it actually works!

In addition to this, changing the $this->load->view from:
Code:
$this->load->view('_global/header');
to:
Code:
include '_global/header.php';

will also work.

So my question now is: Why wont the $this->load->view('_global/header') load when it's within a sub-folder in this particular case? Am I not specifying a top level directory or something?

Let me know what you guys think,

Thanks for the help so far.
#6

[eluser]Derek Allard[/eluser]
oh... wait a sec. change "_global" to "global". What happens?
#7

[eluser]xzela[/eluser]
Same deal either way.

I renamed the '_global' directory to 'global' and the view still did not load. I even created a new directory called 'foo' and copied the header.php file to there and I got the same results.

I have to clarify, is there another way to load external php files within a view? Perhaps I'm miss using the '$this->load->view()' feature and this is causing the real problem.

Thanks for the help, much appreciated.
#8

[eluser]Derek Allard[/eluser]
hrm... thought it might be something else. Well, I'm totally dumbfounded. you should be able to use view within views no problem. If the original code you posted is accurate (you said you'd been moving some stuff around) and if "header.php" is actually in application/views/_global/header.php... then it makes no sense to me. I've just tested this (again) and it works for me. Could you try an even more minimal example.
#9

[eluser]xzela[/eluser]
Well, I think we found the issue, and I can't believe how lame this mistake was:

I change the code from this:
Code:
$this->load->view('_global\header');
to this:
Code:
$this->load->view('_global/header');

After changing the backslash to a forward slash it solved the problem.

Thanks for the help everyone. It was much appreciated.




Theme © iAndrew 2016 - Forum software by © MyBB