Codeigniter: Best way to structure partial views

后端 未结 7 1915
無奈伤痛
無奈伤痛 2020-11-29 19:39

How would you structure the below page in Codeigniter?

\"alt

I thought about creating seperate c

相关标签:
7条回答
  • 2020-11-29 20:13

    I like what Phil Sturgeon mentioned. Although it's considered as very complicated i really liked template structure that magento have.

    Inspired with that way of structuring i made my logic, (which is not great at all but it's simple as it can be,. and perhaps maybe i could override ->view loader and make it accept some kind of object as a template name and than load the structure as required)

    first: This approach must be used very responsibly (you mus prepare data in controller/method that your templates require!

    second: Template needs to be prepared and structured properly.

    This is what i do:

    • in every controller i have attribute of Array type, something like this:

      class Main extends CI_Controller {
      
      public $view = Array(
              'theend' => 'frontend',
              'layout' => '1column',
              'mainbar' => array('content','next template file loaded under'),
              'sidebar' => array('generic','next template file loaded under'),
              'content' => '',
      );
      
    • In every method for which i want to use previous structure, and if i want to change it a bit i write it like this:

      public function index()
      {
      $data['view'] = $this->view;  // i take/load global class's attribute
      $data['view']['mainbar'] = Array('archive','related_posts'); // i change mainbar part of it
      // i add/load data that i need in all those templates that are needed $data['view'] also my using same Array  $data['my_required_data_that_i_use_in_template_files'] = 1;
      $this->load->view('main',$data); //
      }
      

    third In /application/view folder i have structure like

    /view/main.php <-- which basically just determines which side's wrapper of web to load (frontend or backend or some other)
    
    /view/frontend/wrapper.php
    
    /view/backend/wrapper.php
    
    /view/mobile/wrapper.php   <-- this wrappers are again another level of structuring for ex:
    
    /view/backend/layouts/   <-- inside i have templates different layouts like 1column.php 2columns-left (have left side is narrow one),2columns-right,3columns... etc...
    
    /view/backend/mainbar/   <-- inside i have templates for mainbar in pages
    
    /view/backend/mainbar/.../ <-- in the same way it's possible to add folders for easily grouping templates for example for posts so you add for example
    
        /view/backend/mainbar/posts/  <-- all templates for creating, editing etc posts... 
    
        /view/backend/sidebar/   <-- inside i have templates for sidebar in pages
    
        /view/backend/...other special cases.... like dashboard.php
    

    forth file in /app/view/main.php looks something like:

    if ($view['theend'] == "frontend")
    {
    $this->load->view('/frontend/wrapper');
    } elseif ($view['theend'] == "backend")
    {
    $this->load->view('/backend/wrapper');
    }
    

    fifth wrapper is simple some php in structured HTML where you have head (loading html headers, title etc...) header/headers (loading in headers if there are any in passed $data['view']['headers'] variable/array) layout (loads in layout file that simply have new html structured file with next level of loading files) footer/footers (loading in footers if there are any in passed $data['view']['footers'] variable) scripts (loading inscripts like analytics/facebook scripts just before tag)

    sixth So in the same way, layout would be also loading in mainbar/sidebar content that is specified in public $view = Array(....)

    If i need in some method, i simply override part of public $view = Array(...) attribute, and i override just part that is different.

    it's done something like this:

    public function index()
    {
        $data['view'] = $this->view;  // i take/load global class's attribute
        $data['view']['mainbar'] = Array('archive','related_posts'); // i change mainbar part of it
    // i add/load data that i need in all those templates that are needed $data['view'] also my using same Array  $data['my_required_data_that_i_use_in_template_files'] = 1;
        $this->load->view('main',$data); //
    }
    

    FINALLY Load goes like this:

    1. $this->load->view('main',$data); <-- Loads /app/view/main.php and passes $data $data has node 'view' ($data['view']) and in it it has sub nodes which determines other important things like: what is the end, what layout, what headers, what footers etc...

    2. Using defined data in $data['view']['theend'] it loads proper wrapper

    3. Again using data in $data['view']['layout'] further in wrapper it loads other, deeper structures like layout...
    4. layout, uses same $data['view']['mainbar'], $data['view']['sidebar'] and catch other important parts to load like mainbar templates, sidebar templates ...

    That is about it...

    p.s. I'm so sorry for not using numbers, but stackoverflow system is so strange that instead of showing 3. it shows me 1.. as you see i had some nested lists...

    0 讨论(0)
提交回复
热议问题