Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this overkill, or good use of CakePHP's HTML helper?

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.

like image 298
Stephen Avatar asked Jul 13 '10 00:07

Stephen


4 Answers

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?

like image 182
Leo Avatar answered Sep 21 '22 13:09

Leo


The undeniable benefit of this is 100% correct syntax, since you've removed any possibility of fat-fingering and missing open/closing tags. What I can tell you from experience though is that half a year from now, it will be twice as hard to read and modify this structure. It is also really hard to insert conditional elements. You'd need to resort to the ternary operator here, which makes things even less readable.

Overall, I'd recommend to go with a traditional HTML/PHP mix.

like image 24
deceze Avatar answered Sep 19 '22 13:09

deceze


Personally I am conflicted about this, but I choose the HTML+PHP mode when working with PHP. I can see the advantages of either, but this is why I choose HTML+PHP:

  1. PHP is a templating language - the best. As a templating language, I feel it is far superior to any other PHP templating language, and many of the templating languages in other language web frameworks for it's flexibility and power.

    If I were working with a language like Python or Java, I'd most probably prefer the form you suggest - but it's just not the perfect solution when working with PHP.

  2. You lose the ability to use the many tools already developed for working with HTML itself. You especially lose the people who are comfortable with modifying HTML and who would be able to otherwise make simple changes in the views themselves.

  3. Absolute flexibility without adding more layers of API.

As a side effect of this, I tend to use the for(): and endfor; syntax, etc, and strive to never echo HTML tags - I'll restructure to avoid it (ie, not using methods unless inside a helper etc, in which case I'll generate my tags with the Html Helper, because it's just dumb looking to have HTML soup inside of a PHP class or function :P).

like image 32
Iiridayn Avatar answered Sep 23 '22 13:09

Iiridayn


By using the helpers you are, in a way, future proofing your code. So when HTML5 comes along and the html or head tags change in the new spec. Theoretically you just change your html helper class and all your markup is HTML5.

However, on the contrary, you are also relying on Cake to generate well formed tags. Although a lot of these frameworks are full stack, there are inevitably some areas they handle better than others. You should not expect them to facility the entire HTML tag set.

I personally think it's overkill to do what you have done. I like using the HTML helpers for links, urls, and included files because of the directory mapping benefits. But I don't use the helpers to generate a simple div tag.

like image 41
Jason McCreary Avatar answered Sep 21 '22 13:09

Jason McCreary