CodeIgniter Forums
Problem with views - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Problem with views (/showthread.php?tid=2476)



Problem with views - El Forum - 08-07-2007

[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?


Problem with views - El Forum - 08-07-2007

[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.


Problem with views - El Forum - 08-07-2007

[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


Problem with views - El Forum - 08-08-2007

[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 ?


Problem with views - El Forum - 08-08-2007

[eluser]sophistry[/eluser]
This has to be the most frequently asked question on the forums. Here is a link to the FAQ where you will find several links to threads as well as the numerous names that this design approach goes under.


Problem with views - El Forum - 08-08-2007

[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


Problem with views - El Forum - 08-08-2007

[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.


Problem with views - El Forum - 08-08-2007

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