Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static HTML or PHP generated HTML?

Tags:

html

php

class

I've been reading a lot of PHP tutorials lately, and I keep seeing examples of "helper classes", which are mostly used to generate typical HTML components. Forms seems to be popular.

I can accept that this would probably make the code a little clearer, as I am all too familiar with the "klunky-ness" of meshing html with php. Is there any "other" reason? Some kind of performance benefit? I can see why it would be nice if your website was going to have 100 forms in various places, but if you only have one or two it seems like more work than is necessary.

like image 982
zdkroot Avatar asked Mar 30 '11 13:03

zdkroot


2 Answers

Ease of use and rapid development come to mind. It would actually be slower performance generating HTML with PHP, but I doubt it would ever become the bottleneck of your application (and you could cache most of the HTML output anyway).

For example, which of these looks easier to implement...

<?php 
 $name = 'abc';
 $options = array('a', 'b', 'c');
 $selected = 2;
?>

Example 1

<?php echo form::select($name, $options, $selected); ?>

Example 2

<select name="<?php echo $name; ?>">
<?php foreach($options as $value => $node): ?>
   <option
     value="<?php echo $value; ?>"
     <?php echo ($selected === $index) ? ' selected="selected"' : NULL; ?>
   >
   <?php echo $node; ?>
   </option>
<?php endforeach; ?>
</select>

I think you will find the first example much easier to read, less to type and guaranteed to work every time (so long as you coded it correctly).


Also, if your boss says...

Hey zdkroot, we now need to make all password input elements have a red border in IE6.

...you might think to yourself...

I know, I'll use input[type="password"] { ... }. Oh wait, IE6 doesn't support that?

...so you can go into your input generating functions and add a class there which you can reference in your CSS. It will also make changing from /> to > a synch too (if you decide to).

like image 82
alex Avatar answered Oct 03 '22 17:10

alex


Consider the example you gave - a form builder.

The thing with a form is that you need more than just the HTML. You also need to map those input fields to variables, you need to validate them, you need to save them to a database, you need to pre-populate them with data from the DB.

A good form builder class will handle most of these. If you built the HTML code manually, you'd still be able to modify that easily enough, but you'd have to write your own validation and processing routines.

That's the benefit of using a pre-built class.

There are down-sides as well, of course. Typically, a class like this will make it very easy to create a form that meets the design criteria of the people who created the class. But if your requirements are slightly different to that, it can be quite tricky to force it to do what you want.

Most of the time it does work well, though.

like image 22
Spudley Avatar answered Oct 03 '22 18:10

Spudley