Welcome Guest, Not a member yet? Register   Sign In
Need Help with Views
#3

[eluser]CroNiX[/eluser]
I believe in total separation of functionality. What that means to me, is that each controller should ONLY deal with its own specific type of logic. Like if I have a controller that is for products and has methods to add/delete/edit products, etc, I would never even deal with ads, or anything else, anywhere in that controller. Just like I would never output a header or footer directly from a controller. I only output the view generated by THAT controller, and pass it back to a master library which handles assembling all views from whatever other controllers are necessary.

Lets take your example of the ads.

In my master library (which is autoloaded), in the construct, it would get the current users id (or status or whatever). It would then call an internal library method to get the ads appropriate for that user by their level or whatever your criteria is. Now, this data is available anywhere from the library because it was autoloaded.

There is also another method in this library called generate_view($content), which would look something like this:
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Template
{
    private $ads = array();
    private $user = array();
    public $CI = null;

function __construct()
{
    $this->CI =& get_instance();
    $this->user = $this->load_user();  //you would have a method to get the user data via id from session
    $this->ads = $this->load_ads();  //you would have a method to get ads based on user data
}

function generate_view($content)
{
    $ads['ads'] = $this->ads();  //ads was already set in the construct of the library

    //lets generate the "ads" view and store it in an array (using 3rd parameter of TRUE to get it as a string)
    //We pass the $ads array to the ads view, and within that view it loops through the ads creating whatever is necessary for that view.  
    $data['ad_content'] = $this->CI->load->view('frontend/partials/ads', $ads, TRUE);  

    //gets header and footer...assuming there is nothing to pass to the header to create its view
    $data['header'] = $this->CI->load->view('frontend/partials/header', array(), TRUE);  
    $data['footer'] = $this->CI->load->view('frontend/partials/footer', array(), TRUE);  //gets footer

    //here we are just passing along the specific content generated by whatever controller we called this method from so we
    //can pass it to the "master template" view.
    $data['content'] = $content;

    //now output all of the "partial views" to the main template and then to the browser
    $this->CI->load->view('frontend/partials/main_template', $data);  //no 3rd parameter used here...just output it
}

}
//In your "main_template.php" file, you would have something like:
<?php echo $header; ?>
<div id="ads"><php echo $ads; ?&gt;</div>
<div id="content">&lt;?php echo $content; ?&gt;</div>
<div id="footer">&lt;?php echo $footer; ?&gt;</div>

//in your "ads.php" template you would have something like:
<ul>
&lt;?php foreach($ads as $ad): ?&gt;
    <li>&lt;?php echo $ad['name']; ?&gt;</li>
&lt;?php endforeach; ?&gt;
</ul>

So, if I am in my products controller, I might do something like:
Code:
....
class Products extends Controller
{
    function __construct()
    {
        parent::Controller();
    }

function get()
{
    $cat_id = (int) $this->uri->segment(3);
    $data['products'] = $this->product_model->get_products_by_cat($cat_id);

    //generate our view partial and send it to the template_library for display
    $content = $this->load->view('frontend/partials/product_display', $data, TRUE);

    $this->template_library->generate_view($content);
}

}
//now all of my classes methods are very clean and only deal with their own specific logic and everything else is done.
//Very...modularized and DRY.


Messages In This Thread
Need Help with Views - by El Forum - 10-11-2010, 07:56 AM
Need Help with Views - by El Forum - 10-11-2010, 11:13 AM
Need Help with Views - by El Forum - 10-11-2010, 02:06 PM
Need Help with Views - by El Forum - 10-11-2010, 08:38 PM



Theme © iAndrew 2016 - Forum software by © MyBB