Manuel Pear

HTML_QuickForm_Renderer_ObjectFlexy

HTML_QuickForm_Renderer_ObjectFlexy -- A renderer for Flexy templates

Description

This renderer allows you to output your form using a Flexy template. A static and dynamic usage example is available in the docs/renderers directory.

Usage example

The following is an example of using the ObjectFlexy renderer with a simple template.

Exemple 47-1. Defining the elements

  1. <?php
  2.  
  3. require_once 'HTML/Template/Flexy.php';  
  4. require_once 'HTML/QuickForm.php';  
  5. require_once 'HTML/QuickForm/Renderer/ObjectFlexy.php';  
  6.  
  7.  
  8. // Form name will be used to find the placeholders.
  9.  
  10. $form = new HTML_QuickForm('form', 'POST');  
  11.  
  12. // Personal information
  13.  
  14. $form->addElement('header', 'personal', 'Personal Information');  
  15.  
  16. $form->addElement('text', 'email', 'Your email:');  
  17. $form->addElement('password', 'pass', 'Your password:', 'size=10');  
  18. $name['last'] = &HTML_QuickForm::createElement('text', 'first', 'First', 'size=10');  
  19. $name['first'] = &HTML_QuickForm::createElement('text', 'last', 'Last', 'size=10');  
  20. $form->addGroup($name, 'name', 'Name:', ', ');  
  21. $areaCode = &HTML_QuickForm::createElement('text', '', null,'size=4 maxlength=3');  
  22. $phoneNo1 = &HTML_QuickForm::createElement('text', '', null, 'size=4 maxlength=3');  
  23. $phoneNo2 = &HTML_QuickForm::createElement('text', '', null, 'size=5 maxlength=4');  
  24. $form->addGroup(array($areaCode, $phoneNo1, $phoneNo2), 'phone', 'Telephone:', '-');  
  25.  
  26. // Company information
  27.  
  28. $form->addElement('header', 'company_info', 'Company Information');  
  29.  
  30. $form->addElement('text', 'company', 'Company:', 'size=20');  
  31.  
  32. $str[] = &HTML_QuickForm::createElement('text', '', null, 'size=20');  
  33. $str[] = &HTML_QuickForm::createElement('text', '', null, 'size=20');  
  34. $form->addGroup($str, 'street', 'Street:', '<br />');  
  35.  
  36. $addr['zip'] = &HTML_QuickForm::createElement('text', 'zip', 'Zip', 'size=6 maxlength=10');  
  37. $addr['city'] = &HTML_QuickForm::createElement('text', 'city', 'City', 'size=15');  
  38. $form->addGroup($addr, 'address', 'Zip, city:');  
  39.  
  40. $select = array('' => 'Please select...', 'AU' => 'Australia', 'FR' => 'France', 'DE' => 'Germany', 'IT' => 'Italy');  
  41. $form->addElement('select', 'country', 'Country:', $select);  
  42.  
  43. // Other elements
  44.  
  45. $form->addElement('checkbox', 'news', '', " Check this box if you don't want to receive our newsletter.");  
  46.  
  47. $form->addElement('reset', 'reset', 'Reset');  
  48. $form->addElement('submit', 'submit', 'Register');  
  49.  
  50. // Tries to validate the form
  51. if ($form->validate()) { 
  52.    // Form is validated, then freezes the data
  53.    $form->freeze();  
  54. }  
  55.  
  56. // setup a template object
  57. $options = &PEAR::getStaticProperty('HTML_Template_Flexy','options');  
  58. $options = array( 
  59.    'templateDir' => './templates', 
  60.    'compileDir' => './templates/build', 
  61.    'forceCompile' => 1, 
  62.    'debug' => 0, 
  63.    'local' => 'en'  
  64. );  
  65.  
  66. $template = new HTML_Template_Flexy($options);  
  67.  
  68. $renderer =& new HTML_QuickForm_Renderer_ObjectFlexy($template);  
  69. $renderer->setLabelTemplate("label.html");  
  70. $renderer->setHtmlTemplate("html.html");  
  71.  
  72. $form->accept($renderer);  
  73.  
  74. $view = new StdClass;  
  75. $view->form = $renderer->toObject();  
  76.  
  77. $template->compile("flexy-static.html");  
  78. $template->outputObject($view);  
  79.  
  80.  
  81. ?> 

The template files used above are the following: label.html

Exemple 47-2. A template for the labels

{if:required}
    <font color="red" size="1">*</font>
{end:}
{label:h}
html.html

Exemple 47-3. A template for the overall html

{if:error}
    <font color="orange" size="1">{error:h}</font><br />
{end:}
{html:h}
flexy-static.html

Exemple 47-4. A template for the form

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!--  $Id$ -->
<html>
<head>
    <title>Flexy template : 2 column layout example</title>
    <style type="text/css">
       .errors {
       font-family: sans-serif;
       color : #000;
       background-color : #FFF;
       font-size : 12pt;
       }
       .label {
       font-family: sans-serif;
       color : Navy;
       font-size : 11px;
       text-align : right;
       vertical-align : top;
       white-space: nowrap;
       }
       .element {
       font-family: sans-serif;
       background-color : #EEE;
       text-align : left;
       white-space: nowrap;
       }
       .note {
       font-family: sans-serif;
       background-color : #EEE;
       text-align : center;
       font-size : 10pt;
       color : AAA;
       white-space: nowrap;
       }
       th {
       font-family: sans-serif;
       font-size : small;
       color : #FFF;
       background-color : #AAA;
       }
       .maintable {
       border : thin dashed #D0D0D0;
       background-color : #EEE;
       }
    </style>
{form.javascript:h}
</head>

<body>

{form.outputHeader():h}
{form.hidden:h}

<table class="maintable" width="600" align="center">
    <tr>
        <td width="50%" valign="top"><!-- Personal info -->
            <table width="100%" cellpadding="4">
                <tr><th colspan="2">{form.header.personal:h}</th></tr>
                <tr>
                    <td class="label">{form.name.label:h}</td>
                    <td class="element">{form.name.error:h}
                        <table cellspacing="0" cellpadding="1">
                            <tr>
                                <td>{form.name.first.html:h}</td>
                                <td>{form.name.last.html:h}</td>
                            </tr>
                            <tr>
                                <td><font size="1" color="grey">{form.name.first.label:h}</font></td>
                                <td><font size="1" color="grey">{form.name.last.label:h}</font></td>
                            </tr>
                        </table>
                    </td>
                </tr>
                <tr>
                    <td class="label">{form.phone.label:h}</td>
                    <td class="element">{form.phone.html:h}</td>
                </tr>
                <tr>
                    <td class="label">{form.email.label:h}</td>
                    <td class="element">{form.email.html:h}</td>
                </tr>
                <tr><td colspan="2" class="note">Please, choose a 8-10 characters password.</td></tr>
                <tr>
                    <td class="label">{form.pass.label:h}</td>
                    <td class="element">{form.pass.html:h}</td>
                </tr>
            </table>
        </td>
        
        <td width="50%" valign="top"><!-- Company info -->
            <table width="100%" cellpadding="4">
                <tr><th colspan="2">{form.header.company_info:h}</th></tr>
                <tr>
                    <td class="label">{form.company.label:h}</td>
                    <td class="element">{form.company.html:h}</td>
                </tr>
                <tr>
                    <td class="label" valign="top">{form.street.label:h}</td>
                    <td class="element">{form.street.html:h}</td>
                </tr>
                <tr>
                    <td class="label">{form.address.label:h}</td>
                    <td class="element">{form.address.error:h}
                        <table cellspacing="0" cellpadding="1">
                            <tr>
                                <td>{form.address.zip.html:h}</td>
                                <td>{form.address.city.html:h}</td>
                            </tr>
                            <tr>
                                <td><font size="1" color="grey">{form.address.zip.label:h}</font></td>
                                <td><font size="1" color="grey">{form.address.city.label:h}</font></td>
                            </tr>
                        </table>
                    </td>
                </tr>
                <tr>
                    <td class="label">{form.country.label:h}</td>
                    <td class="element">{form.country.html:h}</td>
                </tr>
                <tr>
                    <td class="label">{form.destination.label:h}</td>
                    <td class="element">{form.destination.html:h}</td>
                </tr>
            </table>
        </td>
    </tr>
</table>

<table width="600" align="center">
    <tr>
        <td>{form.requirednote:h}</td>
        <td align="right">{form.reset.html:h} {form.submit.html:h}</td>
    </tr>
    <tr>
        <td colspan="2" style="font-size:11px; color: navy;"><br />{form.news.html:h}</td>
    </tr>
</table>

</form>

<br />
<b>Collected Errors:</b><br />
{foreach:form.errors,error}
    <font color="red">{error}</font> in element [{name}]<br />
{end:}

 
<p><strong>The used "Static" Object</strong></p>
<pre style="font-size: 12px;">
{static_object}
</pre>

</body>
</html>

For more information on Flexy templating, see the package homepage.


Remonter Remonter
L'éditeur javascript - CSS - Gentoo - Tutoriaux PHP - Tutoriels PHP - Bretagne - php - Moto - Kit graphique