GridHelper - for easy grids in CakePHP
Been busy as hell and I’m afraid it will be like this for another month or so, but here is a quick one I felt like sharing because I hope it will be helpful to some.
Haven’t you ever wished to only have to write something like this in your views:
e($grid->create($results));
Note: It’s still unfinished and I know I will have lots of revisions to it while working on this app, so if you have any suggestions/critics or better ways of doing it, please let me know.
/app/views/helpers/grid.php:
/**
* GridHelper - easily creating grids
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @author Jad Bitar
* @copyright Copyright 2007, loudbaking.com
*/
class GridHelper extends AppHelper
{
/**
* Field/Title inflections
*
* @var array
* @access public
*/
var $inflections = array(
'commission' => 'Earned',
'created' => 'Created on',
'email' => 'Email address',
'ip' => 'IP address',
'is_active' => 'Status',
'joined' => 'Date',
'ref_code' => 'Code',
'url' => 'URL address',
);
/**
* Defines how to format values in cells
*
* @var array
* @access public
*/
var $formats = array(
'date' => 'Y-m-d',
'currency' => 'USD',
'is_active' => array('Not active', 'Active'),
);
/**
* Dependent on the following helpers
*
* @var array
* @access public
*/
var $helpers = array('Html', 'Number', 'String', 'Time');
/**
* Patterns to use when creating the HTML
*
* @var array
* @access public
*/
var $tags = array(
'table' => '
As you can see from this code, it’s still pretty simplistic but the idea is to completely separate resultset preparation from rendering re-enforcing the fat models, skinny controllers by adding skinny views (maybe?).
If you think it could help you at the stage it is at, copy it here - WP seems to eat chunks of it when saving.
Trackbacks
Use this link to trackback from your own site.


I think to make this helper really reusable you have to get rid of the hard-coded column names. But at the moment I don’t have an idea how to do that…
Beautiful JadB, I was just thinking about putting together something similar.
@all: Thanks for dropping by.
@Daniel: What you mean by ‘getting rid of the col names’?
@Jad: See the _format() method, if I want to use this helper I would have to modify this method as the names used there are specific to your application. I hope you see what I mean
@Daniel: Ah! now I see what you mean.. Ya, I thought of that but haven’t tried implementing the solution yet. I believe callback methods like AppHelper::gridFieldName() would work great for that - will keep you guys updated, I have to finish it soon anyhow.