Welcome Guest, Not a member yet? Register   Sign In
How to contain header files in the CI?
#1

[eluser]shinianyijian[/eluser]
In PHP,I know that to do so:include "header.php",but how to do it in the ci? Still do that?or...
#2

[eluser]Crackz0r[/eluser]
Hello.

You could use a Template system like this:

IN your views folder you could create a subfolder called includes, right there you could create 3 files: header.php, template.php, footer.php (although you could use as many as you need to use "fixed content"). And that files are:

template.php:

Code:
<?php
if (!defined('BASEPATH')) { exit('No direct script access allowed'); }

$this->load->view('includes/header'); //here you load the header view, in the includes folder, which is a fixed content in all your pages... i think
$this->load->view($main_content); //this variable is what i'm using to load a view dynamically
$this->load->view('includes/footer'); //here you load the header view, in the includes folder, which is a fixed footer content.
?>

header.php:
This is the html contento for the header...itt could looks like this, which is my case

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;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
&lt;link rel="shortcut icon" href="&lt;?php echo base_url(); ?&gt;media/favicon.ico" /&gt;
&lt;link rel="shortcut icon" href="&lt;?php echo base_url(); ?&gt;media/favicon.png" type="image/png" /&gt;
&lt;link rel="stylesheet" type="text/css" href="&lt;?php echo base_url(); ?&gt;css/global.css" /&gt;
&lt;link rel="stylesheet" type="text/css" href="&lt;?php echo base_url(); ?&gt;css/project/jquery-ui-1.8.20.custom.css" /&gt;
[removed][removed]
[removed][removed]
&lt;title&gt;&lt;?php echo $title ?&gt;&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
<div class="header left" id="header" >
<a href="&lt;?php echo base_url() ?&gt;"><img src="&lt;?php echo base_url() ?&gt;media/logo.png" border="0" class="left" alt="VetBoard.CO" /></a>
</div>
<div>HERE YOU COULD I.E. SET YOU MENU NAV BAR</div>

As you can see, it's is a normal html page with all the featuresw it needs... Also, note that the title, is a vairable, which we will se how to set it up in some moments.

footer.php
Here you then need to close the body and html tags, as well as you can set any static content, which will be seen in all your pages as the header.

Code:
<div>ANY CONTENT YOU WANT TO BE SHARED IN ALL YOUR PAGES</div>
&lt;/body&gt;
&lt;/html&gt;

This is almost the full body structure of your site... now lets see the controler, its called, humm, login and ofc its placed whitin you controllers folder in you application folder... as usual.

login.php

Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {

    function __construct()
    {
    parent::__construct();
  $this->load->library(array('form_validation'));
  $this->load->helper(array('form', 'url', 'string'));
    }
    
    public function index()
    {
        if($this->session->userdata('logged_in') === TRUE)
        {
            redirect('/dashboard/', 'location');
        }
        else
        {
     $show['title']    =    'My Site Title, loaded in the view and changed dynamically in each of you controller';
            $show['main_content'] =   'login_view';
            $this->load->view('includes/template', $show);
        }
    }

Here as you can see... not the function itself, what i want to show you is, the else part, when you call a view, there are a few variables, the title and that called main_content, the title, is the title that you may want to set in that specific part of your site, the other variable, is the fragment or the single view wich is going to be loaded in the template.php file, if your remember, there was a piece: $this->load->view($main_content); well passing this as a variable, you are allowed to set in each controller a specific view to load... and then you load the view in the last line: $this->load->view('includes/template', $show); passing the variables as parameters for the template view...

Now finally lets create the login view, which will be placed in your views folder, NOT IN the includes folder and here is:

login_view.php

Remember, file placed in views folder and called as you loaded it: login_view

Code:
&lt;?php echo form_open('account/signin'); ?&gt;
  <ul>
   <li>
    <label for="email">e-Mail: </label>
       &lt;input id="email" name="email" type="text" value="&lt;?php echo set_value('email'); ?&gt;" /&gt;
      
   </li>
   <li>
    <label for="password">Password: </label>
       &lt;input id="password" name="password" type="password" /&gt;
    
   </li>
  
  
   <li>
       <label></label>
       <span>input type="submit" value="Log in"  style="width:120px"/>
       </span>
   </li>
  </ul>
  &lt;?php echo form_close(); ?&gt;

And thats it, you will get a full html page, with dynamic content and easy to manage and maintain, since you will only need to create, modify or delet the single views...

Hope it help you reggards
#3

[eluser]markanderson993[/eluser]
I agree with Crackz0r that a templating system would be a great way to solve your problem. To implement it however, I was able to create a library named template.php, which I added to my autoload config. My class looked something like this..

*edit - sorry for the lack of formatting. I just slapped some code out there and couldn't find a convenient way to format it Sad

Code:
&lt;?php

class Template {
function load($view = '', $vars = array(), $return = false) {
$this->CI =& get_instance();
if(!isset($vars['head_title'])) $vars['head_title'] = $this->CI->config->item('website_name');
$vars['template_contents'] = APPPATH . 'views/'.$view;
return $this->CI->load->view('template/container', $vars, $return)
}
}

The beauty in a class like this is that whenever you're ready to load a view via a controller.. you can do something like this
Code:
class Home extends CI_Controller {
function index() {
$data = array(); // super awesome
$this->template->load('home/index',$data);
}
}

Hope this helps you out!




Theme © iAndrew 2016 - Forum software by © MyBB