2012-11-06 53 views
0

我試圖在多個頁面中重用表格模板。我試圖將模板保存在* php文件中,稍後將其作爲include包含,但這不起作用。我的代碼如下所示:如何在CodeIgniter中加載表格數據的表格模板

//on the controller 
$this->load->view('includes/tabularDataTemplate.php'); 

//this is the contents of the tabularDataTemplate.php file  
$tmpl = array (
    'table_open'   => '<table border="1" cellpadding="4" cellspacing="0">', 

    'heading_row_start' => '<tr>', 
    'heading_row_end'  => '</tr>', 
    'heading_cell_start' => '<th>', 
    'heading_cell_end' => '</th>', 

    'row_start'   => '<tr>', 
    'row_end'    => '</tr>', 
    'cell_start'   => '<td>', 
    'cell_end'   => '</td>', 

    'row_alt_start'  => '<tr>', 
    'row_alt_end'   => '</tr>', 
    'cell_alt_start'  => '<td>', 
    'cell_alt_end'  => '</td>', 

    'table_close'   => '</table>' 
    ); 

$this->table->set_template($tmpl); 

我相信必須有一個更好的(工作)的方式來實現這一目標。

回答

0

我還沒有嘗試過了,但你可以做這樣的事情:

//on the controller 
$tmpl = $this->load->view('includes/tabularDataTemplate.php','',TRUE); 
    //This will return view file as string. 

//this is the contents of the tabularDataTemplate.php file  
array (
    'table_open'   => '<table border="1" cellpadding="4" cellspacing="0">', 

    'heading_row_start' => '<tr>', 
    'heading_row_end'  => '</tr>', 
    'heading_cell_start' => '<th>', 
    'heading_cell_end' => '</th>', 

    'row_start'   => '<tr>', 
    'row_end'    => '</tr>', 
    'cell_start'   => '<td>', 
    'cell_end'   => '</td>', 

    'row_alt_start'  => '<tr>', 
    'row_alt_end'   => '</tr>', 
    'cell_alt_start'  => '<td>', 
    'cell_alt_end'  => '</td>', 

    'table_close'   => '</table>' 
    ); 

$this->table->set_template($tmpl); 
+0

謝謝,但我得到一個錯誤「無法加載所需的文件:包括/ tabularDataTemplate.php」 – user1765388

+0

有你檢查文件路徑? –

+0

是的,文件路徑似乎很好。 – user1765388

相關問題