2014-02-21 31 views
3

我爲我的web項目使用codeigniter php框架。我的問題是如何爲css,js和圖像等資源設置多個模板佈局和基本路徑?我想設置前端,登錄和後端佈局。所以我需要設置3個佈局。Codeigniter - 如何設置多個模板佈局和資產(CSS,JS和圖像)?

我的項目結構

enter image description here

這些是我的項目的樣本代碼。

MY_Controller.php

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

class My_Controller extends CI_Controller { 
    protected $layout = 'admin'; 

    protected $stylesheets = array(
    'app.css' 
); 
    protected $javascripts = array(
    'app.js' 
); 

    protected function render($content) { 
    $view_data = array(
     'content' => $content, 
     'stylesheets' => $this->get_stylesheets(), 
     'javascripts' => $this->get_javascripts() 
    ); 
    $this->load->view($this->layout,$view_data); 
    } 

    protected function get_stylesheets() { 
    return $this->stylesheets; 
    } 

    protected function get_javascripts() { 
    return $this->javascripts; 
    } 


} 

Home.php

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

class Home extends My_Controller { 
    public function index() { 
    $content = $this->load->view('home/index',null,true); 
    $this->render($content); 
    } 
} 

?> 

我在互聯網上找到此鏈接,但不適合我的項目。 http://williamsconcepts.com/ci/codeigniter/libraries/template/reference.html

+0

我使用williamsconcepts模板,它適用於我。爲什麼不適合你?就是想? – TigerTiger

回答

0

我已經使用這樣的,可以在該樣品將幫助你

用戶控制器(控制器/ user.php的)

<?php 
    class User extends CI_Controller 
    { 
     function displayuser() 
     { 
      $data['users'] = array(1,2,3); 
      $data['inc_page'] = 'display'; // views/display.php page 
      $this->load->view('user_layout', $data); 
     } 
    } 
?> 

顯示視圖(視圖/ Display.php的)

<table border="0" cellspacing="0" cellpadding="3"> 
    <?php 
     foreach($users as $userid) 
     { 
    ?> 
    <tr> 
     <td><?php echo $userid;?></td> 
    </tr> 
    <?php 
     } 
    ?> 
</table> 

User Layou噸(視圖/ user_layout.php)

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
</head> 
<body> 
<?php echo $this->load->view($inc_page); ?> 
</body> 
</html> 
0

可以使用SITE_URL(); 在根中創建一個css文件夾。 讓說這個文件夾被命名爲「css」。

然後讓我們說我們在該文件夾中創建了一個名爲style.css的文件。 所以我們只需要在site_url中調用它。

+1

我相信你正在考慮爲base_url()提供文檔根目錄(例如http:// domain/my/path),而不是'site_url()',它爲尋址控制器生成URLS(即http:// domain /index.php/my/path)。 – Filou