Codeigniter: Best way to structure partial views

后端 未结 7 1938
無奈伤痛
無奈伤痛 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 19:59

    @Reinis answer probably hit the spot correctly for older versions of CI less than 2.0 however alot has changed since then, so I thought I'd answer this question with an up to date method of what I've done.

    Most of it is similar to @Reinis method and also described here:http://codeigniter.com/wiki/MY_Controller_-_how_to_extend_the_CI_Controller

    However here are the updates ive done:

    Step 1: Create a MY_Controller.php file and store it in /application/core

    Step 2: In your MY_Controller.php file put in the following contents:

    load->view('base', $data, true));
        }
    
    }
    

    Step 3: Create a sample controller to base off of MY_Controller.php, in this case I will create a welcome.php controller inside of application/controllers/ with the following content:

    load->view('welcome_message');
        }
    
    }
    

    Once you have these controllers set, do the following:

    Step 4: Create a base view inside of /application/views and name the file base.php, the content of the file should be similar to this:

    
    
    
    
        
            
            
             
            
        
        
            
    load->view('shared/scripts.php'); ?>

    Step 5: Create another view in /application/views and name this view welcome_message.php, the content of this file will be:

    Welcome

    Once, all this is complete, you should see the following output:

    
    
    
    
        
            
            
             
            
        
        
            
            

    Welcome

As you can see

Welcome

was put into the base template.

Resources:

  • Obviously @Reinis initial response
  • http://codeigniter.com/wiki/Extending_Controller_with_masterpage_template_funtionality

Hope this helps anyone else coming across this technique.

提交回复
热议问题