简体   繁体   中英

Codeigniter template libraries editing

I found this great link for beginners' templating in Codeigniter:

http://net.tutsplus.com/tutorials/php/an-introduction-to-views-templating-in-codeigniter/

Genuinely, this is the first time I've been able to follow a tutorial on this subject step-by-step and somewhat understand the logic.

However, while it allows me to specify a template (eg. default) it limits me in having to have ALL of the HTML body code in that content view file. I want to split it up as header, main-content, optional sidebar and footer.

Some of my web pages are three-column (with a left-sidebar and right-sidebar), some are two-column (with the same right-sidebar) and some are one-column (no sidebars) - hence my idea was to have 3 corresponding templates - but I would then have to repeat the code for my right-sidebar in many different view files, which will make it more difficult to edit down the line - and ultimately defeat the purpose of having a template system.

Would it be possible to achieve what I want by editing the existing code, or is there another layout/template library someone could recommend to me?

PS. I have thought about the fact that I can just edit the following lines in the library file to achieve a loaded header and footer, but the body code and optional sidebar is my main roadblock as I can't load a sidebar here without it applying to all templates - including a one-column template.

   $this->ci->load->view('header_view, $data);
   $this->ci->load->view('templates/'.$tpl_view, $data);
   $this->ci->load->view('footer_view, $data);

hi just create your sidebar as view and pass it in you layout views here i have create a simple layout system have look on it

First create MY_Controller under application/core directory copy the following code

class MY_Controller extends CI_Controller{

//all you default views
public $layout = 'default';
public $header = 'default_heder';
public $right_sidebar = 'default_right_sidebar';
public $left_sidebar = 'default_left_sidebar';
public $footer = 'default_footer';

public function __construct() {
        parent::__construct();
}

public build($view,$data = array()){

       $layout_data = array();
       $layout_data['header'] = $this->load->view($this->header,$data,TRUE);
       $layout_data['right_sidebar'] = $this->load->view($this->right_sidebar,$data,TRUE);
       $layout_data['left_sidebar'] = $this->load->view($this->left_sidebar,$data,TRUE);
       $layout_data['footer'] = $this->load->view($this->footer,$data,TRUE);
       $layout_data['body_content'] = $this->load->view($view,$data,TRUE);

       $this->load->view($this->layout,$layout_data);

}

}

2nd create any controller and extend it with MY_Controller i have created example page controller

class Page extends MY_Controller{

public function __construct() {
        parent::__construct();
}

public index(){
          $data['content'] = 'content here';
          $this->build('page_view',$data);
}

public one_coulmn(){
          $this->layout = 'one_column'; // change layout view
          $this->right_sidebar = 'inner_right_sidebar'; // change sidebar view
          $data['content'] = 'content here';
          $this->build('page_view',$data);
}

}

here is example of your 3 column view

// three col layout
<div id="header"><? echo $header ?></div>
<div id="left_sidebar"><? echo $left_sidebar ?></div>
<div id="body_content"><? echo $body_content?></div>
<div id="right_sidebar"><? echo $right_sidebar ?></div>
<div id="footer"><? echo $footer ?></div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM