Source code in Qf2ValidTypes.tpl

To see how this is used return to this tutorial index.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- This is the smarty template that goes with Qf2ValidTypesSm.php
     Return to the tutorial index to see a demonstration of what this does.

     Copyright (c) 2009, Parliament Hill Computers ( Author: Alain D D Williams (
     You may used this file as the basis your own (or organisation's/company's) project (under whatever
     licence that you see fit), but may not claim ownership or copyright of the substantially unmodified file.
     This file is made available in the hope that it is useful, there is no warranty at all, use at your own risk.

     SCCS: @(#)Qf2ValidTypes.tpl	1.6 10/21/13 15:08:34
  <title>Smarty template demonstration</title>
  <link rel="stylesheet" type="text/css" href="/DefaultStyle.css">
  <link rel="stylesheet" type="text/css" href="Qf2StyleSm.css">

 {* Emit any javascript from quickform *}
 {if $FormData.javascript}

  <!-- A standard page header -->
  <div style='position : absolute;text-align: left;top:10px;'>
   <a href='/'><img src='/Images/kite_40_83_trans.gif' width="40" height="83" alt="Parliament Hill Computers Ltd" style="border-width: 0;"></a>
  <div style='top:0px;margin:0px auto;text-align: center; font-size:40pt; '>

  <!-- The date and time substitution on the line below is done entirely in smarty -->
  The current date and time is {$|date_format:"%Y-%m-%d %H:%M:%S"}
   View this using a different Smarty template that contains code specific to each field by clicking <a href="?ByElement=1">here</a>

   <b>This uses generic Smarty code for each element</b><p>

   <!-- The form attributes are: is it a GET or POST form, the URL to action -->
   <form {$FormData.attributes}>
     {if isset($FormData.hidden)}{foreach from=$FormData.hidden item=head}

     {* Generate for every field in the form.
      * $FormData contains more than just form fields, this other stuff is ignored.
      * Form fields are arrays that contain the following elements of interest:
      * id      Always exists, generated by QF but can be set by the 'id' element
      *         of the 3rd argument to addElement().
      * name    This is the field name that will be used in $_GET/$_POST.
      *         This is usually the same as $key set by {foreach}, but see group below.
      * label   This some text that should be used to tell the user what the field is for,
      *         eg 'Age'. This is set by 'label' element of the 4th argument to addElement()
      *         or with the setLabel() method. It might not be set at all.
      * html    The html for the input field, you generate this.
      * type    text, image, textarea, ... If you were clever you could look at this.
      *         Note that this is not set for: fieldset or group.
      * attribs An array of field attributes, some generated by QuickForm2 others
      *         that were given to addElement().
      * error   This will be present if there is a validation error associated with this field.
      *         If the option group_errors is set this will not be generated, any error
      *         will be in a pseudo field called 'errors', this is what happens in this example.
      * value   The element value.
      * elements If a fieldset or group, the contained elements.
      *          If a checkbox an array keyed by fieldname index (eg '0' from 'vegetable[0]')
      *            value is the form field.
      *          If a radio button an array keyed by the field 'value' (the 'value' from the attributes)
      *            value is the form field.
      * Often you will generate the fields in the form and place them where you want them,
      * to do this use the fields, eg: {$FormData.Age.html}
      * What is going on here is complicated because it searches $FormData, it ignores anything
      * that is not an input field (is_array($Field) && $ != '').
      * Look towards the bottom of the loop for ''Normal case''.
      * The rest of this deals with a 'group' within a 'group' this is more complicated,
      * these appear to smarty as an array, loop through it in the same way as the outer loop
      * works, looking for arrays with a member 'id'. Thus you end up with 3 nested loops.
      * The {foreach gets the key, more by way of interest than need.
      * If $FormData.debug is true then generate some tracing.
      * Not everything has a label, so generate &nbsp; to keep the columns aligned.
      * This generates far too many blank lines in the HTML - but this is demo code!
     {foreach from=$FormData item=Field key=Key}
       {* Check that it is an input field *}
       {if is_array($Field) && $ != ''}

         {if $FormData.debug}<br>outer:id={$} key={$Key}<br>{/if}

         {if is_array($Field) && is_array($Field.elements) && $ != ''}
	   {* This is a group - loop through the subvalues *}

           {if $FormData.debug}Group subvalues<br>{/if}

	   {foreach from=$Field item=FieldIn_1 key=KeyIn_1}
	        {* Loop through the members of the group array *}
	        {if $FormData.debug}KeyIn_1:{$KeyIn_1}<br>{/if}

                {* Is this a group within a group ? *}
		{if is_array($FieldIn_1) && $ != ''}
		    {if is_array($FieldIn_1.elements)}
		      {foreach from=$FieldIn_1 item=FieldIn_2 key=KeyIn_2}
		        {* 3rd nested loop - traverse group within group *}
			{if $FormData.debug}KeyIn_2:{$KeyIn_2}{/if}

			{if is_array($FieldIn_2) && $ != ''}
                            <div class="row">
			    {if $FormData.debug}Name={$}{/if}
			        <label class="element">{$FieldIn_2.label|default:'&nbsp;'}</label>
			        <div class="element">{$FieldIn_2.html}</div>

		      {* Top level group - ie not a group within a group *}
                      <div class="row">
		      {if $FormData.debug}inner id: {$} name: {$}<br>{/if}
		          <label class="element">{$FieldIn_1.label|default:'&nbsp;'}</label>
		          <div class="element">{$FieldIn_1.html}</div>
		  {if $FormData.debug}Ignore this inner field:{$FieldIn_1}{/if}
		{if $FormData.debug}<p>{/if}

         {else}{* group check *}

	   {* Normal case - ie when not a group -- not much to this really! *}

           <div class="row">
             <label class="element">{$Field.label|default:'&nbsp;'}</label>
             <div class="element">{$Field.html}</div>

         {/if}{* group check *}
       {/if}{* Input field check *}

   {if $FormData.required_note and not $FormData.frozen}

   <!-- Display any errors, loop over what there is in the errors array -->
   <div style="color:red">
    {foreach from=$FormData.errors item=error}

  &nbsp;<!-- Need this else the formatting is wrong -- don't know why -->
   This is a simple form, fill in some values and press <i>Submit</i>.
   This is a quickform that has been rendered with smarty.

  <p style="font-size: x-large; font-weight: bold;">
   Return to this <em><a href='index.php'>tutorial index</a></em>.

Return to this tutorial index.