Welcome Guest, Not a member yet? Register   Sign In
Where do you put header.php and footer.php?
#11

[eluser]Carlos Mora[/eluser]
@jason,
I've started using you template and, as bengrice said, $data appears to be out of scope. I'm not sure wich is the best way to pass the full $data array to subviews, but may be that the real $data should be in an elementt of original array, like in

<code>
function index()
{
$data["title"] = "Clientes";
$data["heading"] = "Lista de clientes";
$data["query"] = $this->db->get('clientes');

$view_data['page'] = 'cliente_view'; // pass the view to use as a parameter
$view_data['data'] = $data; // pass the real data so it is declared as $data

$this->load->view('container',$view_data);
</code>

Another solution may be use use
global $data;

inside container.php to bring back $data into scope. Both solutions worked here, but I'll appreciate the advise of an expert, in particular with the usage of global $data.

Best regards.

Carlos
#12

[eluser]bengrice[/eluser]
hi carlos,

just encase you missunderstood. i got an error if i tried to pass through the data var. if i just left it off everything passed through as normal eg this is my simple container view
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;?php $this->load->view('layout/meta'); ?&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;?php
$this->load->view('layout/header');
$this->load->view($page);
$this->load->view('layout/footer');
?&gt;
&lt;/body&gt;
&lt;/html&gt;

as you can see i haven't passed through the second variable the $data one however any data i set and passed through when i called this container view automagically becomes available in the nested views (accessed in the normal way ie calling the variable name directly)

hope you understand lol

ben
#13

[eluser]Carlos Mora[/eluser]
Ben,

In your code you are passing just the $page name, but no other info to show into the view.
The automagically part is the basic mechanism in CI to pass data to views. The magic happens in load->view(), where the array is converted into local vars, with a 'per function' scope, so loading nested views have the collateral effect of loosing the vars passed, ej. in your $this->load->view($page).
A solution is, as posted, to introduce the global $data; into the container.php, so nested views can receive the vars passed in the original $data array.

Please try changing your code into :

Code:
&lt;body&gt;;
&lt;?php
global $data;
$this->load->view('layout/header');
$this->load->view($page, $data);
$this->load->view('layout/footer');
?&gt;;
&lt;/body&gt;
#14

[eluser]bengrice[/eluser]
no what im trying to say is that mines works. . . im looking at it now and it passes through and works fine no global no data var thats all

cheers
ben
#15

[eluser]The Beginner[/eluser]
Hi all,
I really appreciate this post, it give me much greater insite into how to complete my first major project in CI. The only thing I am wondering is, what is contained in "&lt;?php $this->load->view('layout/meta');?&gt;"? is this meta data files? What is it's purpose?
#16

[eluser]khancute[/eluser]
[quote author="The Beginner" date="1253864836"]Hi all,
I really appreciate this post, it give me much greater insite into how to complete my first major project in CI. The only thing I am wondering is, what is contained in "&lt;?php $this->load->view('layout/meta');?&gt;"? is this meta data files? What is it's purpose?[/quote]

well if i'm not mistaken, the meta.php file is used to include all of your asset such as css files, javascript or ajax libraries.
#17

[eluser]The Beginner[/eluser]
So does that mean that the css file I use for my menu that is currently included in my header_view could be loaded in that file instead, keeping all those things in one place? And if that is the case, obivously it would be available to header_view when it loaded?
If I understand that correctly that is really cool. I still am having difficulty getting pea brain around that. Someone used the term AutoMagically, referring to CI, that is the way I feel about it right now.lol Hopefully someday soon I will get my head around it.
Thanks,
Del
#18

[eluser]vecima[/eluser]
I took a somewhat different approach to this (headers and footers).

in my controller (in my case my other controllers inherit from this one) i have a render method that the controllers call to set everything up.

application/libraries/Base_controller.php (you can call it whatever you want obviously)
Code:
&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Base_Controller extends Controller {

    var $data = array();    
    
    function Base_Controller()
    {
        parent::Controller();
    }

    function _render($view_data=NULL)
    {
        $content = "";
        if ($view_data != NULL) {
            if (is_array($view_data)) {
                foreach($view_data as $view) {
                    if ($view != NULL) {
                        $content .= $this->load->view($view, $this->data, true);
                    }
                }
            } else {
                $content .= $this->load->view($view_data, $this->data, true);
            }
        }

        $output = $this->load->view('header', $this->data, true);
        $output .= $content;
        $output .= $this->load->view('footer', $this->data, true);
        
        $this->output->set_output($output);
    }
}

then other controllers can call _render like so:

Code:
//just put one view between the header and footer
$this->_render('whatever_view');

//or, put an array of views between the header and footer
$this->_render(array('whatever_view', 'sidebar/some_sidebar_view', 'sidebar/another_sidebar_view'));

the only downside to this approach is that instead of having a "container" my header has:

&lt;html&gt;
&lt;head&gt;
&lt;/head&gt;
&lt;body&gt;
<some stuff />

and my footer has
<some stuff />
&lt;/body&gt;

so you aren't getting all the tags neatly closed in one view, but instead of a container, head, and footer, i just have a header and footer.
#19

[eluser]The Beginner[/eluser]
Hey guy's,
I have tried both ways (with..: global $data, and with out and by the time I call $this->load->view('wpmenu') I get and error that 'memu' is not a valid var yet I set it in my Controller for wpmenu to = $data['menu']= $my_menu_data) also in my heaader file I am including a CSS file and tried using $base_url() and I get an error there that says base_url is an invalid function even though I am loading 'url' in autoload.php where $autoload['helper'] = array('snappy_source','url');
I am sure I am missing something, I just can't seam to figure out what it is.
Any suggestions?
#20

[eluser]khancute[/eluser]
[quote author="The Beginner" date="1254014508"]Hey guy's,
I have tried both ways (with..: global $data, and with out and by the time I call $this->load->view('wpmenu') I get and error that 'memu' is not a valid var yet I set it in my Controller for wpmenu to = $data['menu']= $my_menu_data) also in my heaader file I am including a CSS file and tried using $base_url() and I get an error there that says base_url is an invalid function even though I am loading 'url' in autoload.php where $autoload['helper'] = array('snappy_source','url');
I am sure I am missing something, I just can't seam to figure out what it is.
Any suggestions?[/quote]

could you show your code, it kinda hard to see what might be the problem.
i might have a guess but i'm not sure, better wait till you show your code.

thanks




Theme © iAndrew 2016 - Forum software by © MyBB