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 (www.phcomp.co.uk). Author: Alain D D Williams (addw@phcomp.co.uk).
     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
 -->
<html>
 <head>
  <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}
     {$FormData.javascript}
 {/if}
 </head>
 <body>

  <!-- 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>
  <div style='top:0px;margin:0px auto;text-align: center; font-size:40pt; '>
   {$FormData.DemoHeader}
  </div>
  <p>

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

   <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}
       {$head}
     {/foreach}{/if}

     {* 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) && $Field.id != '').
      * 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) && $Field.id != ''}

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

         {if is_array($Field) && is_array($Field.elements) && $Field.id != ''}
	   {* 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) && $FieldIn_1.id != ''}
		    {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) && $FieldIn_2.id != ''}
                            <div class="row">
			    {if $FormData.debug}Name={$FieldIn_2.name}{/if}
			        <label class="element">{$FieldIn_2.label|default:'&nbsp;'}</label>
			        <div class="element">{$FieldIn_2.html}</div>
                            </div>
			    {/if}
		      {/foreach}

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

         {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>
           </div>

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

   </form>
   {if $FormData.required_note and not $FormData.frozen}
     {$FormData.required_note}
   {/if}

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

  <p>
  &nbsp;<!-- Need this else the formatting is wrong -- don't know why -->
  <p>
   This is a simple form, fill in some values and press <i>Submit</i>.
  <p>
   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>.
  </p>
 </body>
</html>

Return to this tutorial index.