I just reformatted the default layout of my CakePHP application. I eliminated as much in-line html as possible by putting almost everything inside the html helper methods.
It was fun, but I’m wondering what benefit I’ve gained from this exercise, if any?
<?php
$output = implode("\n", array(
$html->docType(),
$html->tag('html', implode("\n", array(
$html->tag('head', implode("\n", array(
$html->charset(),
$html->tag('title', 'Title For App'),
$html->css('css', NULL, array('media' => 'screen,print')),
$html->css('print', NULL, array('media' => 'print')),
$html->script(array('cufon', 'jquery','external'))
))),
$html->tag('body', implode("\n", array(
$html->tag('div', $content_for_layout, array('id' => 'wrapper')),
$html->scriptBlock('Cufon.now();')
)))
)), array('xmlns' => 'http://www.w3.org/1999/xhtml'))
));
echo $output;
?>
I suppose at least it looks nice and compact, and is pretty readable. What pitfalls should I be aware of in this scenario? Should I be aware of any speed issues?
I like it — and I don’t.
I guess I need convincing one way or the other.
If you’re wondering, the implodes put nice line breaks in the html when viewing the source.
I had this discussion in the Google group some years back. Eventually, you’ll realise that it doesn’t make a lot of difference which way you do it until you need to programatically manipulate stuff – then, if you went the HTML route, you’ll find your code peppered with
<?php&?>or string concatenations or double quote variable substitutions.Now, many applications down the line, I prefer maintaining the ones with more helper than markup.
There is a lot of HTML that isn’t covered by helpers, so you can’t avoid a mix, but you can minimise complexity and confusion by using helpers wherever possible. When you start using forms, you get a lot of security stuff thrown in and IDs and NAMEs formatted the way CakePHP prefers.
PHP and CakePHP are built for this. Why only use half a language or half a framework?