Just in case you haven’t read the previous part, where I started incorporating a basic HTML form helper class into this example framework, below I listed the partial source code belonging to this class. In this way you can quickly learn how it does its business. Essentially, the form helper was initially created in the following manner: class Form { private static $instance = NULL;
// get Singleton instance of Form class public static function getInstance() { if (self::$instance === NULL) { self::$instance = new self; } return self::$instance; }
// render <form> opening tag public static function open(array $attributes) { $html = '<form'; if (!empty($attributes)) { foreach ($attributes as $attribute => $value) { if (in_array($attribute, array('action', 'method', 'id', 'class', 'enctype')) and !empty($value)) { // assign default value to 'method' attribute if ($attribute === 'method' and ($value !== 'post' or $value !== 'get')) { $value = 'post'; } $html .= ' ' . $attribute . '="' . $value . '"'; } } } return $html . '>'; }
// render <input> tag public static function input(array $attributes) { $html = '<input'; if (!empty($attributes)) { foreach ($attributes as $attribute => $value) { if (in_array($attribute, array('type', 'id', 'class', 'name', 'value')) and !empty($value)) { $html .= ' ' . $attribute . '="' . $value . '"'; } } } return $html . '>'; }
// render </form> closing tag public static function close() { return '</form>'; } }// End Form class Unquestionably, understanding the underlying logic of the above “Form” helper class isn’t rocket science, is it? As you can see, the helper implements only three static methods, where two of them are aimed at rendering the opening and closing tags of a regular HTML form, and the remaining one simply constructs input-type elements. Finally, I decided to add an additional method within the class, which permits it to return a Singleton instance of the helper -- but this process is entirely optional, especially in those cases where the rendering methods will be called statically. At this point you should be familiar with the inner workings of the earlier “Form” helper class, since all of its methods are focused on performing basic string manipulations. Nevertheless, as I pointed out at the beginning, the form helper should be capable at least of constructing HTML text areas as well. So, can this be accomplished in a few simple steps? You bet! And to prove this, in the next section I’m going to show you how to add this extra functionality to the helper via a single static method. The full details of this process will be discussed in the upcoming segment, so to get there click on the link that appears below and keep reading.
blog comments powered by Disqus |
|
|
|
|
|
|
|