Welcome Guest, Not a member yet? Register   Sign In
Problem with views
#1

[eluser]proximo[/eluser]
I wanna try to do something like this:

Code:
<?php

class Welcome extends Controller {

    function __constructor()
    {
        parent::Controller();    
        
        $this->load->view('header', array());
    }
    
    function index()
    {
        $this->load->view('content',array());
    }
    
    function __destructor()
        {
        
        $this->load->view('footer',array());
    
    }
}
?>
But I have problem becouse there are no __destructor (white page) and this idea doesn't work :/. Someone has idea how to make this?
#2

[eluser]Michael Wales[/eluser]
There are literally dozens of posts on using partials/segments within your views - just do some searching around.

Using the constructor and destructor won't work, but there are other solutions.
#3

[eluser]chobo[/eluser]
I don't get why you want to have a destructor? You can load the footer from the index() function, you can also load the header views from the index() function, or any function you want.

Here is a really good thread for templating and partials.

Partial views
#4

[eluser]proximo[/eluser]
So, if I make so as chobo saying, I will must in every function repeat view header and footer. Probaly that i write helper or liblary.

So my code look like this:
Code:
<?php

class Welcome extends Controller {

    function Welcome()
    {
        parent::Controller();    
    }
    
    function index()
    {
        $data = array(
                        'heading' => 'My News Page',
                        'sidebar' => $this->db->get('sidebar'),
                         'content' => $this->db->get('content')
                         );

                 $this->load->vars($data);

                 $this->load->view('container');
    }

        function show(){

        $data = array(
                        'heading' => 'My News Page',
                        'sidebar' => $this->db->get('sidebar'),
                         'content' => $this->db->get('content')
                         );

                 $this->load->vars($data);

                 $this->load->view('container');

        }

}
?>

and this can slow down script ?
#6

[eluser]gunter[/eluser]
interesting idea to use constructor and destructor to output the header and footer html... does it work if you echo your views like this?

Code:
echo      $this->load->view('footer',array(),TRUE);

another possibility is:



Processing Output

CodeIgniter has an output class that takes care of sending your final rendered data to the web browser automatically. More information on this can be found in the Views and Output class pages. In some cases, however, you might want to post-process the finalized data in some way and send it to the browser yourself. CodeIgniter permits you to add a function named _output() to your controller that will receive the finalized output data.

Important: If your controller contains a function named _output(), it will alwaysbe called by the output class instead of echoing the finalized data directly. The first parameter of the function will contain the finalized output.


that means you could use this:
Code:
function _output($output)
{
    echo      $this->load->view('header',array(),TRUE);
    echo $output;
    echo      $this->load->view('footer',array(),TRUE);
}

that means, instead of your con/destructor view loading you simply use this _output() method
#7

[eluser]coolfactor[/eluser]
It's __construct() and __deconstruct() anyway.

No "or" on the end of those, but as stated above, this is not the best way to handle partials.
#8

[eluser]proximo[/eluser]
Ok thanks for help :] I use this method: http://ellislab.com/forums/viewthread/44916/#213643. It's good method for newbie :]




Theme © iAndrew 2016 - Forum software by © MyBB