Welcome Guest, Not a member yet? Register   Sign In
Efficiently re-using code
#8

[eluser]Xandrios[/eluser]
Thanks a lot Smile

viewLib seems to be a bit limiting, due to the static header/footer/sides definitions. I decided to go with Coolfactor’s Library.

To be able to simplify things I decided to extend the Coolfactor’s library. The content of this extended class could/should probably be different for each site. This is what it looks like for my test:
Code:
class Theview extends View {
    var $CI;
    var $content;
    
    function Theview(){
        parent::View();
        $this->CI = & get_instance();
    }
    
    function partReturn($view) {
        return $this->CI->load->view($view, $this->vars, TRUE);
    }
    
    function enableSideBoxes($sideboxes){
        
        //loop through boxes
        foreach ($sideboxes as $box){    
            switch($box){
                case "poll": //set data vars for poll
                    $this->CI->load->model("Pollmodel");                
                    $pollquestions = $this->CI->Pollmodel->getData();                
                    $this->CI->theview->set("pollquestions", $pollquestions);
                break;
            }
            
            //collect view data
            $output[$box] = $this->CI->theview->partReturn($box);    
        }    
        
        //pass view data to 'boxes' var, to be used in the mid-view
        $this->CI->theview->set('boxes', $output);
    }
    
    function addContent($content){
        $this->content = $content;
    }
    
    function showAll(){
        //add header
        $this->CI->theview->part('header', 'header.php');
        
        //add content
        $this->CI->theview->set('content', $this->content);
        $this->CI->theview->part('mid', 'mid.php');
        
        //add footer
        $this->CI->theview->part('footer', 'footer.php');
        
        //spit out the page
        $this->CI->theview->load('pagelayout');
    }
}

This defines a couple of things. First I added a function to complement the Coolfactor’s part() function: partReturn(). That way I am able to retrieve the data of the partial view as well. Having all data in variables is a bit more flexible. I only output the final data using the 'pageLayout' view, which simply echoes the header, mid and footer vars.

This site has a header, midsection with multiple sideboxes, a content section, and a footer. The most interesting ones are the sideboxes. I enable them by passing an array with the names of the ones I want for that specific page. The enableSideBoxes() function loads the views of each sidebox and if needed gathers some data from their Models. The output from the sideboxes is saved in the 'boxes' var.

The mid-view looks like this, and displays the sideboxes and main content:
Code:
<table border="1">
    <tr>
        <td width="100">
        
            &lt;?php foreach($boxes as $box=>$boxdata):?&gt;
                &lt;!--- &lt;?=$box?&gt; --&gt;
                &lt;?=$boxdata;?&gt;
            &lt;?php endforeach;?&gt;
        
        </td>
        <td width="600">
            &lt;?=$content?&gt;
        </td>
    </tr>
</table>

And the poll-sidebox view:
Code:
<strong>Poll</strong>
<p>
    <ul>    
    &lt;?php foreach($pollquestions as $question):?&gt;

    <li>&lt;?=$question;?&gt;</li>

    &lt;?php endforeach;?&gt;
    </ul>
</p>

Now the controller looks like this:
Code:
class News extends Controller {

    function index()
    {        
        $this->theview->enableSideBoxes(array("ad", "poll"));
        
        $this->theview->set("story1", "Hello, this is a news story!");
        $this->theview->set("story2", "And another one");
        $content = $this->theview->partReturn("news");
        
        $this->theview->addContent($content);
        $this->theview->showAll();
    }
}

It might look a bit confusing, but for every next page I only need to call the functions shown in the example controller. Which is quite easy, and allows to define how the sides should look like.


Messages In This Thread
Efficiently re-using code - by El Forum - 07-06-2007, 01:45 PM
Efficiently re-using code - by El Forum - 07-06-2007, 02:34 PM
Efficiently re-using code - by El Forum - 07-06-2007, 03:08 PM
Efficiently re-using code - by El Forum - 07-06-2007, 07:12 PM
Efficiently re-using code - by El Forum - 07-06-2007, 07:25 PM
Efficiently re-using code - by El Forum - 07-07-2007, 05:50 AM
Efficiently re-using code - by El Forum - 07-07-2007, 06:23 PM
Efficiently re-using code - by El Forum - 07-08-2007, 10:28 AM



Theme © iAndrew 2016 - Forum software by © MyBB