Welcome Guest, Not a member yet? Register   Sign In
loading menu for all controllers ?
#1

[eluser]hardik[/eluser]
hi,

i am bit new to codeigniter. and i have set template like below..

i m creating one template.php

Code:
<html>
<head>
<title>
    <?php echo $title; ?>
</title>
</head>

<body>

<?php
echo $this->load->view('header');
echo $this->load->view('navigation');

<div>
echo $this->load->view($view);
</div>


echo $this->load->view('footer');
?&gt;


&lt;/body&gt;
&lt;/html&gt;

now i m loading it using controller like ....

Code:
class Welcome extends Controller {

    function Welcome()
    {
        parent::Controller();
        session_start();    
    }
    
    function index()
    {
        $data['navigationdata']=$this->navigation->getAllmenuitems(); // this loads all navigation items from model navigation
        $data['view'] = 'home'; // this loads home view
        $this->load->view('template',$data);
    }
    
}

all code works perfectly but after creating few more controllers like user,categories,....etc i have question

how can i load navigation data directly in my navigation view without loading it every time from all the controllers...?

is there a way i can load some data directly as my navigation will be in every page in the site?

i did search a lot and found great things in wiki. i came to this article

http://codeigniter.com/wiki/Header_and_F...ge_-_jedd/

i read it thrice but i didn't get how it works?

can anyone help me how i can achieve this ?
#2

[eluser]hardik[/eluser]
i was finally able to solve this at my own Smile

thanks to really cool tutorials and help available at CI forums and faq...

what i have created is one base controller which will be inherited by all other controllers and that base controller will inherit ci controller class. problem i got was i was not able to understand where to put my files for base controller as well as how i can access those variables that i declare in my base class controller to directly getting in view.

so here is what i have written

Code:
this will be stored in system/applications/libraries/MY_Controller.php , i got headache for this as i put it into controller directory but after searching forums finally i found i need to store it in libraries folder....

&lt;?php
class MY_Controller extends Controller
{    
    var $data=array();
        
    function MY_Controller()
    {        
        parent::Controller();
        $this->data['menu'] = $this->_getmenu ();
    }
    private function _getmenu (){
             //returns menu_array();
    }
}
?&gt;

now all my default controllers will be inheriting this controller...

Code:
&lt;?php
class Welcome extends MY_Controller {

    function Welcome()
    {
        parent::MY_Controller();
        session_start();    
    }
    function index()
    {
        $data['view'] = 'home';
        $this->load->view('template',$data);
    }

and finally my template

Code:
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;
    &lt;?php echo $title; ?&gt;
&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;

&lt;?php
echo $this->load->view('header');

echo $this->data['menu']; /* this will return menu from my base controller.....just check base class there i have declare data array variable in the header portion of the class...and i m not setting this variable from any other place its just sets only once in base class.....*/

<div>
echo $this->load->view($view);
</div>


echo $this->load->view('footer');
?&gt;


&lt;/body&gt;
&lt;/html&gt;

all ci guru's please review my code and tell me is it the right way ? am i doing something wrong or this is ok ?
#3

[eluser]jedd[/eluser]
[quote author="hardik" date="1259336458"]
i was finally able to solve this at my own Smile
[/quote]

Glad to hear.

Next time you have a problem, try consulting the [url="/wiki/FAQ"]FAQ[/url] or even [url="/wiki/Header_and_Footer_and_Menu_on_every_page_-_jedd/"]other bits[/url] of the wiki.
#4

[eluser]├╝berfuzz[/eluser]
Nice going harddik. If you're using the same template throughout the site, you might as well put the &lt;head&gt; stuff, the footer and what have you in the template. CI helps you with some nice functions in the url class.

Code:
&lt;?php echo doctype('xhtml1-strict'); ?&gt;

&lt;html &gt;

&lt;head&gt;
&lt;title&gt;Your beautiful site&lt;/title&gt;

&lt;?php echo meta($meta); ?&gt;

&lt;?php foreach($link_tags AS $tag): ?&gt;
&lt;?php echo link_tag($tag); ?&gt;

&lt;?php endforeach; ?&gt;

&lt;body&gt;
etc..

<div id="footer"></div>


&lt;/head&gt;
#5

[eluser]├╝berfuzz[/eluser]
Nice going harddik. If you're using the same template throughout the site, you might as well put the &lt;head&gt; stuff, the footer and what have you in the template. CI helps you with some nice functions in the url class.

Code:
&lt;?php echo doctype('xhtml1-strict'); ?&gt;

&lt;html &gt;

&lt;head&gt;
&lt;title&gt;Your beautiful site&lt;/title&gt;

&lt;?php echo meta($meta); ?&gt;

&lt;?php foreach($link_tags AS $tag): ?&gt;
&lt;?php echo link_tag($tag); ?&gt;

&lt;?php endforeach; ?&gt;

&lt;body&gt;
&lt;?php //maybe render a menu from sql-table. ?&gt;

<div id="footer"></div>


&lt;/head&gt;
#6

[eluser]Aken[/eluser]
Given that system of doing things also, if your header and footer and whatnot will not change very often, you might as well include their HTML in your template view also. That way you're making only loading two views - the template and the content - instead of four or five views.
#7

[eluser]jwindhorst[/eluser]
Here's the way I handle the header/footer issue. I first create a custom_functions_helper.php file in the helpers directory. Inside there I create this function:
Code:
function load_view_with_templates($views, $header_data=array())
    {
        // determine which layout we are using
        list($layout, $header_data['nav_style']) = explode("_", LAYOUT);
        $header_data['layout'] = $layout;

        $CI =& get_instance();
        $CI->load->view("/layout/header",$header_data);

        if(is_array($views))
            foreach($views AS $v=>$d)
                $CI->load->view("/layout/$layout/views/$v", $d);
        else
            $CI->load->view("/layout/$layout/views/$views", $header_data);

        $CI->load->view("/layout/footer");
    }

Then in my controllers, instead of calling the normal
Code:
$this->load->view('view_name);

I call:
Code:
load_view_with_templates($views, $data);

The views parameter can be an array, so if you needed to load multiple views between your header and footer that would also work fine.

Hope it helps.




Theme © iAndrew 2016 - Forum software by © MyBB