Welcome Guest, Not a member yet? Register   Sign In
Nested views, need a solution
#1

[eluser]Unknown[/eluser]
I'm fairly new to CodeIgniter, and I've never really worked with frameworks, so bear with me. I'm setting up a blog (go figure), and I'm using some nested views, but unfortunately, the views are not being drawn as I had hoped. Here is my main view file...

Code:
<? $this->load->view('static/header.php') ?>

<?=$auth ?>
<?=$page ?>

<? $this->load->view('static/footer.php') ?>

And here is what $auth and $page are defined as in my home class.

Code:
function index()
{
    // Check if the user is authed.
    if (!$this->dx_auth->is_logged_in())
    {
        $data['auth'] = $this->load->view('static/login_small');
    }
        
    // Draw the page contents.
    $data['page'] = $this->load->view('pages/home/index.php');
        
    $this->load->vars($data);
    $this->load->view('home_view');
}

I would have thought that the page would end up being drawn in the order of Header, Auth, Page, and then Footer. However, it is drawn in the following order: Auth, Page, Header, Footer. I'm assuming this is on purpose so that headers aren't sent before the views are executed, so I'm looking for a solution to this problem without having to define some sort of $header and $footer template variable in every class file. Any help is appreciated.
#2

[eluser]devbro[/eluser]
no no no. you forgot to pass the variables to your view.

http://ellislab.com/codeigniter/user-gui...views.html

and look up: Adding Dynamic Data to the View

it explains how to send variables to view files.

also keep in mind that loading a view does not stop the code from continuing. you either need to call die() or redirect().
#3

[eluser]David Johansson[/eluser]
The variables are passed to the view by the load->vars() function, so that isn't the problem.

The problem is that you are outputting your the auth and the page view when you are creating them instrad of returning them as strings.

This should do it:
Code:
function index()
{
    // Check if the user is authed.
    if (!$this->dx_auth->is_logged_in())
    {
        $data['auth'] = $this->load->view('static/login_small','',TRUE);
    }
        
    // Draw the page contents.
    $data['page'] = $this->load->view('pages/home/index.php','',TRUE);
        
    $this->load->vars($data);
    $this->load->view('home_view');
}

When you add the third parameter TRUE to the view function the output is passed as a string.




Theme © iAndrew 2016 - Forum software by © MyBB