[eluser]PhilTem[/eluser]
I just made a nice little snippet anyone can use within the MY_Controller at actually any place and can call it from any controller extending the MY_Controller class
Code:
//---------------------------------------------------------------
/**
* Easy wrapper to load the table-library and to set the template
*
* @access protected
*
* @return void
*/
protected function load_table_lib()
{
// Load the table-library
$this->load->library('table');
// Let's get our possible locations here
$possible_locations = array();
// First, look within the module under <module>/views/table/controller/method.php
if ( $this->_module )
{
// Let's append the module-name to the path
$path = APPPATH . 'modules/' . $this->_module . '/';
$tmp = array(
$path . 'views/layouts/table/' . $this->_controller . '/' . $this->_method . EXT,
$path . 'views/layouts/table/' . $this->_controller . EXT,
$path . 'views/layouts/table/default' . EXT,
$path . 'views/layouts/table' . EXT
);
$possible_locations = array_merge($possible_locations, $tmp);
}
// Define an array with all possible locations for the table-template
$tmp = array(
APPPATH . 'views/layouts/table/' . $this->_controller . '/' . $this->_method . EXT,
APPPATH . 'views/layouts/table/' . $this->_controller . EXT,
APPPATH . 'views/layouts/table/default' . EXT,
APPPATH . 'views/layouts/table' . EXT
);
// Merge the basic locations into the possible locations
$possible_locations = array_merge($possible_locations, $tmp);
// Loop over every possible file-location and see if it is a file
foreach ( $possible_locations as $filename )
{
// Let's check if we found any file
if ( is_file($filename) )
{
// Include it so we get the variable in
include($filename);
// Let's check the right variable was set inside the file
if ( ! isset($template) OR ! is_array($template) )
{
// Not correctly
show_error('Trying to set a table-template but the template is not set correctly');
// Break the script from futher proceeding
return NULL;
}
// Let's set the template given
$this->table->set_template($template);
// Just brake the loop so we won't do anything else
return NULL;
}
}
// Let's define a simple default template here
$template = array (
'table_open' => '<table class="table">',
'heading_row_start' => '<tr class="odd">',
'heading_row_end' => '</tr>',
'heading_cell_start' => '<th>',
'heading_cell_end' => '</th>',
'row_start' => '<tr class="even">',
'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>'
);
// And set it
$this->table->set_template($template);
return NULL;
}
I guess/hope it's obvious what the code is doing (thanks to me actually commenting my lines): Once you call $this->load_table_lib() within any of your controller's methods the script is looking for a table-template which can be defined in multiple places:
Either within your module (in case your using HMVC and have your modules stored under ./application/modules/*) or within the default views-folder.
But, you'll need at least a 'layouts' folder within the 'views'-folder.
So, e.g. you have something like this
Code:
./application
modules/
users/
views/
layouts/
table/ <= default location folder for table-layouts
login/ <= controller-name
index.php <= method-name
table.php <= used within any other controller/method despite login/index
views/
layouts/
table.php <= used on any other controller despite all controllers in module 'users'
And within your file, e.g. login/index.php you simply write
Code:
$template = array (
'table_open' => '<table class="table">',
'heading_row_start' => '<tr class="odd">',
'heading_row_end' => '</tr>',
'heading_cell_start' => '<th>',
'heading_cell_end' => '</th>',
'row_start' => '<tr class="even">',
'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>'
);
That's the default table-template you can find in the user_guide. But you need the variable to be named $template and to be an array (otherwise there will be an error shown)
Hope you like (and maybe even use it) and if you have any questions or comments on speed-up or anything else, let me know
[edit]
Just two remarks:
- It should be fully non-HMVC compatible without changing the code (at least I hope so

)
- Just to avoid questions: Place it somewhere in MY_Controller.php located under /core