Initial Thoughts
Initially I was thinking of suggesting a view class that could hold the states and weekdays in instance variables, then have methods to get the header for each state and then display the cells for each weekday, but that seems like a lot of extra overhead just to display a table, and wouldn't provide the desired separation of business logic and display templates.
Simple improvements
As GrumpyCrouton mention in the SO answer, PHP Short echo tags could be used to shorten the inline echo statements (i.e. <?php echo
to <?=
).
$d
could be eliminated by using the associative array syntax of foreach
foreach (array_expression as $key => $value)
statement
1
foreach($weekdays as $d => $weekday) {
//$d doesn't need to be incremented since it is the keys - e.g. 0, 1, 2, etc.
}
Or just used array_keys() since the weekday value isn't used...
foreach(array_keys($weekdays) as $d) {
//$d doesn't need to be incremented since it is the keys - e.g. 0, 1, 2, etc.
}
One could define a template using Heredoc syntax, like:
$cellTemplate = <<<CELL
<td>
<input name="date-%s" />
</td>
CELL;
Then that could be used for each table cell:
foreach ( $states as $state ) { ?>
<tr class="<?= $state; ?>">
<th><?= $state; ?></th>
<?php
foreach ( $weekdays as $d => $weekday ) {
$dateName = $date->addDay( $d )->format('[Y][m][d]') . '[' . $state . ']';
echo sprintf($cellTemplate, $dateName);
}
?>
</tr>
<?php
}
?>
A demonstration can be seen here. Note that instead of using the Carbon DateTime I am using PHP's DateTime class along with DateInterval to accomplish the date addition with DateTime::add().
Template Engines
One could also utilize a template engine, like Smarty, Symfony’s Twig, Laravel’s Blade, etc.
Here is an example with a Smarty template. I like bumperbox's notion of storing the formatted weekdays in an array and using that in later on. That array could also be generated using array_map() (though accessing $date
inside the callback would require a use
statement):
$formattedWeekdays = array_map(function($d, $weekday) use ($date) {
return $date->addDay( $d )->format('[Y][m][d]');
}, array_keys($weekdays), $weekdays);
$smarty->assign('states', $states);
$smarty->assign('formattedWeekdays', $formattedWeekdays);
$smarty->display('../templates/weekdays.tpl');
And that template file (i.e. weekdays.tpl) might look like this:
<table>
{foreach $states as $state}
<tr class="{$state}">
<th>{$state}</th>
{foreach $formattedWeekdays as $formattedWeekday }
<td>
<input name="date-{$formattedWeekday}[{$state}]" />
</td>
{/foreach}
</tr>
{/foreach}
</table>
That way the display logic is separate from the business logic. As this wikibooks page alludes to, the HTML is separated from the PHP code so multiple people (e.g. graphics designer, developers, etc.) can manage the separate parts since they are de-coupled.
1http://php.net/manual/en/control-structures.foreach.php
echo date->addDay
- is that supposed to beecho $date->addDay
? if so, where does$date
come from? is it a Carbon DateTime object, a Zend_Date object, or something else? \$\endgroup\$