Welcome Guest, Not a member yet? Register   Sign In
View load problem
#1

[eluser]Unknown[/eluser]
I am using the following code in a application/controllers/frontpage.php
Code:
<?php
class Frontpage extends Controller {

    function index()
    {
        $this->load->view('themes/v1/header');
        $this->load->view('themes/v1/footer');
    }
}
?>

For some reason it only loads the last enter view
In this case the footer
Why?
#2

[eluser]David Cassidy[/eluser]
The CodeIgniter Output library is designed to handle a single view file. Although, embedding templates within other templates is common, and very easy to do with CodeIgniter. While there are several method to use, I'd recommend one of the following:

Method 1

Controller
Code:
<?php

class Example extends Controller {

    var $data;
    
    function Example() {
        parent::Controller();
        }
        
    function index() {
        # Assign template vars
        $this->data['somevar'] = "Some value";
        
        # Display view
        $this->load->view('example', $this->data);
        }

    }

?>

View
Code:
<? this->load->view('header', $this->vars); ?>

<p>Hello, World! Here is our dynamic data: &lt;?=$somevar;?&gt;</p>

&lt;? this->load->view('footer', $this->vars);

Method 2
This method will allow you to declare all of your templates within the controller and keeping your views cleaner.

Controller
Code:
&lt;?php

class Example extends Controller {

    var $data;

    function Example() {
        parent::Controller();
        }

    function index() {
        # Assign template vars
        $this->data['somevar'] = "Some value";

        # Define layout
        $layout  = $this->load->view('header',  $this->data, true);
        $layout .= $this->load->view('example', $this->data, true);
        $layout .= $this->load->view('footer',  $this->data, true);

        # Contruct view
        $this->output->set_output($layout);

        # Display view
        $this->output->get_output();
        }

    }

?&gt;

The point in Method 2 is that you use the optional third parameter of $this->load->view(), which allows you to return the template as a string. You could, at this point simply echo this to the user, but would lose the ability to use CI's caching library. Instead, you use the set_output() and get_output() to properly send the output to the browser.




Theme © iAndrew 2016 - Forum software by © MyBB