EllisLab text mark
Advanced Search
     
Header controller?
Posted: 16 November 2012 08:04 AM   [ Ignore ]
Joined: 2012-07-10
32 posts

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)

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_dataTRUE));            
  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…

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
html>
<
head>
<
meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<
title>Home</title>

<!-- 
Meta 
<meta name="description" content=""/>
<
meta name="keywords" content=""/>

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

<!-- 
Javascript -->
[removed][removed]

</head>

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

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

 

 
Posted: 16 November 2012 11:31 AM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Joined: 2012-07-10
32 posts

Any clue? Please!

Thanks again!

 
Posted: 16 November 2012 07:51 PM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2012-04-02
85 posts

Why don’t you just call the

$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!

 
Posted: 20 November 2012 07:48 AM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Joined: 2012-07-10
32 posts

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)

<?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

// 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
<body>
<
div id="header" class="clearfix">
<?php $this->load->view('includes/header'); ?>
</div>
<
div id="contents">
<?php echo $contents?>
</div>
<
div id="footer">
<?php $this->load->view('includes/footer'); ?>
</div>
</
body

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!

 
Posted: 20 November 2012 08:01 AM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Joined: 2012-07-27
27 posts

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.

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

<body>
{bodystuff}
{title} 
{description}
{
/bodystuff}
</body

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,

 
Posted: 20 November 2012 08:10 AM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Joined: 2012-07-10
32 posts

hummm, i will have to do:

$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?

 

 
Posted: 20 November 2012 12:42 PM   [ Ignore ]   [ # 6 ]   [ Rating: 0 ]
Avatar
Joined: 2012-04-02
85 posts
Felipe Deitos - 20 November 2012 07:48 AM

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)

<?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 */ 

 

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

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

$data_for_the_header && $data_for_the_footer 
 
Posted: 20 November 2012 12:48 PM   [ Ignore ]   [ # 7 ]   [ Rating: 0 ]
Joined: 2011-09-29
16 posts

You can look into this template library. There is also documentation for it.
http://williamsconcepts.com/ci/codeigniter/libraries/template/

 
Posted: 20 November 2012 02:01 PM   [ Ignore ]   [ # 8 ]   [ Rating: 0 ]
Joined: 2012-07-10
32 posts

#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…

// 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

// 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!

 

 
Posted: 20 November 2012 02:11 PM   [ Ignore ]   [ # 9 ]   [ Rating: 0 ]
Avatar
Joined: 2009-02-19
3796 posts
Felipe Deitos - 20 November 2012 02:01 PM

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…

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…

//...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'$dataTRUE);
$this->template->generate($page_title$view); 

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

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_dataTRUE);

  
//Now output the header and the passed in content using CI's output class
  
$CI->output->set_output($header $content);  
 Signature 
 
Posted: 20 November 2012 02:36 PM   [ Ignore ]   [ # 10 ]   [ Rating: 0 ]
Joined: 2012-07-10
32 posts

Thatssss getting better… its really simple and dont need to download anything.
And i will probably increment a zillion things in this library hehehe

One more question in your library you do this…

$header_data['menu'$CI->some_model->get_menu(); 

This “some_model” is any model inside the app/model folder? simple like that?

Thanks CroNiX!

 
Posted: 20 November 2012 02:45 PM   [ Ignore ]   [ # 11 ]   [ Rating: 0 ]
Avatar
Joined: 2009-02-19
3796 posts

Yes, but my code doesn’t show it being loaded.  It was an example of how to use CI’s resources within your own library.  You’d either have to load that model in the library, or autoload it.

 Signature 
 
Posted: 21 November 2012 12:02 AM   [ Ignore ]   [ # 12 ]   [ Rating: 0 ]
Joined: 2011-09-29
16 posts

In the template library I have mentioned above, there is a option to set a default content of a portion of the page, instead of dynamically changing it everytime.

http://williamsconcepts.com/ci/codeigniter/libraries/template/


You can refer the following lines

It looks like our header and footer regions will have the same content on just about every page. Keeping them as regions will make our template flexible, but we can also define default content for these regions, which we can either append or overwrite from our code. We’ll add these lines of code to the bottom of config/template.php:
$template[‘default’][‘regions’][‘header’] = array(‘content’ => array(’<h1>CI Rocks!</h1>’));
$template[‘default’][‘regions’][‘footer’] = array(‘content’ => array(’© Our Company Inc.’));

in this link

Hope it helps:)

 

 
Posted: 21 November 2012 06:45 AM   [ Ignore ]   [ # 13 ]   [ Rating: 0 ]
Joined: 2012-07-10
32 posts

CroNiX and Satej you guys are awsome!
Thanks for the answers!
I will probably go a little deeper in this template thing starting from your thoughts!

Thanks again!

Cheers!

 
Posted: 21 November 2012 07:15 AM   [ Ignore ]   [ # 14 ]   [ Rating: 0 ]
Joined: 2011-09-29
16 posts

Happy to help smile