Welcome Guest, Not a member yet? Register   Sign In
Header controller?
#1

[eluser]Felipe Deitos[/eluser]
I got a really newbie question related to templating... here we go:
I got a library called Template here is the code... (actually i find this code in some tutorial and i am using it, if anyone got a better one share it please =D)
Code:
class Template {

protected $CI;
var $template_data = array();

function __construct()
{
  $this->CI =& get_instance();
}
        
function set($name, $value)
{
  $this->template_data[$name] = $value;
}
    
function load($template = '', $view = '' , $view_data = array(), $return = FALSE)
{              
  $this->set('contents', $this->CI->load->view($view, $view_data, TRUE));            
  return $this->CI->load->view($template, $this->template_data, $return);
}
}

In my views folder i got a folde called templates with all my templates...
I will post my templates/site.php code here so you guys can understand what i am talking about...

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;title&gt;Home&lt;/title&gt;

&lt;!-- Meta
&lt;meta name="description" content=""/&gt;
&lt;meta name="keywords" content=""/&gt;

&lt;!-- Styles e Icone --&gt;
&lt;link rel="stylesheet" type="text/css" href="&lt;?php echo base_url(); ?&gt;assets/css/styles.css" /&gt;
&lt;link rel="shortcut icon" type="image/ico" href="" /&gt;

&lt;!-- Javascript --&gt;
[removed][removed]

&lt;/head&gt;

&lt;body id="&lt;?php if($this-&gt;uri-&gt;segment(1)){ echo 'internas'; } else { echo 'home'; } ?&gt;"&gt;
<div id="header" class="clearfix">
&lt;?php $this->load->view('includes/header'); ?&gt;
</div>
<div id="contents">
&lt;?php echo $contents; ?&gt;
</div>
<div id="footer">
&lt;?php $this->load->view('includes/footer'); ?&gt;
</div>
&lt;/body&gt;
&lt;/html&gt;

As you notice, i got a folder called includes inside views and got a header.php inside this folder...
My question is, how can i pull some database data in the header.php?
Maybe its a little bit confusing but i hope you guys can understand my question

Cheers! Thanks for helping
#2

[eluser]Felipe Deitos[/eluser]
Any clue? Please!

Thanks again!
#3

[eluser]ojcarga[/eluser]
Why don't you just call the
Code:
$this->load->view("path_to_the_header_dot_php/header", $data);

And, inside $data you can put all your database data you want to send to the views.

Cheers!
#4

[eluser]Felipe Deitos[/eluser]
I know but remember that i am calling $this->load->view('bla') in the view... not in the controller...
I am really confused about that...

I will try give and example...
The Template library that i posted above is already autoloaded and working...

This is my home controller... (look at the comments)
Code:
&lt;?php

class Home extends CI_Controller {

function __construct() {
  
  parent::__construct();
  $this->load->model("home_model");
  
}

function index() {
  
  $data['banners'] = $this->home_model->get('banners');
  $data['parceiros'] = $this->home_model->get('parceiros');
  $data['noticias'] = $this->home_model->get('noticias');
                // The first param is the template i am using... (I will post right after this)
                // The second one is the view i am loading...
                // Third is date comming from database
  $this->template->load('templates/home', 'home/index', $data);

}

}

/* End */

Here is the templates/home
Code:
// As you can see this is the template... it calls the header and footer and the var $contents is where the
// view will be placed... (You can see how it works in the Template library code above
&lt;body&gt;
<div id="header" class="clearfix">
&lt;?php $this->load->view('includes/header'); ?&gt;
</div>
<div id="contents">
&lt;?php echo $contents; ?&gt;
</div>
<div id="footer">
&lt;?php $this->load->view('includes/footer'); ?&gt;
</div>
&lt;/body&gt;

The includes/header is loaded in every page, what i need is to pull some $data here, but i dont see how...
Since i load the includes/header in a view and not the controller...


Its confusing but i hope someone can try to help me, Thanks for reading. Cheers!
#5

[eluser]adamck[/eluser]
You need to load the views in the controller, loading views from views will make life a pain!

have you looked into the template parser? its very easy to use.

This is a basic example.

Code:
function index()
{
  //LOAD MODEL
  $this->load->model('home_model');

  //LOAD DATA (Mysql queries with multiple rows are allowed by returning result_array(); )
  $header_data['headstuff'] = $this->home_model->header_stuff();
  $body_data['bodystuff'] = $this->home_model->body_stuff();
  $footer_data['footerstuff'] = $this->home_model->footer_stuff();

  //LOAD VIEWS (ive used subfolders to separate the theme from the body)
  $this->parser->parse('site/theme/header', $header_data);
  $this->parser->parse('site/body', $body_data);
  $this->parser->parse('site/theme/footer', $footer_data);
}

then in your views
Code:
&lt;body&gt;
{bodystuff}
{title} - {description}
{/bodystuff}
&lt;/body&gt;

it works very well and saves all the loops etc... having to be written.

I have also noticed when you load views in a controller, or a view, it tends to load them in order of what loads first gets displayed first... which means your body will load, then your header, then your footer... the parser fixes this issue!

Ad,
#6

[eluser]Felipe Deitos[/eluser]
hummm, i will have to do:
Code:
$header_data['headstuff'] = $this->home_model->header_stuff();
  $body_data['bodystuff'] = $this->home_model->body_stuff();
  $footer_data['footerstuff'] = $this->home_model->footer_stuff();

  //LOAD VIEWS
  $this->parser->parse('site/theme/header', $header_data);
  $this->parser->parse('site/competition', $body_data);
  $this->parser->parse('site/theme/footer', $footer_data);

In every controller? In Every model i will have to do the same query to the header.php?
#7

[eluser]ojcarga[/eluser]
[quote author="Felipe Deitos" date="1353412094"]I know but remember that i am calling $this->load->view('bla') in the view... not in the controller...
I am really confused about that...

I will try give and example...
The Template library that i posted above is already autoloaded and working...

This is my home controller... (look at the comments)
Code:
&lt;?php

class Home extends CI_Controller {

function __construct() {
  
  parent::__construct();
  $this->load->model("home_model");
  
}

function index() {
  
  $data['banners'] = $this->home_model->get('banners');
  $data['parceiros'] = $this->home_model->get('parceiros');
  $data['noticias'] = $this->home_model->get('noticias');
                // The first param is the template i am using... (I will post right after this)
                // The second one is the view i am loading...
                // Third is date comming from database
  $this->template->load('templates/home', 'home/index', $data);

}

}

/* End */


[/quote]

That is your code, cool, now look at this:

Code:
function index() {
  
  $data['banners'] = $this->home_model->get('banners');
  $data['parceiros'] = $this->home_model->get('parceiros');
  $data['noticias'] = $this->home_model->get('noticias');
  $data['data_for_the_header'] = "This is the content of the data for my header";
  $data['data_for_the_footer'] = "This is the content of the data for my footer";
              
  $this->load->view('home/index', $data);

/*
* Doing that, you will be able to use all the variables "inside" $data in any sub view loaded
* after, so, $data_for_the_header and $data_for_the_footer can be used any other view loaded
* after the first "->load->view" call
*/

}

So, yes, inside your "includes/header" and "includes/footer" views you can use
Code:
$data_for_the_header && $data_for_the_footer
#8

[eluser]satej[/eluser]
You can look into this template library. There is also documentation for it.
http://williamsconcepts.com/ci/codeignit.../template/
#9

[eluser]Felipe Deitos[/eluser]
#ojcarga
first of all thanks for the help you are giving me..
but unfotunately this isnt what i am looking for, i know how to pass and array throw a view and all this stuffs.

I will try one more time to explain what i want but not looking to my code this... lets start from zero hehe

Im my new site i will have a dropdown menu, in this menu we have the item called products on rollover the products all the categories(comming from database) will dropdown.

Thats it, this is my template that will show in everypage of my site... only the menu and the content will come right bellow the menu... (no footer)

This is my home controller...
Code:
// Go into products_model and get categories
$data['menu_categories'] = $this->products_model->get_categories();
// Load the header and put the $data with categories
$this->load->view('header', $data);
// Load the view home, here will be some randon content
$this->load->view('home');

This is my product controller
Code:
// Go into products_model and get categories
$data['menu_categories'] = $this->products_model->get_categories();
// Load the header and put the $data with categories
$this->load->view('header', $data);
// Load the view products, here will be some randon content about products
$this->load->view('products');

Here is my giant question...
i will have to put the $data['menu_categories'] in every controller?
There is no way of doing like a header controller?
This header controller build the entire header that is loaded just once at the top of every page...

This is really hard to explain but it seems so simple and i cannot do this... There is like 1 million posts about templates and no one seems to do something like that.

Sorry for bothering you guys!

Cheers!

#10

[eluser]CroNiX[/eluser]
[quote author="Felipe Deitos" date="1353434468"]
i will have to put the $data['menu_categories'] in every controller?
There is no way of doing like a header controller?
This header controller build the entire header that is loaded just once at the top of every page...
[/quote]
No, you can put it in your template library.

In the library I use, all I do is pass the template library the CURRENT view from whatever controller. The library automatically puts the header (retrieving data from the database, checking user permissions, only displaying things that that user level allows, etc), menu and footer. It just inserts whatever content I send it in between them. So none of my controllers have anything to do with the header, menu or footer. They just deal with the specific task they are supposed to do.

For instance, in my controller...

Code:
//...do work...
$data['something'] = $this->some_model->get_data();

$page_title = 'Your Page Title';
//load the view for THIS controller, as a variable, and pass it to the template.
$view = $this->load->view('includes/user_profile', $data, TRUE);
$this->template->generate($page_title, $view);

//in /application/libraries/Template.php...

Code:
function generate($page_title, $content)
{
  $CI =& get_instance();

  //build the header and insert the page_title
  $header_data['page_title'] = $page_title;
  $header_data['menu'] = $CI->some_model->get_menu();
  $header = $CI->load->view('header', $header_data, TRUE);

  //Now output the header and the passed in content using CI's output class
  $CI->output->set_output($header . $content);  
}




Theme © iAndrew 2016 - Forum software by © MyBB