Welcome Guest, Not a member yet? Register   Sign In
Way to go about a simple header
#1

[eluser]dehumanizer[/eluser]
Hello there,

I discovered CI some weeks ago and have been spellbound since, this is very cool really.

I would like to ask the way to go about a common header where I have the company logo, primary navigation, sign in and sign up links.

Suppose I need to have the logo and meta-tags according to the domain name, display additional links if the user is logged as admin or not, etc.

Since I can't load the Header controller inside other controllers what I can understand is that I would build the header with a pre_controller hook, there I can have all the queries and conditional logic to parse the header.

I also tested the MX HMVC, after installation I tried this example and worked:

Code:
<?php echo modules::run('header'); ?>
<p>&lt;?php echo $body; ?&gt;</p>
&lt;?php echo modules::run('footer'); ?&gt;

Seems very good, but I'm really not sure if this is coming along for CI 2.5, 3, 4, etc.

I appreciate any help.
#2

[eluser]boltsabre[/eluser]
There's a few ways to go about it:

- You can extend the base controller with MY_Controller, thus any other "normal" controller that extends this will have access to your code.
- For your "sign in / sign up" links I just check for my session variable inside my view code and echo out the appropriate link (and username if they are logged in). I'm happy with this, but others will have their own opinion/view on this.
- You can use a template format for including "page modules" (header, footer, side nav, etc).

I actually have my modules divided up like this, and just include them in each view (I'm not using the template method).

Code:
// a view file
&lt;?php $this->load->view('includes/header_open'); ?&gt;
// header open has normal stuff like site wide stylesheet, js file, favicon, doctype, etc
// If this particular page needs anything specific, like a JS file, CSS stylesheet, etc, just code it in here
&lt;?php $this->load->view('includes/header_close'); ?&gt;
//Head close includes closing &lt;/head&gt; tag, opening &lt;body&gt; tag, top icon image, top nav bar, login/signup stuff, etc

// body content goes here

&lt;?php $this->load->view('includes/footer_open'); ?&gt;
// footer open has my closing body wrapper div, and my "footer" with "contact us, copyright, etc
// If I need any JS at the bottom for this view put it here
&lt;?php $this->load->view('includes/footer_close'); ?&gt;
//this is the closing &lt;/html&gt; tag, etc

Problem with this method is that I have to put these includes in each and every view file, and if I ever want to change the format I'm in for a hell of a lot work updating each view, but it gives me the flexibility to include specific JS files on individual pages manually, place inline JS up in the header, or footer, etc.
#3

[eluser]dehumanizer[/eluser]

Thank you Mr. boltsabre for these clarifications.

Let me ask a few things about your code if you allow me.

The included files in the code I suppose are static files?

The conditional logic for the header and footer would be located where exactly related to this code?

Regards!
#4

[eluser]boltsabre[/eluser]
Quote:The included files in the code I suppose are static files?
To a certain degree, but you really can do whatever you like in them.

For example, in my 'includes/footer_close' I have something like this:
Code:
...
&lt;?php if(!isset($no_social_links)){
   // JS code for my fb, twitter, google+ like buttons
}
?&gt;
&lt;/html&gt;
In any view that I don't want to display the "like buttons" (such as the "user panel" pages), in my controller I just set:
$data['no_social_links'] = true;


And in my 'includes/header_close' file, I have something like this
Code:
...
&lt;/head&gt;
&lt;body&gt;
... // logo
&lt;?php if(isset($this->session->userdata('logged_in'))){
   //echo out user name, display html "logout" link, etc
}else{
   // display html "login" link and html "sign up" link, etc
}
...
<div class="breadcrumbs">
&lt;?php echo $breadcrumbs; ?&gt; //created in a helper file I've created
</div>
...

Quote:The conditional logic for the header and footer would be located where exactly related to this code?
Where ever makes the most sense for what you are trying to do. As mentioned, I've made "breadcrumb" helpers for that stuff, my "logged in status" is just coded direct into the actual view code, when I don't want to display social media like buttons I just set a variable in that specific function (or in the constructor if I don't want them for the entire controller), it really just depends on what you want to do.

Hope that helps a bit. And I hope some other people post on here, I'm sure there are many other approaches. I picked this way because it gives me lots of flexibility on a page to page basis, but as mentioned, the trade off is that if I ever need to upgrade my approach I will have to manually go and edit 1000+ view files...
#5

[eluser]dehumanizer[/eluser]

Thank you again sir, reading your reply I could also grab more concepts about codeigniter.

Best Regards!
#6

[eluser]boltsabre[/eluser]
no worries champ




Theme © iAndrew 2016 - Forum software by © MyBB