2017-06-29 24 views
0

我想創建一個模板codeigniter,但我不知道我是否做得對,這是我到目前爲止..如何有人告訴我如果我做對了嗎?我使用codeigniter 3和bootstrap 4 ... web應用程序也應該使用websocket作爲儀表板。我在做這個codeigniter模板嗎?

模板

<div class="menu-wrap"> 
    <?php $this->load->view('landing/menu-wrap'); ?> 
</div> 
<!-- Header Section Start --> 
<header id="video-area" data-stellar-background-ratio="0.5"> 
    <div id="block" data-vide-bg="video/video"></div> 
    <?php $this->load->view('landing/video-area'); ?> 
</header> 
<!-- Features Section End --> 
<section id="services" class="section"> 
    <?php $this->load->view('landing/services'); ?> 
</section> 
<!-- Services Section Start --> 

<!-- Services Section End --> 

<!-- Features Section Start --> 
<section id="features" class="section" data-stellar-background-ratio="0.2"> 
    <?php $this->load->view('landing/features'); ?> 
</section> 
<!-- Start Video promo Section --> 
<section class="video-promo section" data-stellar-background-ratio="0.5"> 
    <div class="overlay"></div> 
    <?php $this->load->view('landing/video-promo'); ?> 
</section> 
<!-- End Video Promo Section --> 

<!-- Portfolio Section --> 
<section id="portfolios" class="section"> 
    <!-- Container Starts --> 
    <?php $this->load->view('landing/portfolios'); ?> 
     <!-- Container Ends --> 
</section> 
<!-- Portfolio Section Ends --> 

<!-- Start Pricing Table Section --> 
<div id="pricing" class="section pricing-section"> 
    <?php $this->load->view('landing/pricing'); ?> 
</div> 
<!-- End Pricing Table Section --> 

<!-- Counter Section Start --> 
<div class="counters section" data-stellar-background-ratio="0.5"> 
    <div class="overlay"></div> 
    <?php $this->load->view('landing/counters'); ?> 
</div> 
<!-- Counter Section End --> 

<!-- testimonial Section Start --> 
<div id="testimonial" class="section"> 
    <?php $this->load->view('landing/testimonial'); ?> 
</div> 
<!-- testimonial Section Start --> 

<!-- Download Section Start --> 
<section id="download" class="section"> 
    <?php $this->load->view('landing/download'); ?> 
</section> 
<!-- Download Section End --> 

<!-- Blog Section --> 
<section id="blog" class="section"> 
    <!-- Container Starts --> 
    <?php $this->load->view('landing/blog'); ?> 
</section> 
<!-- blog Section End --> 

<section id="contact" class="section"> 
    <?php $this->load->view('landing/contact'); ?> 
</section> 
<!-- Contact Section End --> 

<!-- Subcribe Section Start --> 
<div id="subscribe" class="section"> 
    <?php $this->load->view('landing/subscribe'); ?> 
</div> 
<!-- Subcribe Section End --> 
+0

順便說一句,這不是在CodeIgniter中加載模板的正確方法。 –

+0

您可以在您的控制器方法中加載模板,或者您可以爲其製作加載程序。 –

回答

3

CI中使用的模板理想的方式是這樣的:

使您的視圖查看文件夾說的template.php

<html> 
    <head> 
     <meta charset="utf-8"> 
     <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
     <title><?php echo site_name(); ?></title> 
     <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
     .... 
     //Load your css files here 
     .... 
    </head> 
    <body> 
    // All common HTML content like header and all parent div(s) with class like container (is using bootstrap) will go here. 
     <!-- Wrapper Start --> 
     <div class="wrapper"> 
      <div class="structure-row"> 
      <!-- Header --> 
      <?php echo $this->load->view('blocks/header'); ?> 
      <?php echo $content; ?> 
      </div> 
     </div> 
     <!-- Footer --> 
     <?php echo $this->load->view('blocks/footer'); ?> 
    </body> 
</html> 

然後在庫文件夾中創建一個庫文件Template.php

<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 
/** 
* Template 
* 
* Enables the user to load template 
* 
* Usage: 
* 
* Load it within your Controllers: 
* $this->load->library("Template"); 
* 
* Configure CodeIgniter to Auto-Load it: 
* 
* Edit application/config/autoload.php 
* $autoload['libraries'] = array('Template'); 
* 
* 
* Use it in your view files 
* $this->template->view('view', $parameters); 
* 
*/ 
class Template { 

    var $obj; 
    var $template; 

    public function __construct($template = array('template' => 'template')) 
    { 
     $this->obj =& get_instance(); 
     $this->template = $template['template']; 
    } 

    /** 
    * 
    * set_template() 
    * 
    * Sets the template 
    * 
    */ 
    public function set_template($template) 
    { 
     $this->template = $template; 
    } 

    /** 
    * 
    * view() 
    * 
    * Loads the view 
    * 
    */ 
    public function view($view, $data = NULL, $return = FALSE) 
    { 
     $CI = & get_instance(); 

     $loaded_data = array(); 
     $loaded_data['content'] = $this->obj->load->view($view, $data, true); 



     if ($return) 
     { 
      $output = $this->obj->load->view($this->template, $loaded_data, true); 
      return $output; 
     } 
     else 
     { 
      $this->obj->load->view($this->template, $loaded_data, false); 
     } 
    } 

} 

然後在你的config/autoload.php文件加載這樣上面給出的庫:

$autoload['libraries'] = array('database', 'session', 'template'); 

現在終於可以在模板類似這樣的內加載自己的觀點:

$this->template->view('folder_name/view_file_name'); 

希望這會對你有所幫助。如果你需要更多的信息,你可以寫下評論。