Skip to content

Instantly share code, notes, and snippets.

@mcfdn
Created November 6, 2012 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcfdn/4027123 to your computer and use it in GitHub Desktop.
Save mcfdn/4027123 to your computer and use it in GitHub Desktop.
Table View Helper
<?php
/**
* @author James McFadden <james@jamesmcfadden.co.uk>
*/
class App_View_Helper_RenderTable extends Zend_View_Helper_Abstract
{
/**
* Generate and return the table HTML
*
* @param array $headerDataMap
* @param array $data
* @return string
*/
public function renderTable(array $headerDataMap, array $models)
{
$headers = array_keys($headerDataMap);
$html = '';
$html .= ' <table>
<thead>
<tr>
';
foreach($headers as $header) {
$html .= '<th>' . $header . '</th>';
}
$html .= ' </tr>
</thead>
<tbody>
';
foreach($models as $model) {
$html .= '<tr>';
foreach($headers as $x => $header) {
$classProperty = $headerDataMap[$headers[$x]];
if(is_array($classProperty)) {
$tdValue = $classProperty['Static'];
if(isset($classProperty['Placeholders'])) {
foreach($classProperty['Placeholders'] as $placeholderName => $propertyWanted) {
$placeholderValue = $model->{$classProperty};
$tdValue = str_replace('%' . $placeholderName . '%', $placeholderValue, $tdValue);
}
}
} else {
$tdValue = $model->{$classProperty};
}
$html .= '<td>' . $tdValue . '</td>';
}
$html .= '</tr>';
}
$html .= ' </tbody>
</table>';
return $html;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment