Source for file ArraySmarty.php
Documentation is available at ArraySmarty.php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Alexey Borzov <borz_off@cs.msu.su> |
// | Bertrand Mansion <bmansion@mamasam.com> |
// | Thomas Schulz <ths@4bconsult.de> |
// +----------------------------------------------------------------------+
// $Id: ArraySmarty.php,v 1.10 2005/04/26 07:58:55 ths Exp $
require_once 'HTML/QuickForm/Renderer/Array.php';
* A static renderer for HTML_QuickForm, makes an array of form content
* useful for an Smarty template
* Based on old toArray() code and ITStatic renderer.
* The form array structure is the following:
* [frozen] => whether the complete form is frozen'
* [javascript] => javascript for client-side validation
* [attributes] => attributes for <form> tag
* [hidden] => html of all hidden elements
* [requirednote] => note about the required elements
* [1st_element_name] => Error for the 1st element
* [nth_element_name] => Error for the nth element
* [1st_header_name] => Header text for the 1st header
* [nth_header_name] => Header text for the nth header
* [1st_element_name] => Array for the 1st element
* [nth_element_name] => Array for the nth element
* // where an element array has the form:
* [value] => element value,
* [type] => type of the element
* [frozen] => whether element is frozen
* [label] => label for the element
* [required] => whether element is required
* // if element is not a group:
* [html] => HTML for the element
* // if element is a group:
* [separator] => separator for group elements
* [1st_gitem_name] => Array for the 1st element in group
* [nth_gitem_name] => Array for the nth element in group
* The Smarty template engine instance
* The current element index inside a group
var $_groupElementIdx = 0;
* How to handle the required tag for required fields
* @see setRequiredTemplate()
* How to handle error messages in form validation
* @see setErrorTemplate()
* @param object reference to the Smarty template engine instance
* @param bool true: render an array of labels to many labels, $key 0 to 'label' and the oterh to "label_$key"
$this->HTML_QuickForm_Renderer_Array (true , $staticLabels);
* Called when visiting a header element
* @param object An HTML_QuickForm_header element being visited
if ($name = $header->getName ()) {
$this->_ary['header'][$name] = $header->toHtml ();
$this->_ary['header'][$this->_sectionCount] = $header->toHtml ();
$this->_currentSection = $this->_sectionCount++;
} // end func renderHeader
* Called when visiting a group, before processing any group elements
* @param object An HTML_QuickForm_group object being visited
* @param bool Whether a group is required
* @param string An error message associated with a group
parent ::startGroup ($group, $required, $error);
$this->_groupElementIdx = 1;
* Creates an array representing an element containing
* the key for storing this
* @param object An HTML_QuickForm_element object
* @param bool Whether an element is required
* @param string Error associated with the element
function _elementToArray (&$element, $required, $error)
$ret = parent ::_elementToArray ($element, $required, $error);
if ('group' == $ret['type']) {
$ret['html'] = $element->toHtml ();
// we don't need the elements, see the array structure
if (($required || $error) && !empty ($this->_required)){
$this->_renderRequired ($ret['label'], $ret['html'], $required, $error);
if ($error && !empty ($this->_error)) {
$this->_renderError ($ret['label'], $ret['html'], $error);
// create keys for elements grouped by native group or name
if (strstr($ret['name'], '[') or $this->_currentGroup) {
preg_match('/([^]]*)\\[([^]]*)\\]/', $ret['name'], $matches);
if (isset ($matches[1 ])) {
array ('[' , ']', '[\'\']'),
array ('[\'', '\']', '[]' ),
$sKeys = '[\'' . $matches[1 ] . '\']' . $sKeysSub;
$sKeys = '[\'' . $ret['name'] . '\']';
// special handling for elements in native groups
if ($this->_currentGroup) {
// skip unnamed group items unless radios: no name -> no static access
// identification: have the same key string as the parent group
if ($this->_currentGroup['keys'] == $sKeys and 'radio' != $ret['type']) {
// reduce string of keys by remove leading group keys
if (0 === strpos($sKeys, $this->_currentGroup['keys'])) {
// element without a name
} elseif ($ret['name'] == '') {
$sKeys = '[\'element_' . $this->_elementIdx . '\']';
$sKeys = '[\'' . $ret['name'] . '\']';
// for radios: add extra key from value
if ('radio' == $ret['type'] and substr($sKeys, -2 ) != '[]') {
$sKeys .= '[\'' . $ret['value'] . '\']';
} // end func _elementToArray
* Stores an array representation of an element in the form array
* @param array Array representation of an element
function _storeArray ($elAry)
// where should we put this element...
if (is_array($this->_currentGroup) && ('group' != $elAry['type'])) {
$toEval = '$this->_currentGroup' . $sKeys . ' = $elAry;';
$toEval = '$this->_ary' . $sKeys . ' = $elAry;';
* Called when an element is required
* This method will add the required tag to the element label and/or the element html
* such as defined with the method setRequiredTemplate.
* @param string The element label
* @param string The element html rendering
* @param boolean The element required
* @param string The element error
* @see setRequiredTemplate()
function _renderRequired (&$label, &$html, &$required, &$error)
$this->_tpl->assign (array (
if (!empty ($label) && strpos($this->_required, $this->_tpl->left_delimiter . '$label') !== false ) {
$label = $this->_tplFetch ($this->_required);
if (!empty ($html) && strpos($this->_required, $this->_tpl->left_delimiter . '$html') !== false ) {
$html = $this->_tplFetch ($this->_required);
$this->_tpl->clear_assign (array ('label', 'html', 'required'));
} // end func _renderRequired
* Called when an element has a validation error
* This method will add the error message to the element label or the element html
* such as defined with the method setErrorTemplate. If the error placeholder is not found
* in the template, the error will be displayed in the form error block.
* @param string The element label
* @param string The element html rendering
* @param string The element error
* @see setErrorTemplate()
function _renderError (&$label, &$html, &$error)
$this->_tpl->assign (array ('label' => '', 'html' => '', 'error' => $error));
$error = $this->_tplFetch ($this->_error);
$this->_tpl->assign (array ('label' => $label, 'html' => $html));
if (!empty ($label) && strpos($this->_error, $this->_tpl->left_delimiter . '$label') !== false ) {
$label = $this->_tplFetch ($this->_error);
} elseif (!empty ($html) && strpos($this->_error, $this->_tpl->left_delimiter . '$html') !== false ) {
$html = $this->_tplFetch ($this->_error);
$this->_tpl->clear_assign (array ('label', 'html', 'error'));
} // end func _renderError
* Process an template sourced in a string with Smarty
* Smarty has no core function to render a template given as a string.
* So we use the smarty eval plugin function to do this.
* @param string The template source
function _tplFetch ($tplSource)
require SMARTY_DIR . '/plugins/function.eval.php';
return smarty_function_eval (array ('var' => $tplSource), $this->_tpl);
* Sets the way required elements are rendered
* You can use {$label} or {$html} placeholders to let the renderer know where
* where the element label or the element html are positionned according to the
* required tag. They will be replaced accordingly with the right value. You
* can use the full smarty syntax here, especially a custom modifier for I18N.
* {if $required}<span style="color: red;">*</span>{/if}{$label|translate}
* will put a red star in front of the label if the element is required and
* @param string The required element template
$this->_required = $template;
} // end func setRequiredTemplate
* Sets the way elements with validation errors are rendered
* You can use {$label} or {$html} placeholders to let the renderer know where
* where the element label or the element html are positionned according to the
* error message. They will be replaced accordingly with the right value.
* The error message will replace the {$error} placeholder.
* {if $error}<span style="color: red;">{$error}</span>{/if}<br />{$html}
* will put the error message in red on top of the element html.
* If you want all error messages to be output in the main error block, use
* the {$form.errors} part of the rendered array that collects all raw error
* If you want to place all error messages manually, do not specify {$html}
* Groups can have special layouts. With this kind of groups, you have to
* place the formated error message manually. In this case, use {$form.group.error}
* where you want the formated error message to appear in the form.
* @param string The element error template
$this->_error = $template;
} // end func setErrorTemplate
Documentation generated on Mon, 11 Mar 2019 14:16:30 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|