CodeIgniter Forums
reuse of code, controller inside view - 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: reuse of code, controller inside view (/showthread.php?tid=32544)



reuse of code, controller inside view - El Forum - 07-27-2010

[eluser]Unknown[/eluser]
I have problem with reusing code. Imagin that i have many controllers. Every contoller loads "top menu view", "left menu view", "right panel", "content view".

Every one of this views contains data that depends on current state so lot of code is calculated inside controller and only vars are pushed to view. Now, with 10+ controllers this type of coding is hard to modify or maintain. Best solution will be to load "top menu controller", "left menu controller", "right panel contoller" atc, but i know that CI does not support it. Do you have any suggestions how to deal with this?


Second question is about loading controller inside view. I want tohave different view for every category but all categories are handled by one controler. What view to load controller knows from database. Now, as i said before, every category looks different. I would like do split every category view split into many reusable parts and like in previous question: every part is dynamic. For me best solution will be eg.

category_cars_view
->load header_controller
->load left_menu_controller
->load cars_category_content_view

category_planes_view
->load header_controller
->load left_menu_controller
->load left_advert_controller
->load right_advert_controller
->load planes_category_content_view

category_bikes_view
->load header_controller
->load top_advert_controller
->load bikes_category_content_view


reuse of code, controller inside view - El Forum - 07-27-2010

[eluser]Met[/eluser]
Use one controller to load the views and return the desired data from a model.

Depending on results of data, show a different category_content-view.

Code:
<?php

class Category extends Controller {

// bla bla

    function show_category($category)
    {      
        $this->load->view('head');
        
        $categoryData = $this->category_model->getCategory($category);

        /* determine what category was selected and load view
        (this way would would but wouldn't be that great) */
        $this->load->view($category . 'content_view', $categoryData);
        
        $this->load->view('head');
    }

}



reuse of code, controller inside view - El Forum - 07-27-2010

[eluser]Unknown[/eluser]
it is not exactly what i meant. Imagine that before

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

i have 40 lines of code how to "head" should look fo this category and i also want to reuse this code.
Also some categories does not contain "head". Entire layout depends on most-outer view, so it would be nice to include controllers inside views. But by "controller" i mean something like snippet or module.


reuse of code, controller inside view - El Forum - 07-27-2010

[eluser]mddd[/eluser]
Why not make a model called 'layout' that contains that information.That would do everything you descibe..
You could make your controller methods like this:
Code:
class Some_controller extends Controller
{
  function some_method()
  {
    // this method has to be shown in 'category_cars' mode..
    $this->layout_model->set_mode('cars');
    // now, the model knows the mode and can return views based on that
    $parts = array();
    $parts['header'] = $this->layout_model->get_header();
    // the layout model loads a view, filled with information, depending on the mode.
    // this can be repeated for other parts
    $this->load->view('main_view', $parts);
  }
}

You could even make it like this:
Code:
class Some_controller extends Controller
{
  function some_method()
  {
    // this method has to be shown in 'category_cars' mode..
    $this->layout_model->set_mode('cars');
    $layout =& $this->layout_model;
    // we set the layout model to the right mode and send a reference to it to the view
    $this->load->view('main_view', compact('layout'));
  }
}

// in the view:

<p>Some normal html code</p>
&lt;!-- get a complete header, based on the mode that we set before --&gt;
&lt;?=$layout->get_header()?&gt;



reuse of code, controller inside view - El Forum - 07-27-2010

[eluser]coffeeandcode[/eluser]
I have a template library with a render() function that loads all the layout views. Each controller method calls $this->template->render() and passes it the content views and any needed data, instead of loading views.