How to Deal With Codeigniter Templates?

前端 未结 12 1886
梦谈多话
梦谈多话 2020-12-08 01:02

I\'m fairly new to MVC, and I\'ve found CodeIgniter recently. I\'m still learning everyday, but one problem is its template engine. What is the best way to create templates

12条回答
  •  悲&欢浪女
    2020-12-08 01:45

    I have two primary templates; one for the site and one for our admin panel. Here is my setup for our main site (mostly static)... I decided on one controller called site It calls the template file and each page and gets its view file.

    Why doesn't anyone mention template engine use? Are -just- views better/faster?

    • In config/template.php I defined the template(s). Note *site_template* is in the views folder:

      $template['site']['template'] = 'site_template';
      $template['site']['regions'] = array('title','section','col2','content',);
      $template['site']['parser'] = 'parser';
      $template['site']['parser_method'] = 'parse';
      $template['site']['parse_template'] = FALSE;
      
    • In config/routers.php I setup rules to handle the requests for the site controller which are single segments urls mostly but we do have one section that is structured as such; /who-we-are and then for selected people /who-we-are/robert-wayne and so:

      $route['what-we-do'] = 'site/what_we_do';
      $route['who-we-are'] = 'site/who_we_are';
      $route['who-we-are/(:any)'] = "site/who_we_are/$1"
      
    • And controllers/site.php Again with a function for each page/section:

      class Site extends CI_Controller
      {
      function __construct() {
          parent::__construct();
          $this->template->set_template('site'); // ask for the site template
          $this->load->library('mobile');
      }
      public function index()
      {
      $data = array('section' => 'home');
      $this->template->write_view('col2', 'site/menu.php', $data);
      $this->template->write('title', "COOL PAGE TITLE", TRUE);
      $this->template->write('section', $data['section'], TRUE);
      $this->template->write_view('content', 'site/welcome', $data);
      $this->template->render();
      }
      public function who_we_are()
      {
      // this bit readies the second segment.
      $slug = str_replace('-', '_', $this->uri->segment(2, 0));
      if($slug) // IF there is a second segment we load the person.
      {
      $data['bio'] = $this->load->view('site/people/'.$slug, '', true)
      } else {
      // where it loads the general view who_we_are
      }
      // and so on for each page...
      

    and as fine point notice the router lets us leave out `/site/' in the url, http://the site.com/who-we-are

    thoughts? anyone? bueller?

提交回复
热议问题