How to create Master Page(Layout) with base design style

前端 未结 4 766
逝去的感伤
逝去的感伤 2020-12-09 12:56

I\'m new in CodeIgniter. I want to create Master Page or Layout with base style that will be contain Menu, footer and etc. I don\'t want to write repeating content in all pa

4条回答
  •  猫巷女王i
    2020-12-09 13:18

    Dynamic Layout

    You would create a new Controller with a public $template variable Your extended Controller will then inherit the $template variable from the Master Controller.

    MY_Controller

    class MY_Controller extends CI_Controller
    {
        public $template=null;
    
        public function __construct()
        {
            if(is_null($this->template)){
                $this->template = 'layouts/default';
            }   
        }
    }
    

    Admin_controller

    class Admin_Controller extends MY_Controller
    {
        public function __construct()
        {
            //Still inherits from MY_Controller
            //this time, any controller extending Admin_Controller
            //will point to 'views/layouts/admin'
    
            if(is_null($this->template)){
                $this->template = 'layouts/admin';
            }   
        }
    }
    

    -

    class User extends MY_Controller
    {
        public function index()
        {
            //$this->template is inherited 
            //from MY_Controller
            //which point to 'views/layouts/default'
    
            //We can also load a view as data
            //ie no master layout, INTO our master layout
            //note we don't pass $this->template
            //and set the third param as true
            $dynamic_sidebar = $this->load->view('views/sidebar/dynamic', array(
                'data'  =>  'some_data'
            ), true);
    
            return $this->load->view($this->template, array(
                'partial'   =>  'users/index' //partial view,
                'dynamic_sidebar'   =>  $dynamic_sidebar
            ));
        }
    }
    

    Views/Layouts/default

    
    
        //load the main view
        //in our example we have also loaded
        //a dynamic sidebar with this view
        load->view($partial); ?>
        load->view($dynamic_sidebar); ?>
    
        //load a static view
        //views/sidebar/static
        load->view('sidebar/static'); ?>
    
    
    

提交回复
热议问题