如何在CodeIgniter中加载表格数据的表模板

时间:2012-11-06 02:18:24

标签: codeigniter html-table

我正在尝试在多个页面中重用表库的表模板。我试图将模板保存在* php文件中以便稍后将其称为包含但不起作用。我的代码如下所示:

//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);

我确信必须有一种更好(有效)的方法来实现这一目标。

1 个答案:

答案 0 :(得分: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);