Static HTML or PHP generated HTML?
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.
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).
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.
For a selection of reasons why a form helper or library might be helpful you can checkout a question I recently posted A PHP and jQuery form creation and validation library available?
The often give you:
- An easy way to validate
- Sometimes client side validation is included
- Form re-usability
- Faster speed of development
精彩评论