• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Where do you put header.php and footer.php?

#1
[eluser]johnnyForums[/eluser]
First, do you use a footer and header.php file and if you do where do you put it? The way I had/have it is I found a free template somewhere and chopped it up into header and footer items. But, since it has .php items in it I was unsure if I should put it in my assets folder.

Thank you.

#2
[eluser]jshultz[/eluser]
I put them in the views. Here's how I do it:

My top level folder has these folders:

application - the application folder
assets: contains css, javascript, images
system - the system folder
user_guide - the user guide

Now, in the application/views folder I have a file called container.php which contains this:

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html &gt;
&lt;head&gt;
&lt;?php
$this->load->view('meta');
?&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;?php
          $this->load->view('header', $data);
          $this->load->view('menu');
          $this->load->view('sidebar', $data);
          $this->load->view($page, $data);
          $this->load->view('footer');
?&gt;
&lt;/body&gt;
&lt;/html&gt;

Now, obviously you can see that the container view loads the other views (i.e. header, menu, sidebar, and footer. You'll notice, though that I have $page in there also. In my controller, I have the following code (for example):

Code:
function about()
    {
          $data['page_title'] = 'OSM Projects';
          $data['page'] = 'about'; // pass the actual view to use as a parameter
          $this->load->view('container',$data);
        
    }

$page is just a variable that I use to pass whatever view I want to be loaded with that particular function whether it's my about view, welcome view, or whatever.

This is the way I do it, it keeps my controller pretty clean (or cleaner) and seems to do the trick. Is this the best way? I'm not sure. I'm still learning as well. I actually learned this technique in the forums myself and it's worked pretty well. Soon, I hope to learn an even better way but this will get me started. Hopefully, it'll do the same for you.

#3
[eluser]johnnyForums[/eluser]
Thanks. I don't know that I understand the $page though. Is $page not $data['page'] because I am not in the controller but in the view. I know that's a basic question, just trying to understand. Also is $data, in about() passed to all the other views in container.php (which is your view file, if I understand you.

So essentially, they browse to http://localhost/about. Is that right for the way you have it set up? Or do I misunderstand?

#4
[eluser]jshultz[/eluser]
right. In the controller it's just 'page' and it equals the about view. so when the container view loads it knows that 'page' in this particular instance will be the about view. make sense?

#5
[eluser]johnnyForums[/eluser]
[quote author="jshultz" date="1249618997"]right. In the controller it's just 'page' and it equals the about view. so when the container view loads it knows that 'page' in this particular instance will be the about view. make sense?[/quote]

Yes, but I think that I am not understanding something fundemental.

How did $data['page'] in:


Code:
$data['page'] = 'about'; // pass the actual view to use as a parameter
     $this->load->view('container',$data);



become just $page in:

Code:
$this->load->view($page, $data);

I know you are passing $data to the view container and container.php gets $data. I'm just asking how come you can reference it as $page without the $data['page'] array prefix? What happened to $data['']?

Also, is $data the same for all these pages:

Code:
$this->load->view('header', $data);
          $this->load->view('menu');
          $this->load->view('sidebar', $data);
          $this->load->view($page, $data);
          $this->load->view('footer');

Seems like it is or that it must be.

#6
[eluser]jshultz[/eluser]
Here's more from my site controller:
Code:
function index()
    {

          $data['user_id']    = $this->tank_auth->get_user_id();
          $data['username']    = $this->tank_auth->get_username();
          $data['page_title'] = 'OSM Projects';
          $data['page'] = 'welcome'; // pass the actual view to use as a parameter
          $this->load->view('container',$data);

    }

    function about()
    {
          $data['user_id']    = $this->tank_auth->get_user_id();
          $data['username']    = $this->tank_auth->get_username();
          $data['page_title'] = 'OSM Projects';
          $data['page'] = 'about'; // pass the actual view to use as a parameter
          $this->load->view('container',$data);
        
    }

function project_list()
    {
          if (!$this->tank_auth->is_logged_in()) {
            redirect('/auth/login/');
          } else {
            $this->load->model('Project_model');
            /*$data['query'] = $this->Project_model->getAll();*/
            $data['query'] = $this->Project_model->getByUserId();
            $data['user_id']    = $this->tank_auth->get_user_id();
            $data['username']    = $this->tank_auth->get_username();
            $data['page_title'] = 'OSM Projects';
            $data['page'] = 'project-list-view'; // pass the actual view to use as a parameter
            $this->load->view('container',$data);
        
        }

    }


$data[] is an array. i'm assigning the value 'about' or 'project-list-view' or 'welcome' to the key 'page' in the $data array.

when the container loads it now begins loading the the various values from the $data array. Now, in regards to the question why $page has a $ in front of it in the container view and it doesn't in the site controller? I'm not sure. I just know it needs to be done or it breaks. Hopefully, somebody reading this can weight in on that.

Here's my educated guess:

$data[] is an array and as such you have to have the $ at the beginning in the site controller however $data['page'] doesn't require it because in this context it's page is a key in the array. However, in the view $page is a variable (albeit a part of the $data array) and you have to write it as $page in the view in order to have it render correctly.

Thank you for asking the question, though. Like I said earlier, I'm learning, too, and this makes me think more about what I'm doing and helps me to learn as well.

#7
[eluser]johnnyForums[/eluser]
I think you answered it. Thank you. I'm sure I just didn't listen or understand something that was said over and over that $data, the array passed can have its members referenced by $var name that was a keyname before. Maybe someone else can comment.

#8
[eluser]ToddyBoy[/eluser]
Would you be able to post what you have in your sidebar.php view file?

My site is set up pretty much the same as yours but am having trouble passing values from one view to another with the $data array.

#9
[eluser]jshultz[/eluser]
sure, here you go:

Code:
<div id="sidebar" class="span-7">
  <h2>Get Started</h2>
    
    &lt;?php if (!$this->tank_auth->is_logged_in()): ?&gt;
    
        <p>You're not logged in. Please&lt;?php echo anchor('/auth/login', 'login'); ?&gt;</p>
    
    &lt;?php else: ?&gt;
    
        <p>Hi, <strong>&lt;?php echo $username; ?&gt;</strong>! You are logged in now. &lt;?php echo anchor('/auth/logout/', 'Logout'); ?&gt;</p>
    
    &lt;?php endif; ?&gt;


<h2>Open Sky Media Sites</h2>
<p><a href="http://theosmblog.com" title="The Open Sky Media Blog" target="_blank">The OSM Blog</a></p>
<p><a href="http://sedonaphotoexperience.com" title="Have Your Own Unique Sedona Photo Experience!" target="_blank">Sedona Photo Experience</a></p>
<p><a href="http://verdevalleyblog.com" title="Verde Valley Blog and Independent News Source">Verde Valley Blog</a></p>
<p>&nbsp;</p>
</div>

#10
[eluser]bengrice[/eluser]
Hi there,

just an update to this:

i liked the way you did this so i have used it, however when i built my included views got the error could not find data variable. this is where you were passing the data on to the next view. what i did was removed the data var from the included views and it worked. by the looks of it the data array automatically get passed through to nested views (if CI could confirm this that would be great)

good work

thanks all

ben


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2017 MyBB Group.