2012-03-09 23 views
1

我需要3個不同的模板用於我的Codeigniter應用程序。我讀過關於主題的圖書館。但我仍然沒有得到任何關於如何添加模板到Codeignier的想法..代碼點燃器主題庫。

我瞭解如何在Controller中包含模板。

請幫

+1

從控制器:'$ this-> load-> view('view_name');' - 更多信息在http://codeigniter.com/user_guide/general/views.html – 2012-03-09 02:00:35

回答

2

我使用這個模板庫,非常簡單,適用於我。

應用/庫/的template.php

<?php 
class Template { 
    var $template_data = array(); 
    var $use_template = ''; 

    /** 
    * Set variable for using in the template 
    */ 
    function set($name, $value) 
    { 
     $this->template_data[$name] = $value; 
    } 

    /** 
    * Set template name 
    */ 
    function set_template($name) 
    { 
     $this->use_template = $name; 
    } 

    /** 
    * Load view 
    */ 
    function load($view = '' , $view_data = array(), $template = '', $return = FALSE) 
    { 
     $this->CI =& get_instance(); 

     if (empty($template)) { 
      $template = $this->CI->config->item('template_master'); 
     } 

     if (!empty($this->use_template)) { 
      $template = $this->use_template; 
     } 

     $this->set($this->CI->config->item('data_container'), $this->CI->load->view($view, array_merge($view_data, array ('template' => $this->template_data)), true)); 
     return $this->CI->load->view($this->CI->config->item('template_folder') . '/' . $template, $this->template_data, $return); 
    } 
} 

應用/配置/的template.php

<?php 
$config['template_master'] = 'main'; 
$config['template_folder'] = 'templates'; 
$config['data_container'] = 'content'; 

應用/視圖/模板/ main.php

Header<br /> 
<?php echo $content; ?></br> 
Footer 

應用/ controllers/welcome.php

<?php 
class Welcome extends CI_Controller 
{ 
    public function index() 
    { 
     $this->load->config('template'); 
     $this->load->library('template'); 
     $this->template->load('welcome', array('view' => 'data')); 
    } 
} 

我通常會將自動加載的配置/庫文件,並且您可以隨時使用$ this-> template-> set_template('other_template');使用另一個:)

希望它有幫助。

+0

感謝您的評論,我是要試試這個..更多疑問...如何在模板文件夾中保存不同的模板?任何特定的命名約定或文件夾結構? – ramesh 2012-03-09 02:18:12

+1

由於圖書館加載模板的方式是使用CI的視圖庫,因此您可以放置​​任何想要的名稱。我習慣於將它們命名爲:main.php,admin.php,unauthorized.php等 – 2012-03-09 02:20:59

+0

好的,謝謝...正如你所說我要在庫文件中保存這個「Template.php」創建一個名爲「 main.php「也與confg變更和所有...之後,對於下一個模板,我需要創建一個名爲」movies.php「的新模板並保存在模板文件夾中,並通過$ this-> template-> load將其加載到控制器中('movies',array('view'=>'data')); ......請問這個工作還是我需要再次做任何confg更改...謝謝 – ramesh 2012-03-09 02:31:50

1

我使用以下設置的笨項目:

與樣式表和圖像沿不同的模板是在以下文件夾:

/templates/1/header.php 
/templates/1/footer.php 
/templates/1/images/* 
/templates/1/style/* 
/templates/2/header.php 
/templates/2/footer.php 
/templates/2/images/* 
/templates/2/style/* 

在控制器確定要加載哪個模板並將該路徑作爲變量(本例中爲templatepath)傳遞到您的視圖文件。視圖文件裏面你做到以下幾點:

<?php include($templatepath.'/header.php'); ?> 

在頂部和

<?php include($templatepath.'/footer.php'); ?> 

在底部。