2016-11-09 52 views
0

它是我使用CodeIgniter的第一個項目,並不像看起來那麼容易。什麼是使用CodeIgniter導入js和css的最佳方式?

我必須導入不同的JS和CSS在不同的頁面,我卡住了。

首先,我已經看到了硬編碼回聲都沒有這樣做,所以我做了一個簡單的類象

<?php 
    defined('BASEPATH') OR exit('No direct script access allowed'); 

    class Fileload { 

      public function loadjs($filename) 
      { 
       echo '<script language="javascript" type="text/javascript" src="'.$filename.'"></script>'; 
      } 
      public function loadcss($filename) 
      { 
       echo '<link rel="stylesheet" type="text/css" href="'.$filename.'" >'; 
      } 
    } 
    ?> 

而在我的控制,我用它像

<?php 
defined('BASEPATH') OR exit('No direct script access allowed'); 

class Main extends CI_Controller { 

    /** 
    * Index Page for this controller. 
    * 
    * Maps to the following URL 
    *  http://example.com/index.php/welcome 
    * - or - 
    *  http://example.com/index.php/welcome/index 
    * - or - 
    * Since this controller is set as the default controller in 
    * config/routes.php, it's displayed at http://example.com/ 
    * 
    * So any other public methods not prefixed with an underscore will 
    * map to /index.php/welcome/<method_name> 
    * @see https://codeigniter.com/user_guide/general/urls.html 
    */ 
    public function index() 
    { 
     $this->load->library('fileload'); 

     $this->load->view('head'); 
     $this->load->view('mainpage'); 
     $this->fileload->loadjs('//cdn.jsdelivr.net/jquery.slick/1.6.0/slick.min.js'); 
     $this->load->view('tail'); 
    } 
} 
的CI方式

但是,應該位於'尾部'上方右下角的光滑圖書館位於head>標籤的頂部,這是內部視圖('head');

似乎控制器的方法沒有按照我寫下來的順序運行。它應該先回應腳本文件。

任何人都可以解釋這個CodeIgniter控制器是如何工作的?

+0

創建一個'header.php'(或其他類似的),在其中寫下'

0

您可以做的另一件事是將刀片模板解析器放入CodeIgniter中。 Blade將允許創建部分,並且以這種方式,包括特定於頁面的腳本將變得更容易/更乾淨。 https://github.com/PhiloNL/Laravel-Blade是你可以得到它的地方。注意你也必須爲此使用作曲家。

相關問題