HTML_Quickform headings in Smarty templates using {foreach}
| From: | Matthew Vickery | Date: | Thu, 26 Oct 2006 09:23:29 +0000 |
| Subject: | HTML_Quickform headings in Smarty templates using {foreach} | ||
| Groups: | php.pear.general | ||
| Request: | Send a blank email to pear-general+get-25646@lists.php.net to get a copy of this message | ||
I'm using HTML_Quickform with Smarty.
If I create renderer abject and assign this to an array the resulting array looses the natural flow of the form. i.e. all the heading are at the top of the array, not above the form elements they refer to. This makes it impossible to use a {foreach} in my template to loop over the elements and print these in the correct order.
Code as follows:
// Create the renderer object
$renderer =& new HTML_QuickForm_Renderer_ArraySmarty($tpl) ;
$form->accept($renderer) ;
$tpl->assign( 'Form', $renderer->toArray() ) ;
I've found the following post on the smarty forum that seems helpful:
http://www.phpinsider.com/smarty-forum/viewtopic.php?t=7297&highlight=htmlquickform+controller
Basically he assigns the form data to an array like so:
$tpl->assign( 'Form', $form->toArray() ) ;
This then keeps the correct ordering of the elements and headings and allows the use of a {foreach} in the template like so:
<form {$Form.attributes}>
{foreach from=$Form.elements item=element}
{* $element now contains the attributes of the current form element *}
{if $element.required}
{* this element is required *}
<span class="req">*</span>
{/if}
<label>{$element.label}{$element.html}</label><br />
{/foreach}
{foreach from=$Form.groups item=group}
<fieldset>
<legend>{$group.label}</legend>
{foreach from$group.elements item=element}
...(loop through each element as your did above)
{/foreach}
</fieldset>
{/foreach}
</form>
But what I do not understand is the renderer object part is skipped how do you call display() or fetch():
$tpl->display('admin_email.tpl');
$admin_email->Body = $tpl->fetch('admin_email.tpl');
(for use with phpmailer)