2013-01-03 38 views
1

我是Codeigniter的新手。你如何整合模板?喜歡的東西:如何將模板集成到Codeigniter中?

header_template.php等等

現在,我不喜歡這樣寫道:

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

class Page extends CI_Controller { 

    public function index() 
    { 
     $this->load->view('head_template.php'); 
     $this->load->view('header_template.php'); 
     $this->load->view('navigation_template.php'); 
     $this->load->view('page_view.php'); 
     $this->load->view('footer_template.php'); 

    } 
} 

雖然這是好的,必須有一個更好的辦法。我必須將其包含在每個控制器中,這有點嚇人。

我知道模板引擎,但它不是我正在尋找的。另外,它說它在Codeigniter文檔中速度很慢。

回答

0

由先前的評論者列出的模板引擎是好的,但不會在一段時間內更新,可能會損害您的目標。

雖然這可能工作,我相信this very simple layout library是你在找什麼。

這是非常非常基本的,但完成工作。過去我擴展了它以輕鬆地允許多個「內容部分」,但我通常使用它來快速獲取html頁眉和頁腳。

1
public function index() 
{ 
    $data["header"]  = $this->load->view('head_template.php',"",true); 
    $data["navigation"] = $this->load->view('navigation_template.php',"",true); 
    $data["footer"] = $this->load->view('footer_template.php',"",true); 
    $this->load->view('page_view.php', $data, false); 
} 

你的 「page_view.php」

<html> 
<body> 
<?php 
    echo $header; 
    echo $navigation; 
    echo $footer; 
?> 
</body> 
</html> 

你可以在-http找到更多的信息://www.codeignitor.com/user_guide/general/views.html

代碼僅包含用於模板包含的示例 -

class Template extends CI_Controller{ 

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

/** 
* TODO: Get the template from database or some configuration file 
* 
* 1) Get Template hook 
* 2) Get Header 
* 3) Get Footer 
* 4) Get other hooks 
*/ 
public function loadTemplate($viewName, $headerData = "", 
          $viewData="", $footerData=""){ 
    $headerData["userId"] = (is_numeric($this->CI->session->userdata("userId"))) 
          ? $this->CI->session->userdata("userId") : null;        
    $this->CI->load->view('header/header', $headerData); 
    $this->CI->load->view($viewName, $viewData); 
    $this->CI->load->view('footer/footer', $footerData); 
} 
} 

//模板類以更多代碼結尾

// Login.php that extends template class 
class Login extends Template { 
    public function Login() { 
    parent :: __construct(); 
} 

    public function getUserDetails(){ 
    $userDetails = $this->loadTemplate("myDataNeedToshow"); 

} 
} 
+0

但我仍然必須包括在每個控制器... – Rasteril

+0

對於這個問題,你需要擴展CI_Controller類或其他方式創建你的自己的類擴展CI_Controller類,並擴展你的每個控制器與擴展類。如果你想我可以給你一個例子。 –

+0

如果可能,我將不勝感激。 – Rasteril

0

我要做的就是有一個在看起來像這樣的意見,文件夾名爲template.php文件中:

views/template.php: 
<?= $this->load->view('header_view');?> 
<?= $this->load->view($load_page);?> 
<?= $this->load->view('footer_view');?> 

然後在控制器我這樣稱呼它:

頁。 PHP:

$page = array(
     'meta_title' => 'Register Package', 
     'load_page' => 'package_view' 
     ); 
     $this->load->view('template', $page); 

我敢肯定有一個更好的方式,但我會考慮它,當我得到的時間

+0

哇,這很簡單,正是我所需要的。通過一些定製,我會實現它。 – Rasteril

相關問題