Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To use views or not to use views

I seem right now to be embroiled in a debate with another programmer on this project who thinks that views have no merits. He proposes a system that PHP looks something like this:

$draw = new Draw;
$nav = $draw->wideHeaderBox().
$draw->left().
    $draw->image().
        Image::get($image,60,array('id'=>'header_image')).
    $draw->imageEnd().
$draw->leftEnd().
$draw->left(10).
    '<div id="header_text">'.
        self::defaultSectionText().
    '</div>'.
$draw->leftEnd().

and so on (this is in the controller btw). Now his arguments for this actually make some sense, he claims that if there is a redesign all we need to do is change the HTML in one place and it changes everywhere automatically. For some reason however, this method still rubs me the wrong way, is there any merit to views over this method? I mean besides not having to retype HTML by hand.

like image 774
Eric Scrivner Avatar asked Sep 01 '08 10:09

Eric Scrivner


2 Answers

HTML time-savers are useful, but they're only useful when they're intuitive and easy-to-understand. Having to instantiate a new Draw just doesn't sound very natural. Furthermore, wideHeaderBox and left will only have significance to someone who intimately knows the system. And what if there is a redesign, like your co-worker muses? What if the wideHeaderBox becomes very narrow? Will you change the markup (and styles, presumable) generated by the PHP method but leave a very inaccurate method name to call the code?

If you guys just have to use HTML generation, you should use it interspersed in view files, and you should use it where it's really necessary/useful, such as something like this:

HTML::link("Wikipedia", "http://en.wikipedia.org");
HTML::bulleted_list(array(
    HTML::list_item("Dogs"),
    HTML::list_item("Cats"),
    HTML::list_item("Armadillos")
));

In the above example, the method names actually make sense to people who aren't familiar with your system. They'll also make more sense to you guys when you go back into a seldom-visited file and wonder what the heck you were doing.

like image 82
Brian Warshaw Avatar answered Oct 07 '22 17:10

Brian Warshaw


The argument he uses is the argument you need to have views. Both result in only changing it in one place. However, in his version, you are mixing view markup with business code.

I would suggest using more of a templated design. Do all your business logic in the PHP, setup all variables that are needed by your page. Then just have your page markup reference those variables (and deal with no business logic whatsoever).

Have you looked at smarty? http://smarty.php.net

like image 22
Mark Ingram Avatar answered Oct 07 '22 19:10

Mark Ingram