The best way to understand how the static “textarea()” method coded in the preceding segment fits into the structure of the web form helper class is by examining the full source code of this class. Below I included the complete definition of the helper, so you can see it in one single place for a closer analysis. Here’s the file containing this class: (Form.php) <?php 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 <textarea> tag public static function textarea(array $attributes) { $html = '<textarea'; $content = ''; if (!empty($attributes)) { foreach ($attributes as $attribute => $value) { if (in_array($attribute, array('rows', 'cols', 'id', 'class', 'name', 'value')) and !empty($value)) { if ($attribute === 'value') { $content = $value; continue; } $html .= ' ' . $attribute . '="' . $value . '"'; } } } return $html . '>' . $content . '</textarea>'; }
// render </form> closing tag public static function close() { return '</form>'; } }// End Form class Not too bad, eh? Now that the development of the above “Form” class is over, the functionality of the sample framework has been considerably enhanced. Now it is able to render different elements of an HTML form in a pretty automatic manner. As I said before, I encourage you to add more methods to this helper class. You can give it the ability to construct selects, buttons and other useful elements, which will save you from having to explicitly code any HTML markup. Final thoughts Over this sixth part of the series, I finished building a basic HTML form helper class, in this way adding yet another important component to the sample MVC-driven framework currently being developed in this group of tutorials. At this point, the framework has been packaged with a decent variety of classes that make it pretty functional. Even so, it’d be useful to make it able to cache both database result sets and HTML output. This extra feature would turn in into a more efficient piece of software. With that idea in mind, in the following tutorial I’m going to incorporate into the framework a handy caching class. So, if you want to learn how this class will be created, don’t miss the next article!
blog comments powered by Disqus |
|
|
|
|
|
|
|