0 follower

CHtml

Package system.web.helpers
Inheritance class CHtml
Since 1.0
Version $Id$
Source Code framework/web/helpers/CHtml.php
CHtml is a static class that provides a collection of helper methods for creating HTML views.

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
afterRequiredLabel string the HTML code to be appended to the required label. CHtml
beforeRequiredLabel string the HTML code to be prepended to the required label. CHtml
count integer the counter for generating automatic input field names. CHtml
errorCss string the CSS class for highlighting error inputs. CHtml
errorMessageCss string the CSS class for displaying error messages (see error). CHtml
errorSummaryCss string the CSS class for displaying error summaries (see errorSummary). CHtml
requiredCss string the CSS class for required labels. CHtml
scenario string the scenario used to determine whether a model attribute is required. CHtml

Public Methods

Hide inherited methods

MethodDescriptionDefined By
activeCheckBox() Generates a check box for a model attribute. CHtml
activeCheckBoxList() Generates a check box list for a model attribute. CHtml
activeDropDownList() Generates a drop down list for a model attribute. CHtml
activeFileField() Generates a file input for a model attribute. CHtml
activeHiddenField() Generates a hidden input for a model attribute. CHtml
activeId() Generates input field ID for a model attribute. CHtml
activeLabel() Generates a label tag for a model attribute. CHtml
activeLabelEx() Generates a label tag for a model attribute. CHtml
activeListBox() Generates a list box for a model attribute. CHtml
activeName() Generates input field name for a model attribute. CHtml
activePasswordField() Generates a password field input for a model attribute. CHtml
activeRadioButton() Generates a radio button for a model attribute. CHtml
activeRadioButtonList() Generates a radio button list for a model attribute. CHtml
activeTextArea() Generates a text area input for a model attribute. CHtml
activeTextField() Generates a text field input for a model attribute. CHtml
ajax() Generates the JavaScript that initiates an AJAX request. CHtml
ajaxButton() Generates a push button that can initiate AJAX requests. CHtml
ajaxSubmitButton() Generates a push button that can submit the current form in POST method. CHtml
asset() Generates the URL for the published assets. CHtml
beginForm() Generates an opening form tag. CHtml
button() Generates a button. CHtml
cdata() Encloses the given string within a CDATA tag. CHtml
checkBox() Generates a check box. CHtml
checkBoxList() Generates a check box list. CHtml
closeTag() Generates a close HTML element. CHtml
css() Encloses the given CSS content with a CSS tag. CHtml
cssFile() Links to the specified CSS file. CHtml
encode() Encodes special characters into HTML entities. CHtml
encodeArray() Encodes special characters in an array of strings into HTML entities. CHtml
endForm() Generates a closing form tag. CHtml
error() Displays the first validation error for a model attribute. CHtml
errorSummary() Displays a summary of validation errors for one or several models. CHtml
fileField() Generates a file input. CHtml
form() Generates an opening form tag. CHtml
getActiveId() Returns the element ID that is used by methods such as activeTextField. CHtml
getIdByName() Generates a valid HTML ID based the name. CHtml
hiddenField() Generates a hidden input. CHtml
htmlButton() Generates a button using HTML button tag. CHtml
image() Generates an image tag. CHtml
imageButton() Generates an image submit button. CHtml
label() Generates a label tag. CHtml
linkButton() Generates a link submit button. CHtml
linkTag() Generates a link tag that can be inserted in the head section of HTML page. CHtml
listBox() Generates a list box. CHtml
listData() Generates the data suitable for dropDownList and listBox. CHtml
listOptions() Generates the list options. CHtml
mailto() Generates a mailto link. CHtml
metaTag() Generates a meta tag that can be inserted in the head section of HTML page. CHtml
normalizeUrl() Normalizes the input parameter to be a valid URL. CHtml
openTag() Generates an open HTML element. CHtml
pageStateField() Generates a hidden field for storing persistent page states. CHtml
passwordField() Generates a password field input. CHtml
radioButton() Generates a radio button. CHtml
radioButtonList() Generates a radio button list. CHtml
resetButton() Generates a reset button. CHtml
resolveName() Generates input name for a model attribute. CHtml
resolveNameID() Generates input name and ID for a model attribute. CHtml
script() Encloses the given JavaScript within a script tag. CHtml
scriptFile() Includes a JavaScript file. CHtml
statefulForm() Generates a stateful form tag. CHtml
submitButton() Generates a submit button. CHtml
tag() Generates an HTML element. CHtml
textArea() Generates a text area input. CHtml
textField() Generates a text field input. CHtml
value() Evaluates the value of the specified attribute for the given model. CHtml

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
activeInputField() Generates an input HTML tag for a model attribute. CHtml
addErrorCss() Appends errorCss to the 'class' attribute. CHtml
clientChange() Generates the JavaScript with the specified client changes. CHtml
inputField() Generates an input HTML tag. CHtml
renderAttributes() Renders the HTML tag attributes. CHtml

Property Details

afterRequiredLabel property
public static string $afterRequiredLabel;

the HTML code to be appended to the required label.

See Also

beforeRequiredLabel property
public static string $beforeRequiredLabel;

the HTML code to be prepended to the required label.

See Also

count property (available since v1.0.4)
public static integer $count;

the counter for generating automatic input field names.

errorCss property
public static string $errorCss;

the CSS class for highlighting error inputs. Form inputs will be appended with this CSS class if they have input errors.

errorMessageCss property
public static string $errorMessageCss;

the CSS class for displaying error messages (see error).

errorSummaryCss property
public static string $errorSummaryCss;

the CSS class for displaying error summaries (see errorSummary).

requiredCss property
public static string $requiredCss;

the CSS class for required labels. Defaults to 'required'.

See Also

scenario property
public static string $scenario;

the scenario used to determine whether a model attribute is required.

See Also

Method Details

activeCheckBox() method
public static string activeCheckBox(CModel $model, string $attribute, array $htmlOptions=array ( ))
$model CModel the data model
$attribute string the attribute
$htmlOptions array additional HTML attributes. Besides normal HTML attributes, a few special attributes are also recognized (see clientChange and tag for more details.) Since version 1.0.2, a special option named 'uncheckValue' is available that can be used to specify the value returned when the checkbox is not checked. By default, this value is '0'. Internally, a hidden field is rendered so that when the checkbox is not checked, we can still obtain the posted uncheck value. If 'uncheckValue' is set as NULL, the hidden field will not be rendered.
{return} string the generated check box
Source Code: framework/web/helpers/CHtml.php#1231 (show)
public static function activeCheckBox($model,$attribute,$htmlOptions=array())
{
    
self::resolveNameID($model,$attribute,$htmlOptions);
    if(!isset(
$htmlOptions['value']))
        
$htmlOptions['value']=1;
    if(!isset(
$htmlOptions['checked']) && $model->$attribute==$htmlOptions['value'])
        
$htmlOptions['checked']='checked';
    
self::clientChange('click',$htmlOptions);

    if(
array_key_exists('uncheckValue',$htmlOptions))
    {
        
$uncheck=$htmlOptions['uncheckValue'];
        unset(
$htmlOptions['uncheckValue']);
    }
    else
        
$uncheck='0';

    
$hidden=$uncheck!==null self::hiddenField($htmlOptions['name'],$uncheck,array('id'=>self::ID_PREFIX.$htmlOptions['id'])) : '';

    return 
$hidden self::activeInputField('checkbox',$model,$attribute,$htmlOptions);
}

Generates a check box for a model attribute. The attribute is assumed to take either true or false value. If the attribute has input error, the input field's CSS class will be appended with errorCss.

activeCheckBoxList() method
public static string activeCheckBoxList(CModel $model, string $attribute, array $data, array $htmlOptions=array ( ))
$model CModel the data model
$attribute string the attribute
$data array value-label pairs used to generate the check box list. Note, the values will be automatically HTML-encoded, while the labels will not.
$htmlOptions array addtional HTML options. The options will be applied to each checkbox input. The following special options are recognized:
  • template: string, specifies how each checkbox is rendered. Defaults to "{input} {label}", where "{input}" will be replaced by the generated check box input tag while "{label}" be replaced by the corresponding check box label.
  • separator: string, specifies the string that separates the generated check boxes.
  • checkAll: string, specifies the label for the "check all" checkbox. If this option is specified, a 'check all' checkbox will be displayed. Clicking on this checkbox will cause all checkboxes checked or unchecked. This option has been available since version 1.0.4.
  • checkAllLast: boolean, specifies whether the 'check all' checkbox should be displayed at the end of the checkbox list. If this option is not set (default) or is false, the 'check all' checkbox will be displayed at the beginning of the checkbox list. This option has been available since version 1.0.4.
  • encode: boolean, specifies whether to encode HTML-encode tag attributes and values. Defaults to true. This option has been available since version 1.0.5.
{return} string the generated check box list
Source Code: framework/web/helpers/CHtml.php#1378 (show)
public static function activeCheckBoxList($model,$attribute,$data,$htmlOptions=array())
{
    
self::resolveNameID($model,$attribute,$htmlOptions);
    
$selection=$model->$attribute;
    if(
$model->hasErrors($attribute))
        
self::addErrorCss($htmlOptions);
    
$name=$htmlOptions['name'];
    unset(
$htmlOptions['name']);

    return 
self::hiddenField($name,'',array('id'=>self::ID_PREFIX.$htmlOptions['id']))
        . 
self::checkBoxList($name,$selection,$data,$htmlOptions);
}

Generates a check box list for a model attribute. The model attribute value is used as the selection. If the attribute has input error, the input field's CSS class will be appended with errorCss. Note that a check box list allows multiple selection, like listBox. As a result, the corresponding POST value is an array. In case no selection is made, the corresponding POST value is an empty string.

See Also

activeDropDownList() method
public static string activeDropDownList(CModel $model, string $attribute, array $data, array $htmlOptions=array ( ))
$model CModel the data model
$attribute string the attribute
$data array data for generating the list options (value=>display) You may use listData to generate this data. Please refer to listOptions on how this data is used to generate the list options. Note, the values and labels will be automatically HTML-encoded by this method.
$htmlOptions array additional HTML attributes. Besides normal HTML attributes, a few special attributes are recognized. See clientChange and tag for more details. In addition, the following options are also supported:
  • prompt: string, specifies the prompt text shown as the first list option. Its value is empty.
  • empty: string, specifies the text corresponding to empty selection. Its value is empty. Starting from version 1.0.10, the 'empty' option can also be an array of value-label pairs. Each pair will be used to render a list option at the beginning.
  • options: array, specifies additional attributes for each OPTION tag. The array keys must be the option values, and the array values are the extra OPTION tag attributes in the name-value pairs. For example,
        array(
            'value1'=>array('disabled'=>true, 'label'=>'value 1'),
            'value2'=>array('label'=>'value 2'),
        );
    
    This option has been available since version 1.0.3.
{return} string the generated drop down list
Source Code: framework/web/helpers/CHtml.php#1287 (show)
public static function activeDropDownList($model,$attribute,$data,$htmlOptions=array())
{
    
self::resolveNameID($model,$attribute,$htmlOptions);
    
$selection=$model->$attribute;
    
$options="\n".self::listOptions($selection,$data,$htmlOptions);
    
self::clientChange('change',$htmlOptions);
    if(
$model->hasErrors($attribute))
        
self::addErrorCss($htmlOptions);
    if(isset(
$htmlOptions['multiple']))
    {
        if(
substr($htmlOptions['name'],-2)!=='[]')
            
$htmlOptions['name'].='[]';
    }
    return 
self::tag('select',$htmlOptions,$options);
}

Generates a drop down list for a model attribute. If the attribute has input error, the input field's CSS class will be appended with errorCss.

activeFileField() method
public static string activeFileField(CModel $model, string $attribute, array $htmlOptions=array ( ))
$model CModel the data model
$attribute string the attribute
$htmlOptions array additional HTML attributes (see tag).
{return} string the generated input field
Source Code: framework/web/helpers/CHtml.php#1164 (show)
public static function activeFileField($model,$attribute,$htmlOptions=array())
{
    
self::resolveNameID($model,$attribute,$htmlOptions);
    
// add a hidden field so that if a model only has a file field, we can
    // still use isset($_POST[$modelClass]) to detect if the input is submitted
    
return self::hiddenField($htmlOptions['name'],'',array('id'=>self::ID_PREFIX.$htmlOptions['id']))
        . 
self::activeInputField('file',$model,$attribute,$htmlOptions);
}

Generates a file input for a model attribute. Note, you have to set the enclosing form's 'enctype' attribute to be 'multipart/form-data'. After the form is submitted, the uploaded file information can be obtained via $_FILES (see PHP documentation).

See Also

activeHiddenField() method
public static string activeHiddenField(CModel $model, string $attribute, array $htmlOptions=array ( ))
$model CModel the data model
$attribute string the attribute
$htmlOptions array additional HTML attributes.
{return} string the generated input field
Source Code: framework/web/helpers/CHtml.php#1108 (show)
public static function activeHiddenField($model,$attribute,$htmlOptions=array())
{
    
self::resolveNameID($model,$attribute,$htmlOptions);
    return 
self::activeInputField('hidden',$model,$attribute,$htmlOptions);
}

Generates a hidden input for a model attribute.

See Also

activeId() method (available since v1.0.1)
public static string activeId(CModel $model, string $attribute)
$model CModel the data model
$attribute string the attribute
{return} string the generated input field ID
Source Code: framework/web/helpers/CHtml.php#1584 (show)
public static function activeId($model,$attribute)
{
    return 
self::getIdByName(self::activeName($model,$attribute));
}

Generates input field ID for a model attribute.

activeInputField() method
protected static string activeInputField(string $type, CModel $model, string $attribute, array $htmlOptions)
$type string the input type (e.g. 'text', 'radio')
$model CModel the data model
$attribute string the attribute
$htmlOptions array additional HTML attributes for the HTML tag
{return} string the generated input tag
Source Code: framework/web/helpers/CHtml.php#1615 (show)
protected static function activeInputField($type,$model,$attribute,$htmlOptions)
{
    
$htmlOptions['type']=$type;
    if(
$type==='file')
        unset(
$htmlOptions['value']);
    else if(!isset(
$htmlOptions['value']))
        
$htmlOptions['value']=$model->$attribute;
    if(
$model->hasErrors($attribute))
        
self::addErrorCss($htmlOptions);
    return 
self::tag('input',$htmlOptions);
}

Generates an input HTML tag for a model attribute. This method generates an input HTML tag based on the given data model and attribute. If the attribute has input error, the input field's CSS class will be appended with errorCss. This enables highlighting the incorrect input.

activeLabel() method
public static string activeLabel(CModel $model, string $attribute, array $htmlOptions=array ( ))
$model CModel the data model
$attribute string the attribute
$htmlOptions array additional HTML attributes. A special option named 'label' is recognized since version 1.0.4. If this option is specified, it will be used as the label. Otherwise, the label will be determined by calling CModel::getAttributeLabel.
{return} string the generated label tag
Source Code: framework/web/helpers/CHtml.php#1037 (show)
public static function activeLabel($model,$attribute,$htmlOptions=array())
{
    if(isset(
$htmlOptions['for']))
    {
        
$for=$htmlOptions['for'];
        unset(
$htmlOptions['for']);
    }
    else
        
$for=self::getIdByName(self::resolveName($model,$attribute));
    if(isset(
$htmlOptions['label']))
    {
        
$label=$htmlOptions['label'];
        unset(
$htmlOptions['label']);
    }
    else
        
$label=$model->getAttributeLabel($attribute);
    if(
$model->hasErrors($attribute))
        
self::addErrorCss($htmlOptions);
    return 
self::label($label,$for,$htmlOptions);
}

Generates a label tag for a model attribute. The label text is the attribute label and the label is associated with the input for the attribute (see CModel::getAttributeLabel. If the attribute has input error, the label's CSS class will be appended with errorCss.

activeLabelEx() method (available since v1.0.2)
public static string activeLabelEx(CModel $model, string $attribute, array $htmlOptions=array ( ))
$model CModel the data model
$attribute string the attribute
$htmlOptions array additional HTML attributes.
{return} string the generated label tag
Source Code: framework/web/helpers/CHtml.php#1073 (show)
public static function activeLabelEx($model,$attribute,$htmlOptions=array())
{
    
$realAttribute=$attribute;
    
self::resolveName($model,$attribute); // strip off square brackets if any
    
$htmlOptions['required']=$model->isAttributeRequired($attribute,self::$scenario);
    return 
self::activeLabel($model,$realAttribute,$htmlOptions);
}

Generates a label tag for a model attribute. This is an enhanced version of activeLabel. It will render additional CSS class and mark when the attribute is required. In particular, it calls CModel::isAttributeRequired to determine if the attribute is required under the scenario CHtml::scenario. If so, it will add a CSS class CHtml::requiredCss to the label, and decorate the label with CHtml::beforeRequiredLabel and CHtml::afterRequiredLabel.

activeListBox() method
public static string activeListBox(CModel $model, string $attribute, array $data, array $htmlOptions=array ( ))
$model CModel the data model
$attribute string the attribute
$data array data for generating the list options (value=>display) You may use listData to generate this data. Please refer to listOptions on how this data is used to generate the list options. Note, the values and labels will be automatically HTML-encoded by this method.
$htmlOptions array additional HTML attributes. Besides normal HTML attributes, a few special attributes are recognized. See clientChange and tag for more details. In addition, the following options are also supported:
  • prompt: string, specifies the prompt text shown as the first list option. Its value is empty.
  • empty: string, specifies the text corresponding to empty selection. Its value is empty. Starting from version 1.0.10, the 'empty' option can also be an array of value-label pairs. Each pair will be used to render a list option at the beginning.
  • options: array, specifies additional attributes for each OPTION tag. The array keys must be the option values, and the array values are the extra OPTION tag attributes in the name-value pairs. For example,
        array(
            'value1'=>array('disabled'=>true, 'label'=>'value 1'),
            'value2'=>array('label'=>'value 2'),
        );
    
    This option has been available since version 1.0.3.
{return} string the generated list box
Source Code: framework/web/helpers/CHtml.php#1338 (show)
public static function activeListBox($model,$attribute,$data,$htmlOptions=array())
{
    if(!isset(
$htmlOptions['size']))
        
$htmlOptions['size']=4;
    return 
self::activeDropDownList($model,$attribute,$data,$htmlOptions);
}

Generates a list box for a model attribute. The model attribute value is used as the selection. If the attribute has input error, the input field's CSS class will be appended with errorCss.

activeName() method (available since v1.0.1)
public static string activeName(CModel $model, string $attribute)
$model CModel the data model
$attribute string the attribute
{return} string the generated input field name
Source Code: framework/web/helpers/CHtml.php#1597 (show)
public static function activeName($model,$attribute)
{
    
$a=$attribute// because the attribute name may be changed by resolveName
    
return self::resolveName($model,$a);
}

Generates input field name for a model attribute. Unlike resolveName, this method does NOT modify the attribute name.

activePasswordField() method
public static string activePasswordField(CModel $model, string $attribute, array $htmlOptions=array ( ))
$model CModel the data model
$attribute string the attribute
$htmlOptions array additional HTML attributes. Besides normal HTML attributes, a few special attributes are also recognized (see clientChange and tag for more details.)
{return} string the generated input field
Source Code: framework/web/helpers/CHtml.php#1126 (show)
public static function activePasswordField($model,$attribute,$htmlOptions=array())
{
    
self::resolveNameID($model,$attribute,$htmlOptions);
    
self::clientChange('change',$htmlOptions);
    return 
self::activeInputField('password',$model,$attribute,$htmlOptions);
}

Generates a password field input for a model attribute. If the attribute has input error, the input field's CSS class will be appended with errorCss.

activeRadioButton() method
public static string activeRadioButton(CModel $model, string $attribute, array $htmlOptions=array ( ))
$model CModel the data model
$attribute string the attribute
$htmlOptions array additional HTML attributes. Besides normal HTML attributes, a few special attributes are also recognized (see clientChange and tag for more details.) Since version 1.0.9, a special option named 'uncheckValue' is available that can be used to specify the value returned when the radiobutton is not checked. By default, this value is '0'. Internally, a hidden field is rendered so that when the radiobutton is not checked, we can still obtain the posted uncheck value. If 'uncheckValue' is set as NULL, the hidden field will not be rendered.
{return} string the generated radio button
Source Code: framework/web/helpers/CHtml.php#1190 (show)
public static function activeRadioButton($model,$attribute,$htmlOptions=array())
{
    
self::resolveNameID($model,$attribute,$htmlOptions);
    if(!isset(
$htmlOptions['value']))
        
$htmlOptions['value']=1;
    if(!isset(
$htmlOptions['checked']) && $model->$attribute==$htmlOptions['value'])
        
$htmlOptions['checked']='checked';
    
self::clientChange('click',$htmlOptions);

    if(
array_key_exists('uncheckValue',$htmlOptions))
    {
        
$uncheck=$htmlOptions['uncheckValue'];
        unset(
$htmlOptions['uncheckValue']);
    }
    else
        
$uncheck='0';

    
$hidden=$uncheck!==null self::hiddenField($htmlOptions['name'],$uncheck,array('id'=>self::ID_PREFIX.$htmlOptions['id'])) : '';

    
// add a hidden field so that if the radio button is not selected, it still submits a value
    
return $hidden self::activeInputField('radio',$model,$attribute,$htmlOptions);
}

Generates a radio button for a model attribute. If the attribute has input error, the input field's CSS class will be appended with errorCss.

activeRadioButtonList() method
public static string activeRadioButtonList(CModel $model, string $attribute, array $data, array $htmlOptions=array ( ))
$model CModel the data model
$attribute string the attribute
$data array value-label pairs used to generate the radio button list. Note, the values will be automatically HTML-encoded, while the labels will not.
$htmlOptions array addtional HTML options. The options will be applied to each checkbox input. The following special options are recognized:
  • template: string, specifies how each checkbox is rendered. Defaults to "{input} {label}", where "{input}" will be replaced by the generated radio button input tag while "{label}" be replaced by the corresponding radio button label.
  • separator: string, specifies the string that separates the generated radio buttons.
  • encode: boolean, specifies whether to encode HTML-encode tag attributes and values. Defaults to true. This option has been available since version 1.0.5.
{return} string the generated radio button list
Source Code: framework/web/helpers/CHtml.php#1413 (show)
public static function activeRadioButtonList($model,$attribute,$data,$htmlOptions=array())
{
    
self::resolveNameID($model,$attribute,$htmlOptions);
    
$selection=$model->$attribute;
    if(
$model->hasErrors($attribute))
        
self::addErrorCss($htmlOptions);
    
$name=$htmlOptions['name'];
    unset(
$htmlOptions['name']);

    return 
self::hiddenField($name,'',array('id'=>self::ID_PREFIX.$htmlOptions['id']))
        . 
self::radioButtonList($name,$selection,$data,$htmlOptions);
}

Generates a radio button list for a model attribute. The model attribute value is used as the selection. If the attribute has input error, the input field's CSS class will be appended with errorCss.

See Also

activeTextArea() method
public static string activeTextArea(CModel $model, string $attribute, array $htmlOptions=array ( ))
$model CModel the data model
$attribute string the attribute
$htmlOptions array additional HTML attributes. Besides normal HTML attributes, a few special attributes are also recognized (see clientChange and tag for more details.)
{return} string the generated text area
Source Code: framework/web/helpers/CHtml.php#1144 (show)
public static function activeTextArea($model,$attribute,$htmlOptions=array())
{
    
self::resolveNameID($model,$attribute,$htmlOptions);
    
self::clientChange('change',$htmlOptions);
    if(
$model->hasErrors($attribute))
        
self::addErrorCss($htmlOptions);
    return 
self::tag('textarea',$htmlOptions,isset($htmlOptions['encode']) && !$htmlOptions['encode'] ? $model->$attribute self::encode($model->$attribute));
}

Generates a text area input for a model attribute. If the attribute has input error, the input field's CSS class will be appended with errorCss.

See Also

activeTextField() method
public static string activeTextField(CModel $model, string $attribute, array $htmlOptions=array ( ))
$model CModel the data model
$attribute string the attribute
$htmlOptions array additional HTML attributes. Besides normal HTML attributes, a few special attributes are also recognized (see clientChange and tag for more details.)
{return} string the generated input field
Source Code: framework/web/helpers/CHtml.php#1093 (show)
public static function activeTextField($model,$attribute,$htmlOptions=array())
{
    
self::resolveNameID($model,$attribute,$htmlOptions);
    
self::clientChange('change',$htmlOptions);
    return 
self::activeInputField('text',$model,$attribute,$htmlOptions);
}

Generates a text field input for a model attribute. If the attribute has input error, the input field's CSS class will be appended with errorCss.

addErrorCss() method
protected static void addErrorCss(array &$htmlOptions)
$htmlOptions array HTML options to be modified
Source Code: framework/web/helpers/CHtml.php#1826 (show)
protected static function addErrorCss(&$htmlOptions)
{
    if(isset(
$htmlOptions['class']))
        
$htmlOptions['class'].=' '.self::$errorCss;
    else
        
$htmlOptions['class']=self::$errorCss;
}

Appends errorCss to the 'class' attribute.

ajax() method
public static string ajax(array $options)
$options array AJAX options. The valid options are specified in the jQuery ajax documentation. The following special options are added for convenience:
  • update: string, specifies the selector whose HTML content should be replaced by the AJAX request result.
  • replace: string, specifies the selector whose target should be replaced by the AJAX request result.
Note, if you specify the 'success' option, the above options will be ignored.
{return} string the generated JavaScript
Source Code: framework/web/helpers/CHtml.php#923 (show)
public static function ajax($options)
{
    
Yii::app()->getClientScript()->registerCoreScript('jquery');
    if(!isset(
$options['url']))
        
$options['url']='js:location.href';
    else
        
$options['url']=self::normalizeUrl($options['url']);
    if(!isset(
$options['cache']))
        
$options['cache']=false;
    if(!isset(
$options['data']) && isset($options['type']))
        
$options['data']='js:jQuery(this).parents("form").serialize()';
    foreach(array(
'beforeSend','complete','error','success') as $name)
    {
        if(isset(
$options[$name]) && strpos($options[$name],'js:')!==0)
            
$options[$name]='js:'.$options[$name];
    }
    if(isset(
$options['update']))
    {
        if(!isset(
$options['success']))
            
$options['success']='js:function(html){jQuery("'.$options['update'].'").html(html)}';
        unset(
$options['update']);
    }
    if(isset(
$options['replace']))
    {
        if(!isset(
$options['success']))
            
$options['success']='js:function(html){jQuery("'.$options['replace'].'").replaceWith(html)}';
        unset(
$options['replace']);
    }
    return 
'jQuery.ajax('.CJavaScript::encode($options).');';
}

Generates the JavaScript that initiates an AJAX request.

ajaxButton() method
public static string ajaxButton(string $label, mixed $url, array $ajaxOptions=array ( ), array $htmlOptions=array ( ))
$label string the button label
$url mixed the URL for the AJAX request. If empty, it is assumed to be the current URL. See normalizeUrl for more details.
$ajaxOptions array AJAX options (see ajax)
$htmlOptions array additional HTML attributes. Besides normal HTML attributes, a few special attributes are also recognized (see clientChange and tag for more details.)
{return} string the generated button
Source Code: framework/web/helpers/CHtml.php#887 (show)
public static function ajaxButton($label,$url,$ajaxOptions=array(),$htmlOptions=array())
{
    
$ajaxOptions['url']=$url;
    
$htmlOptions['ajax']=$ajaxOptions;
    return 
self::button($label,$htmlOptions);
}

Generates a push button that can initiate AJAX requests.

public static string ajaxLink(string $text, mixed $url, array $ajaxOptions=array ( ), array $htmlOptions=array ( ))
$text string the link body (it will NOT be HTML-encoded.)
$url mixed the URL for the AJAX request. If empty, it is assumed to be the current URL. See normalizeUrl for more details.
$ajaxOptions array AJAX options (see ajax)
$htmlOptions array additional HTML attributes. Besides normal HTML attributes, a few special attributes are also recognized (see clientChange and tag for more details.)
{return} string the generated link
Source Code: framework/web/helpers/CHtml.php#868 (show)
public static function ajaxLink($text,$url,$ajaxOptions=array(),$htmlOptions=array())
{
    if(!isset(
$htmlOptions['href']))
        
$htmlOptions['href']='#';
    
$ajaxOptions['url']=$url;
    
$htmlOptions['ajax']=$ajaxOptions;
    
self::clientChange('click',$htmlOptions);
    return 
self::tag('a',$htmlOptions,$text);
}

Generates a link that can initiate AJAX requests.

See Also

ajaxSubmitButton() method
public static string ajaxSubmitButton(string $label, mixed $url, array $ajaxOptions=array ( ), array $htmlOptions=array ( ))
$label string the button label
$url mixed the URL for the AJAX request. If empty, it is assumed to be the current URL. See normalizeUrl for more details.
$ajaxOptions array AJAX options (see ajax)
$htmlOptions array additional HTML attributes. Besides normal HTML attributes, a few special attributes are also recognized (see clientChange and tag for more details.)
{return} string the generated button
Source Code: framework/web/helpers/CHtml.php#903 (show)
public static function ajaxSubmitButton($label,$url,$ajaxOptions=array(),$htmlOptions=array())
{
    
$ajaxOptions['type']='POST';
    return 
self::ajaxButton($label,$url,$ajaxOptions,$htmlOptions);
}

Generates a push button that can submit the current form in POST method.

asset() method
public static string asset(string $path, boolean $hashByName=false)
$path string the path of the asset to be published
$hashByName boolean whether the published directory should be named as the hashed basename. If false, the name will be the hashed dirname of the path being published. Defaults to false. Set true if the path being published is shared among different extensions.
{return} string the asset URL
Source Code: framework/web/helpers/CHtml.php#963 (show)
public static function asset($path,$hashByName=false)
{
    return 
Yii::app()->getAssetManager()->publish($path,$hashByName);
}

Generates the URL for the published assets.

beginForm() method (available since v1.0.4)
public static string beginForm(mixed $action='', string $method='post', array $htmlOptions=array ( ))
$action mixed the form action URL (see normalizeUrl for details about this parameter.)
$method string form method (e.g. post, get)
$htmlOptions array additional HTML attributes (see tag).
{return} string the generated form tag.
Source Code: framework/web/helpers/CHtml.php#266 (show)
public static function beginForm($action='',$method='post',$htmlOptions=array())
{
    
$htmlOptions['action']=self::normalizeUrl($action);
    
$htmlOptions['method']=$method;
    
$form=self::tag('form',$htmlOptions,false,false);
    
$request=Yii::app()->request;
    if(
$request->enableCsrfValidation)
    {
        
$token=self::hiddenField($request->csrfTokenName,$request->getCsrfToken(),array('id'=>false));
        
$form.="\n".self::tag('div',array('style'=>'display:none'),$token);
    }
    return 
$form;
}

Generates an opening form tag. Note, only the open tag is generated. A close tag should be placed manually at the end of the form.

See Also

button() method
public static string button(string $label='button', array $htmlOptions=array ( ))
$label string the button label
$htmlOptions array additional HTML attributes. Besides normal HTML attributes, a few special attributes are also recognized (see clientChange and tag for more details.)
{return} string the generated button tag
Source Code: framework/web/helpers/CHtml.php#376 (show)
public static function button($label='button',$htmlOptions=array())
{
    if(!isset(
$htmlOptions['name']))
        
$htmlOptions['name']=self::ID_PREFIX.self::$count++;
    if(!isset(
$htmlOptions['type']))
        
$htmlOptions['type']='button';
    if(!isset(
$htmlOptions['value']))
        
$htmlOptions['value']=$label;
    
self::clientChange('click',$htmlOptions);
    return 
self::tag('input',$htmlOptions);
}

Generates a button.

See Also

cdata() method
public static string cdata(string $text)
$text string the string to be enclosed
{return} string the CDATA tag with the enclosed content.
Source Code: framework/web/helpers/CHtml.php#148 (show)
public static function cdata($text)
{
    return 
'<![CDATA[' $text ']]>';
}

Encloses the given string within a CDATA tag.

checkBox() method
public static string checkBox(string $name, boolean $checked=false, array $htmlOptions=array ( ))
$name string the input name
$checked boolean whether the check box is checked
$htmlOptions array additional HTML attributes. Besides normal HTML attributes, a few special attributes are also recognized (see clientChange and tag for more details.)
{return} string the generated check box
Source Code: framework/web/helpers/CHtml.php#615 (show)
public static function checkBox($name,$checked=false,$htmlOptions=array())
{
    if(
$checked)
        
$htmlOptions['checked']='checked';
    else
        unset(
$htmlOptions['checked']);
    
$value=isset($htmlOptions['value']) ? $htmlOptions['value'] : 1;
    
self::clientChange('click',$htmlOptions);
    return 
self::inputField('checkbox',$name,$value,$htmlOptions);
}

Generates a check box.

checkBoxList() method
public static string checkBoxList(string $name, mixed $select, array $data, array $htmlOptions=array ( ))
$name string name of the check box list. You can use this name to retrieve the selected value(s) once the form is submitted.
$select mixed selection of the check boxes. This can be either a string for single selection or an array for multiple selections.
$data array value-label pairs used to generate the check box list. Note, the values will be automatically HTML-encoded, while the labels will not.
$htmlOptions array addtional HTML options. The options will be applied to each checkbox input. The following special options are recognized:
  • template: string, specifies how each checkbox is rendered. Defaults to "{input} {label}", where "{input}" will be replaced by the generated check box input tag while "{label}" be replaced by the corresponding check box label.
  • separator: string, specifies the string that separates the generated check boxes.
  • checkAll: string, specifies the label for the "check all" checkbox. If this option is specified, a 'check all' checkbox will be displayed. Clicking on this checkbox will cause all checkboxes checked or unchecked. This option has been available since version 1.0.4.
  • checkAllLast: boolean, specifies whether the 'check all' checkbox should be displayed at the end of the checkbox list. If this option is not set (default) or is false, the 'check all' checkbox will be displayed at the beginning of the checkbox list. This option has been available since version 1.0.4.
  • labelOptions: array, specifies the additional HTML attributes to be rendered for every label tag in the list. This option has been available since version 1.0.10.
{return} string the generated check box list
Source Code: framework/web/helpers/CHtml.php#744 (show)
public static function checkBoxList($name,$select,$data,$htmlOptions=array())
{
    
$template=isset($htmlOptions['template'])?$htmlOptions['template']:'{input} {label}';
    
$separator=isset($htmlOptions['separator'])?$htmlOptions['separator']:"<br/>\n";
    unset(
$htmlOptions['template'],$htmlOptions['separator']);

    if(
substr($name,-2)!=='[]')
        
$name.='[]';

    if(isset(
$htmlOptions['checkAll']))
    {
        
$checkAllLabel=$htmlOptions['checkAll'];
        
$checkAllLast=isset($htmlOptions['checkAllLast']) && $htmlOptions['checkAllLast'];
    }
    unset(
$htmlOptions['checkAll'],$htmlOptions['checkAllLast']);

    
$labelOptions=isset($htmlOptions['labelOptions'])?$htmlOptions['labelOptions']:array();
    unset(
$htmlOptions['labelOptions']);

    
$items=array();
    
$baseID=self::getIdByName($name);
    
$id=0;
    
$checkAll=true;

    foreach(
$data as $value=>$label)
    {
        
$checked=!is_array($select) && !strcmp($value,$select) || is_array($select) && in_array($value,$select);
        
$checkAll=$checkAll && $checked;
        
$htmlOptions['value']=$value;
        
$htmlOptions['id']=$baseID.'_'.$id++;
        
$option=self::checkBox($name,$checked,$htmlOptions);
        
$label=self::label($label,$htmlOptions['id'],$labelOptions);
        
$items[]=strtr($template,array('{input}'=>$option,'{label}'=>$label));
    }

    if(isset(
$checkAllLabel))
    {
        
$htmlOptions['value']=1;
        
$htmlOptions['id']=$id=$baseID.'_all';
        
$option=self::checkBox($id,$checkAll,$htmlOptions);
        
$label=self::label($checkAllLabel,$id,$labelOptions);
        
$item=strtr($template,array('{input}'=>$option,'{label}'=>$label));
        if(
$checkAllLast)
            
$items[]=$item;
        else
            
array_unshift($items,$item);
        
$name=strtr($name,array('['=>'\\[',']'=>'\\]'));
        
$js=<<<EOD
jQuery('#$id').click(function() {
var checked=this.checked;
jQuery("input[name='
$name']").each(function() {
    this.checked=checked;
});
});

jQuery("input[name='
$name']").click(function() {
jQuery('#
$id').attr('checked', jQuery("input[name='$name']").length==jQuery("input[name='$name'][checked=true]").length);
});
EOD;
        
$cs=Yii::app()->getClientScript();
        
$cs->registerCoreScript('jquery');
        
$cs->registerScript($id,$js);
    }

    return 
implode($separator,$items);
}

Generates a check box list. A check box list allows multiple selection, like listBox. As a result, the corresponding POST value is an array.

clientChange() method
protected static void clientChange(string $event, array &$htmlOptions)
$event string event name (without 'on')
$htmlOptions array HTML attributes which may contain the following special attributes specifying the client change behaviors:
  • submit: string, specifies the URL that the button should submit to. If empty, the current requested URL will be used.
  • params: array, name-value pairs that should be submitted together with the form. This is only used when 'submit' option is specified.
  • csrf: boolean, whether a CSRF token should be submitted when CHttpRequest::enableCsrfValidation is true. Defaults to false. This option has been available since version 1.0.7. You may want to set this to be true if there is no enclosing form around this element. This option is meaningful only when 'submit' option is set.
  • return: boolean, the return value of the javascript. Defaults to false, meaning that the execution of javascript would not cause the default behavior of the event. This option has been available since version 1.0.2.
  • confirm: string, specifies the message that should show in a pop-up confirmation dialog.
  • ajax: array, specifies the AJAX options (see ajax).
Source Code: framework/web/helpers/CHtml.php#1724 (show)
protected static function clientChange($event,&$htmlOptions)
{
    if(!isset(
$htmlOptions['submit']) && !isset($htmlOptions['confirm']) && !isset($htmlOptions['ajax']))
        return;

    if(isset(
$htmlOptions['return']) && $htmlOptions['return'])
        
$return='return true';
    else
        
$return='return false';

    if(isset(
$htmlOptions['on'.$event]))
    {
        
$handler=trim($htmlOptions['on'.$event],';').';';
        unset(
$htmlOptions['on'.$event]);
    }
    else
        
$handler='';

    if(isset(
$htmlOptions['id']))
        
$id=$htmlOptions['id'];
    else
        
$id=$htmlOptions['id']=isset($htmlOptions['name'])?$htmlOptions['name']:self::ID_PREFIX.self::$count++;

    
$cs=Yii::app()->getClientScript();
    
$cs->registerCoreScript('jquery');

    if(isset(
$htmlOptions['submit']))
    {
        
$cs->registerCoreScript('yii');
        
$request=Yii::app()->getRequest();
        if(
$request->enableCsrfValidation && isset($htmlOptions['csrf']) && $htmlOptions['csrf'])
            
$htmlOptions['params'][$request->csrfTokenName]=$request->getCsrfToken();
        if(isset(
$htmlOptions['params']))
            
$params=CJavaScript::encode($htmlOptions['params']);
        else
            
$params='{}';
        if(
$htmlOptions['submit']!=='')
            
$url=CJavaScript::quote(self::normalizeUrl($htmlOptions['submit']));
        else
            
$url='';
        
$handler.="jQuery.yii.submitForm(this,'$url',$params);{$return};";
    }

    if(isset(
$htmlOptions['ajax']))
        
$handler.=self::ajax($htmlOptions['ajax'])."{$return};";

    if(isset(
$htmlOptions['confirm']))
    {
        
$confirm='confirm(\''.CJavaScript::quote($htmlOptions['confirm']).'\')';
        if(
$handler!=='')
            
$handler="if($confirm) {".$handler."} else return false;";
        else
            
$handler="return $confirm;";
    }

    
$cs->registerScript('Yii.CHtml.#'.$id,"jQuery('#$id').$event(function(){{$handler}});");
    unset(
$htmlOptions['params'],$htmlOptions['submit'],$htmlOptions['ajax'],$htmlOptions['confirm'],$htmlOptions['return'],$htmlOptions['csrf']);
}

Generates the JavaScript with the specified client changes.

closeTag() method
public static string closeTag(string $tag)
$tag string the tag name
{return} string the generated HTML element tag
Source Code: framework/web/helpers/CHtml.php#138 (show)
public static function closeTag($tag)
{
    return 
'</'.$tag.'>';
}

Generates a close HTML element.

css() method
public static string css(string $text, string $media='')
$text string the CSS content
$media string the media that this CSS should apply to.
{return} string the CSS properly enclosed
Source Code: framework/web/helpers/CHtml.php#202 (show)
public static function css($text,$media='')
{
    if(
$media!=='')
        
$media=' media="'.$media.'"';
    return 
"<style type=\"text/css\"{$media}>\n/*<![CDATA[*/\n{$text}\n/*]]>*/\n</style>";
}

Encloses the given CSS content with a CSS tag.

cssFile() method
public static string cssFile(string $url, string $media='')
$url string the CSS URL
$media string the media that this CSS should apply to.
{return} string the CSS link.
Source Code: framework/web/helpers/CHtml.php#215 (show)
public static function cssFile($url,$media='')
{
    if(
$media!=='')
        
$media=' media="'.$media.'"';
    return 
'<link rel="stylesheet" type="text/css" href="'.self::encode($url).'"'.$media.' />';
}

Links to the specified CSS file.

public static string dropDownList(string $name, string $select, array $data, array $htmlOptions=array ( ))
$name string the input name
$select string the selected value
$data array data for generating the list options (value=>display). You may use listData to generate this data. Please refer to listOptions on how this data is used to generate the list options. Note, the values and labels will be automatically HTML-encoded by this method.
$htmlOptions array additional HTML attributes. Besides normal HTML attributes, a few special attributes are recognized. See clientChange and tag for more details. In addition, the following options are also supported specifically for dropdown list:
  • prompt: string, specifies the prompt text shown as the first list option. Its value is empty.
  • empty: string, specifies the text corresponding to empty selection. Its value is empty. Starting from version 1.0.10, the 'empty' option can also be an array of value-label pairs. Each pair will be used to render a list option at the beginning.
  • options: array, specifies additional attributes for each OPTION tag. The array keys must be the option values, and the array values are the extra OPTION tag attributes in the name-value pairs. For example,
        array(
            'value1'=>array('disabled'=>true, 'label'=>'value 1'),
            'value2'=>array('label'=>'value 2'),
        );
    
    This option has been available since version 1.0.3.
{return} string the generated drop down list
Source Code: framework/web/helpers/CHtml.php#659 (show)
public static function dropDownList($name,$select,$data,$htmlOptions=array())
{
    
$htmlOptions['name']=$name;
    if(!isset(
$htmlOptions['id']))
        
$htmlOptions['id']=self::getIdByName($name);
    
self::clientChange('change',$htmlOptions);
    
$options="\n".self::listOptions($select,$data,$htmlOptions);
    return 
self::tag('select',$htmlOptions,$options);
}

Generates a drop down list.

encode() method
public static string encode(string $text)
$text string data to be encoded
{return} string the encoded data
Source Code: framework/web/helpers/CHtml.php#69 (show)
public static function encode($text)
{
    return 
htmlspecialchars($text,ENT_QUOTES,Yii::app()->charset);
}

Encodes special characters into HTML entities. The application charset will be used for encoding.

encodeArray() method (available since v1.0.4)
public static array encodeArray(array $data)
$data array data to be encoded
{return} array the encoded data
Source Code: framework/web/helpers/CHtml.php#84 (show)
public static function encodeArray($data)
{
    
$d=array();
    foreach(
$data as $key=>$value)
    {
        if(
is_string($key))
            
$key=htmlspecialchars($key,ENT_QUOTES,Yii::app()->charset);
        if(
is_string($value))
            
$value=htmlspecialchars($value,ENT_QUOTES,Yii::app()->charset);
        else if(
is_array($value))
            
$value=self::encodeArray($value);
        
$d[$key]=$value;
    }
    return 
$d;
}

Encodes special characters in an array of strings into HTML entities. Both the array keys and values will be encoded if needed. If a value is an array, this method will also encode it recursively. The application charset will be used for encoding.

endForm() method (available since v1.0.4)
public static string endForm()
{return} string the generated tag
Source Code: framework/web/helpers/CHtml.php#286 (show)
public static function endForm()
{
    return 
'</form>';
}

Generates a closing form tag.

See Also

error() method
public static string error(CModel $model, string $attribute, array $htmlOptions=array ( ))
$model CModel the data model
$attribute string the attribute name
$htmlOptions array additional HTML attributes to be rendered in the container div tag. This parameter has been available since version 1.0.7.
{return} string the error display. Empty if no errors are found.
Source Code: framework/web/helpers/CHtml.php#1489 (show)
public static function error($model,$attribute,$htmlOptions=array())
{
    
$error=$model->getError($attribute);
    if(
$error!='')
    {
        if(!isset(
$htmlOptions['class']))
            
$htmlOptions['class']=self::$errorMessageCss;
        return 
self::tag('div',$htmlOptions,$error);
    }
    else
        return 
'';
}

Displays the first validation error for a model attribute.

errorSummary() method
public static string errorSummary(mixed $model, string $header=NULL, string $footer=NULL, array $htmlOptions=array ( ))
$model mixed the models whose input errors are to be displayed. This can be either a single model or an array of models.
$header string a piece of HTML code that appears in front of the errors
$footer string a piece of HTML code that appears at the end of the errors
$htmlOptions array additional HTML attributes to be rendered in the container div tag. This parameter has been available since version 1.0.7.
{return} string the error summary. Empty if no errors are found.
Source Code: framework/web/helpers/CHtml.php#1451 (show)
public static function errorSummary($model,$header=null,$footer=null,$htmlOptions=array())
{
    
$content='';
    if(!
is_array($model))
        
$model=array($model);
    foreach(
$model as $m)
    {
        foreach(
$m->getErrors() as $errors)
        {
            foreach(
$errors as $error)
            {
                if(
$error!='')
                    
$content.="<li>$error</li>\n";
            }
        }
    }
    if(
$content!=='')
    {
        if(
$header===null)
            
$header='<p>'.Yii::t('yii','Please fix the following input errors:').'</p>';
        if(!isset(
$htmlOptions['class']))
            
$htmlOptions['class']=self::$errorSummaryCss;
        return 
self::tag('div',$htmlOptions,$header."\n<ul>\n$content</ul>".$footer);
    }
    else
        return 
'';
}

Displays a summary of validation errors for one or several models.

fileField() method
public static string fileField(string $name, string $value='', array $htmlOptions=array ( ))
$name string the input name
$value string the input value
$htmlOptions array additional HTML attributes (see tag).
{return} string the generated input field
Source Code: framework/web/helpers/CHtml.php#560 (show)
public static function fileField($name,$value='',$htmlOptions=array())
{
    return 
self::inputField('file',$name,$value,$htmlOptions);
}

Generates a file input. Note, you have to set the enclosing form's 'enctype' attribute to be 'multipart/form-data'. After the form is submitted, the uploaded file information can be obtained via $_FILES[$name] (see PHP documentation).

See Also

form() method
public static string form(mixed $action='', string $method='post', array $htmlOptions=array ( ))
$action mixed the form action URL (see normalizeUrl for details about this parameter.)
$method string form method (e.g. post, get)
$htmlOptions array additional HTML attributes (see tag).
{return} string the generated form tag.
Source Code: framework/web/helpers/CHtml.php#250 (show)
public static function form($action='',$method='post',$htmlOptions=array())
{
    return 
self::beginForm($action,$method,$htmlOptions);
}

Generates an opening form tag. This is a shortcut to beginForm.

getActiveId() method
public static string getActiveId(CModel $model, string $attribute)
$model CModel the data model
$attribute string the attribute
{return} string the element ID for the active field corresponding to the specified model and attribute.
Source Code: framework/web/helpers/CHtml.php#1434 (show)
public static function getActiveId($model,$attribute)
{
    return 
self::activeId($model,$attribute);
}

Returns the element ID that is used by methods such as activeTextField. This method has been deprecated since version 1.0.5. Please use activeId instead.

getIdByName() method
public static string getIdByName($name)
$name
{return} string the ID generated based on name.
Source Code: framework/web/helpers/CHtml.php#1572 (show)
public static function getIdByName($name)
{
    return 
str_replace(array('[]''][''['']'), array('''_''_'''), $name);
}

Generates a valid HTML ID based the name.

hiddenField() method
public static string hiddenField(string $name, string $value='', array $htmlOptions=array ( ))
$name string the input name
$value string the input value
$htmlOptions array additional HTML attributes (see tag).
{return} string the generated input field
Source Code: framework/web/helpers/CHtml.php#528 (show)
public static function hiddenField($name,$value='',$htmlOptions=array())
{
    return 
self::inputField('hidden',$name,$value,$htmlOptions);
}

Generates a hidden input.

See Also

htmlButton() method (available since v1.0.8)
public static string htmlButton(string $label='button', array $htmlOptions=array ( ))
$label string the button label. Note that this value will be directly inserted in the button element without being HTML-encoded.
$htmlOptions array additional HTML attributes. Besides normal HTML attributes, a few special attributes are also recognized (see clientChange and tag for more details.)
{return} string the generated button tag
Source Code: framework/web/helpers/CHtml.php#400 (show)
public static function htmlButton($label='button',$htmlOptions=array())
{
    if(!isset(
$htmlOptions['name']))
        
$htmlOptions['name']=self::ID_PREFIX.self::$count++;
    if(!isset(
$htmlOptions['type']))
        
$htmlOptions['type']='button';
    
self::clientChange('click',$htmlOptions);
    return 
self::tag('button',$htmlOptions,$label);
}

Generates a button using HTML button tag. This method is similar to button except that it generates a 'button' tag instead of 'input' tag.

See Also

image() method
public static string image(string $src, string $alt='', array $htmlOptions=array ( ))
$src string the image URL
$alt string the alternative text display
$htmlOptions array additional HTML attributes (see tag).
{return} string the generated image tag
Source Code: framework/web/helpers/CHtml.php#361 (show)
public static function image($src,$alt='',$htmlOptions=array())
{
    
$htmlOptions['src']=$src;
    
$htmlOptions['alt']=$alt;
    return 
self::tag('img',$htmlOptions);
}

Generates an image tag.

imageButton() method
public static string imageButton(string $src, array $htmlOptions=array ( ))
$src string the button label
$htmlOptions array additional HTML attributes. Besides normal HTML attributes, a few special attributes are also recognized (see clientChange and tag for more details.)
{return} string the generated button tag
Source Code: framework/web/helpers/CHtml.php#446 (show)
public static function imageButton($src,$htmlOptions=array())
{
    
$htmlOptions['src']=$src;
    
$htmlOptions['type']='image';
    return 
self::button('submit',$htmlOptions);
}

Generates an image submit button.

See Also

inputField() method
protected static string inputField(string $type, string $name, string $value, array $htmlOptions)
$type string the input type (e.g. 'text', 'radio')
$name string the input name
$value string the input value
$htmlOptions array additional HTML attributes for the HTML tag (see tag).
{return} string the generated input tag
Source Code: framework/web/helpers/CHtml.php#1012 (show)
protected static function inputField($type,$name,$value,$htmlOptions)
{
    
$htmlOptions['type']=$type;
    
$htmlOptions['value']=$value;
    
$htmlOptions['name']=$name;
    if(!isset(
$htmlOptions['id']))
        
$htmlOptions['id']=self::getIdByName($name);
    else if(
$htmlOptions['id']===false)
        unset(
$htmlOptions['id']);
    return 
self::tag('input',$htmlOptions);
}

Generates an input HTML tag. This method generates an input HTML tag based on the given input name and value.

label() method
public static string label(string $label, string $for, array $htmlOptions=array ( ))
$label string label text. Note, you should HTML-encode the text if needed.
$for string the ID of the HTML element that this label is associated with. If this is false, the 'for' attribute for the label tag will not be rendered (since version 1.0.11).
$htmlOptions array additional HTML attributes. Starting from version 1.0.2, the following HTML option is recognized:
<li>required: if this is set and is true, the label will be styled
with CSS class 'required' (customizable with CHtml::$requiredCss),
and be decorated with CHtml::beforeRequiredLabel and
CHtml::afterRequiredLabel.</li>
{return} string the generated label tag
Source Code: framework/web/helpers/CHtml.php#483 (show)
public static function label($label,$for,$htmlOptions=array())
{
    if(
$for===false)
        unset(
$htmlOptions['for']);
    else
        
$htmlOptions['for']=$for;
    if(isset(
$htmlOptions['required']))
    {
        if(
$htmlOptions['required'])
        {
            if(isset(
$htmlOptions['class']))
                
$htmlOptions['class'].=' '.self::$requiredCss;
            else
                
$htmlOptions['class']=self::$requiredCss;
            
$label=self::$beforeRequiredLabel.$label.self::$afterRequiredLabel;
        }
        unset(
$htmlOptions['required']);
    }
    return 
self::tag('label',$htmlOptions,$label);
}

Generates a label tag.

public static string link(string $text, mixed $url='#', array $htmlOptions=array ( ))
$text string link body. It will NOT be HTML-encoded. Therefore you can pass in HTML code such as an image tag.
$url mixed a URL or an action route that can be used to create a URL. See normalizeUrl for more details about how to specify this parameter.
$htmlOptions array additional HTML attributes. Besides normal HTML attributes, a few special attributes are also recognized (see clientChange and tag for more details.)
{return} string the generated hyperlink
Source Code: framework/web/helpers/CHtml.php#329 (show)
public static function link($text,$url='#',$htmlOptions=array())
{
    if(
$url!=='')
        
$htmlOptions['href']=self::normalizeUrl($url);
    
self::clientChange('click',$htmlOptions);
    return 
self::tag('a',$htmlOptions,$text);
}

Generates a hyperlink tag.

linkButton() method
public static string linkButton(string $label='submit', array $htmlOptions=array ( ))
$label string the button label
$htmlOptions array additional HTML attributes. Besides normal HTML attributes, a few special attributes are also recognized (see clientChange and tag for more details.)
{return} string the generated button tag
Source Code: framework/web/helpers/CHtml.php#461 (show)
public static function linkButton($label='submit',$htmlOptions=array())
{
    if(!isset(
$htmlOptions['submit']))
        
$htmlOptions['submit']=isset($htmlOptions['href']) ? $htmlOptions['href'] : '';
    return 
self::link($label,'#',$htmlOptions);
}

Generates a link submit button.

See Also

linkTag() method (available since v1.0.1)
public static string linkTag(string $relation=NULL, string $type=NULL, string $href=NULL, string $media=NULL, array $options=array ( ))
$relation string rel attribute of the link tag. If null, the attribute will not be generated.
$type string type attribute of the link tag. If null, the attribute will not be generated.
$href string href attribute of the link tag. If null, the attribute will not be generated.
$media string media attribute of the link tag. If null, the attribute will not be generated.
$options array other options in name-value pairs
{return} string the generated link tag
Source Code: framework/web/helpers/CHtml.php#183 (show)
public static function linkTag($relation=null,$type=null,$href=null,$media=null,$options=array())
{
    if(
$relation!==null)
        
$options['rel']=$relation;
    if(
$type!==null)
        
$options['type']=$type;
    if(
$href!==null)
        
$options['href']=$href;
    if(
$media!==null)
        
$options['media']=$media;
    return 
self::tag('link',$options);
}

Generates a link tag that can be inserted in the head section of HTML page. Do not confuse this method with link(). The latter generates a hyperlink.

listBox() method
public static string listBox(string $name, mixed $select, array $data, array $htmlOptions=array ( ))
$name string the input name
$select mixed the selected value(s). This can be either a string for single selection or an array for multiple selections.
$data array data for generating the list options (value=>display) You may use listData to generate this data. Please refer to listOptions on how this data is used to generate the list options. Note, the values and labels will be automatically HTML-encoded by this method.
$htmlOptions array additional HTML attributes. Besides normal HTML attributes, a few special attributes are also recognized. See clientChange and tag for more details. In addition, the following options are also supported specifically for list box:
  • prompt: string, specifies the prompt text shown as the first list option. Its value is empty.
  • empty: string, specifies the text corresponding to empty selection. Its value is empty. Starting from version 1.0.10, the 'empty' option can also be an array of value-label pairs. Each pair will be used to render a list option at the beginning.
  • options: array, specifies additional attributes for each OPTION tag. The array keys must be the option values, and the array values are the extra OPTION tag attributes in the name-value pairs. For example,
        array(
            'value1'=>array('disabled'=>true, 'label'=>'value 1'),
            'value2'=>array('label'=>'value 2'),
        );
    
    This option has been available since version 1.0.3.
{return} string the generated list box
Source Code: framework/web/helpers/CHtml.php#702 (show)
public static function listBox($name,$select,$data,$htmlOptions=array())
{
    if(!isset(
$htmlOptions['size']))
        
$htmlOptions['size']=4;
    if(isset(
$htmlOptions['multiple']))
    {
        if(
substr($name,-2)!=='[]')
            
$name.='[]';
    }
    return 
self::dropDownList($name,$select,$data,$htmlOptions);
}

Generates a list box.

listData() method
public static array listData(array $models, string $valueField, string $textField, string $groupField='')
$models array a list of model objects. Starting from version 1.0.3, this parameter can also be an array of associative arrays (e.g. results of CDbCommand::queryAll).
$valueField string the attribute name for list option values
$textField string the attribute name for list option texts
$groupField string the attribute name for list option group names. If empty, no group will be generated.
{return} array the list data that can be used in dropDownList and listBox
Source Code: framework/web/helpers/CHtml.php#1514 (show)
public static function listData($models,$valueField,$textField,$groupField='')
{
    
$listData=array();
    if(
$groupField==='')
    {
        foreach(
$models as $model)
        {
            
$value=self::value($model,$valueField);
            
$text=self::value($model,$textField);
            
$listData[$value]=$text;
        }
    }
    else
    {
        foreach(
$models as $model)
        {
            
$group=self::value($model,$groupField);
            
$value=self::value($model,$valueField);
            
$text=self::value($model,$textField);
            
$listData[$group][$value]=$text;
        }
    }
    return 
$listData;
}

Generates the data suitable for dropDownList and listBox. Note, this method does not HTML-encode the generated data. You may call encodeArray to encode it if needed. Please refer to the value method on how to specify value field, text field and group field.

listOptions() method
public static string listOptions(mixed $selection, array $listData, array &$htmlOptions)
$selection mixed the selected value(s). This can be either a string for single selection or an array for multiple selections.
$listData array the option data (see listData)
$htmlOptions array additional HTML attributes. The following two special attributes are recognized:
  • encode: boolean, specifies whether to encode the values. Defaults to true. This option has been available since version 1.0.5.
  • prompt: string, specifies the prompt text shown as the first list option. Its value is empty.
  • empty: string, specifies the text corresponding to empty selection. Its value is empty. Starting from version 1.0.10, the 'empty' option can also be an array of value-label pairs. Each pair will be used to render a list option at the beginning.
  • options: array, specifies additional attributes for each OPTION tag. The array keys must be the option values, and the array values are the extra OPTION tag attributes in the name-value pairs. For example,
        array(
            'value1'=>array('disabled'=>true, 'label'=>'value 1'),
            'value2'=>array('label'=>'value 2'),
        );
    
    This option has been available since version 1.0.3.
{return} string the generated list options
Source Code: framework/web/helpers/CHtml.php#1652 (show)
public static function listOptions($selection,$listData,&$htmlOptions)
{
    
$raw=isset($htmlOptions['encode']) && !$htmlOptions['encode'];
    
$content='';
    if(isset(
$htmlOptions['prompt']))
    {
        
$content.='<option value="">'.($raw?$htmlOptions['prompt'] : self::encode($htmlOptions['prompt']))."</option>\n";
        unset(
$htmlOptions['prompt']);
    }
    if(isset(
$htmlOptions['empty']))
    {
        if(!
is_array($htmlOptions['empty']))
            
$htmlOptions['empty']=array(''=>$htmlOptions['empty']);
        foreach(
$htmlOptions['empty'] as $value=>$label)
        {
            if(
$raw)
                
$content.='<option value="'.$value.'">'.$label."</option>\n";
            else
                
$content.='<option value="'.self::encode($value).'">'.self::encode($label)."</option>\n";
        }
        unset(
$htmlOptions['empty']);
    }

    if(isset(
$htmlOptions['options']))
    {
        
$options=$htmlOptions['options'];
        unset(
$htmlOptions['options']);
    }
    else
        
$options=array();

    foreach(
$listData as $key=>$value)
    {
        if(
is_array($value))
        {
            
$content.='<optgroup label="'.($raw?$key self::encode($key))."\">\n";
            
$dummy=array('options'=>$options);
            if(isset(
$htmlOptions['encode']))
                
$dummy['encode']=$htmlOptions['encode'];
            
$content.=self::listOptions($selection,$value,$dummy);
            
$content.='</optgroup>'."\n";
        }
        else
        {
            
$attributes=array('value'=>(string)$key'encode'=>!$raw);
            if(!
is_array($selection) && !strcmp($key,$selection) || is_array($selection) && in_array($key,$selection))
                
$attributes['selected']='selected';
            if(isset(
$options[$key]))
                
$attributes=array_merge($attributes,$options[$key]);
            
$content.=self::tag('option',$attributes,$raw?(string)$value self::encode((string)$value))."\n";
        }
    }
    return 
$content;
}

Generates the list options.

mailto() method (available since v1.0.1)
public static string mailto(string $text, string $email='', array $htmlOptions=array ( ))
$text string link body. It will NOT be HTML-encoded. Therefore you can pass in HTML code such as an image tag.
$email string email address. If this is empty, the first parameter (link body) will be treated as the email address.
$htmlOptions array additional HTML attributes. Besides normal HTML attributes, a few special attributes are also recognized (see clientChange and tag for more details.)
{return} string the generated mailto link
Source Code: framework/web/helpers/CHtml.php#347 (show)
public static function mailto($text,$email='',$htmlOptions=array())
{
    if(
$email==='')
        
$email=$text;
    return 
self::link($text,'mailto:'.$email,$htmlOptions);
}

Generates a mailto link.

See Also

metaTag() method (available since v1.0.1)
public static string metaTag(string $content, string $name=NULL, string $httpEquiv=NULL, array $options=array ( ))
$content string content attribute of the meta tag
$name string name attribute of the meta tag. If null, the attribute will not be generated
$httpEquiv string http-equiv attribute of the meta tag. If null, the attribute will not be generated
$options array other options in name-value pairs (e.g. 'scheme', 'lang')
{return} string the generated meta tag
Source Code: framework/web/helpers/CHtml.php#162 (show)
public static function metaTag($content,$name=null,$httpEquiv=null,$options=array())
{
    
$options['content']=$content;
    if(
$name!==null)
        
$options['name']=$name;
    if(
$httpEquiv!==null)
        
$options['http-equiv']=$httpEquiv;
    return 
self::tag('meta',$options);
}

Generates a meta tag that can be inserted in the head section of HTML page.

normalizeUrl() method
public static void normalizeUrl(mixed $url)
$url mixed the parameter to be used to generate a valid URL
Source Code: framework/web/helpers/CHtml.php#986 (show)
public static function normalizeUrl($url)
{
    if(
is_array($url))
    {
        if(isset(
$url[0]))
        {
            if((
$c=Yii::app()->getController())!==null)
                
$url=$c->createUrl($url[0],array_splice($url,1));
            else
                
$url=Yii::app()->createUrl($url[0],array_splice($url,1));
        }
        else
            
$url='';
    }
    return 
$url==='' Yii::app()->getRequest()->getUrl() : $url;
}

Normalizes the input parameter to be a valid URL.

If the input parameter is an empty string, the currently requested URL will be returned.

If the input parameter is a non-empty string, it is treated as a valid URL and will be returned without any change.

If the input parameter is an array, it is treated as a controller route and a list of GET parameters, and the CController::createUrl method will be invoked to create a URL. In this case, the first array element refers to the controller route, and the rest key-value pairs refer to the additional GET parameters for the URL. For example, array('post/list', 'page'=>3) may be used to generate the URL /index.php?r=post/list&page=3.

openTag() method
public static string openTag(string $tag, array $htmlOptions=array ( ))
$tag string the tag name
$htmlOptions array the element attributes. The values will be HTML-encoded using encode(). Since version 1.0.5, if an 'encode' attribute is given and its value is false, the rest of the attribute values will NOT be HTML-encoded.
{return} string the generated HTML element tag
Source Code: framework/web/helpers/CHtml.php#128 (show)
public static function openTag($tag,$htmlOptions=array())
{
    return 
'<' $tag self::renderAttributes($htmlOptions) . '>';
}

Generates an open HTML element.

pageStateField() method
public static string pageStateField(string $value)
$value string the persistent page states in serialized format
{return} string the generated hidden field
Source Code: framework/web/helpers/CHtml.php#313 (show)
public static function pageStateField($value)
{
    return 
'<input type="hidden" name="'.CController::STATE_INPUT_NAME.'" value="'.$value.'" />';
}

Generates a hidden field for storing persistent page states. This method is internally used by statefulForm.

passwordField() method
public static string passwordField(string $name, string $value='', array $htmlOptions=array ( ))
$name string the input name
$value string the input value
$htmlOptions array additional HTML attributes. Besides normal HTML attributes, a few special attributes are also recognized (see clientChange and tag for more details.)
{return} string the generated input field
Source Code: framework/web/helpers/CHtml.php#543 (show)
public static function passwordField($name,$value='',$htmlOptions=array())
{
    
self::clientChange('change',$htmlOptions);
    return 
self::inputField('password',$name,$value,$htmlOptions);
}

Generates a password field input.

radioButton() method
public static string radioButton(string $name, boolean $checked=false, array $htmlOptions=array ( ))
$name string the input name
$checked boolean whether the check box is checked
$htmlOptions array additional HTML attributes. Besides normal HTML attributes, a few special attributes are also recognized (see clientChange and tag for more details.)
{return} string the generated radio button
Source Code: framework/web/helpers/CHtml.php#594 (show)
public static function radioButton($name,$checked=false,$htmlOptions=array())
{
    if(
$checked)
        
$htmlOptions['checked']='checked';
    else
        unset(
$htmlOptions['checked']);
    
$value=isset($htmlOptions['value']) ? $htmlOptions['value'] : 1;
    
self::clientChange('click',$htmlOptions);
    return 
self::inputField('radio',$name,$value,$htmlOptions);
}

Generates a radio button.

radioButtonList() method
public static string radioButtonList(string $name, mixed $select, array $data, array $htmlOptions=array ( ))
$name string name of the radio button list. You can use this name to retrieve the selected value(s) once the form is submitted.
$select mixed selection of the radio buttons. This can be either a string for single selection or an array for multiple selections.
$data array value-label pairs used to generate the radio button list. Note, the values will be automatically HTML-encoded, while the labels will not.
$htmlOptions array addtional HTML options. The options will be applied to each checkbox input. The following special options are recognized:
  • template: string, specifies how each checkbox is rendered. Defaults to "{input} {label}", where "{input}" will be replaced by the generated radio button input tag while "{label}" be replaced by the corresponding radio button label.
  • separator: string, specifies the string that separates the generated radio buttons.
  • labelOptions: array, specifies the additional HTML attributes to be rendered for every label tag in the list. This option has been available since version 1.0.10.
{return} string the generated radio button list
Source Code: framework/web/helpers/CHtml.php#833 (show)
public static function radioButtonList($name,$select,$data,$htmlOptions=array())
{
    
$template=isset($htmlOptions['template'])?$htmlOptions['template']:'{input} {label}';
    
$separator=isset($htmlOptions['separator'])?$htmlOptions['separator']:"<br/>\n";
    unset(
$htmlOptions['template'],$htmlOptions['separator']);

    
$labelOptions=isset($htmlOptions['labelOptions'])?$htmlOptions['labelOptions']:array();
    unset(
$htmlOptions['labelOptions']);

    
$items=array();
    
$baseID=self::getIdByName($name);
    
$id=0;
    foreach(
$data as $value=>$label)
    {
        
$checked=!strcmp($value,$select);
        
$htmlOptions['value']=$value;
        
$htmlOptions['id']=$baseID.'_'.$id++;
        
$option=self::radioButton($name,$checked,$htmlOptions);
        
$label=self::label($label,$htmlOptions['id'],$labelOptions);
        
$items[]=strtr($template,array('{input}'=>$option,'{label}'=>$label));
    }
    return 
implode($separator,$items);
}

Generates a radio button list. A radio button list is like a check box list, except that it only allows single selection.

renderAttributes() method (available since v1.0.5)
protected static string renderAttributes(array $htmlOptions)
$htmlOptions array attributes to be rendered
{return} string the rendering result
Source Code: framework/web/helpers/CHtml.php#1840 (show)
protected static function renderAttributes($htmlOptions)
{
    if(
$htmlOptions===array())
        return 
'';
    
$html='';
    
$raw=isset($htmlOptions['encode']) && !$htmlOptions['encode'];
    unset(
$htmlOptions['encode']);
    if(
$raw)
    {
        foreach(
$htmlOptions as $name=>$value)
            
$html .= ' ' $name '="' $value '"';
    }
    else
    {
        foreach(
$htmlOptions as $name=>$value)
            
$html .= ' ' $name '="' self::encode($value) . '"';
    }
    return 
$html;
}

Renders the HTML tag attributes.

resetButton() method
public static string resetButton(string $label='reset', array $htmlOptions=array ( ))
$label string the button label
$htmlOptions array additional HTML attributes. Besides normal HTML attributes, a few special attributes are also recognized (see clientChange and tag for more details.)
{return} string the generated button tag
Source Code: framework/web/helpers/CHtml.php#432 (show)
public static function resetButton($label='reset',$htmlOptions=array())
{
    
$htmlOptions['type']='reset';
    return 
self::button($label,$htmlOptions);
}

Generates a reset button.

See Also

resolveName() method (available since v1.0.2)
public static string resolveName(CModel $model, string &$attribute)
$model CModel the data model
$attribute string the attribute
{return} string the input name
Source Code: framework/web/helpers/CHtml.php#1810 (show)
public static function resolveName($model,&$attribute)
{
    if((
$pos=strpos($attribute,'['))!==false)
    {
        
$sub=substr($attribute,$pos);
        
$attribute=substr($attribute,0,$pos);
        return 
get_class($model).$sub.'['.$attribute.']';
    }
    else
        return 
get_class($model).'['.$attribute.']';
}

Generates input name for a model attribute. Note, the attribute name may be modified after calling this method if the name contains square brackets (mainly used in tabular input).

resolveNameID() method
public static void resolveNameID(CModel $model, string &$attribute, array &$htmlOptions)
$model CModel the data model
$attribute string the attribute
$htmlOptions array the HTML options
Source Code: framework/web/helpers/CHtml.php#1792 (show)
public static function resolveNameID($model,&$attribute,&$htmlOptions)
{
    
$name=self::resolveName($model,$attribute);
    if(!isset(
$htmlOptions['name']))
        
$htmlOptions['name']=$name;
    if(!isset(
$htmlOptions['id']))
        
$htmlOptions['id']=self::getIdByName($htmlOptions['name']);
}

Generates input name and ID for a model attribute. This method will update the HTML options by setting appropriate 'name' and 'id' attributes. This method may also modify the attribute name if the name contains square brackets (mainly used in tabular input).

script() method
public static string script(string $text)
$text string the JavaScript to be enclosed
{return} string the enclosed JavaScript
Source Code: framework/web/helpers/CHtml.php#227 (show)
public static function script($text)
{
    return 
"<script type=\"text/javascript\">\n/*<![CDATA[*/\n{$text}\n/*]]>*/\n</script>";
}

Encloses the given JavaScript within a script tag.

scriptFile() method
public static string scriptFile(string $url)
$url string URL for the JavaScript file
{return} string the JavaScript file tag
Source Code: framework/web/helpers/CHtml.php#237 (show)
public static function scriptFile($url)
{
    return 
'<script type="text/javascript" src="'.self::encode($url).'"></script>';
}

Includes a JavaScript file.

statefulForm() method
public static string statefulForm(mixed $action='', string $method='post', array $htmlOptions=array ( ))
$action mixed the form action URL (see normalizeUrl for details about this parameter.)
$method string form method (e.g. post, get)
$htmlOptions array additional HTML attributes (see tag).
{return} string the generated form tag.
Source Code: framework/web/helpers/CHtml.php#301 (show)
public static function statefulForm($action='',$method='post',$htmlOptions=array())
{
    return 
self::form($action,$method,$htmlOptions)."\n".
        
self::tag('div',array('style'=>'display:none'),self::pageStateField(''));
}

Generates a stateful form tag. A stateful form tag is similar to form except that it renders an additional hidden field for storing persistent page states. You should use this method to generate a form tag if you want to access persistent page states when the form is submitted.

submitButton() method
public static string submitButton(string $label='submit', array $htmlOptions=array ( ))
$label string the button label
$htmlOptions array additional HTML attributes. Besides normal HTML attributes, a few special attributes are also recognized (see clientChange and tag for more details.)
{return} string the generated button tag
Source Code: framework/web/helpers/CHtml.php#418 (show)
public static function submitButton($label='submit',$htmlOptions=array())
{
    
$htmlOptions['type']='submit';
    return 
self::button($label,$htmlOptions);
}

Generates a submit button.

See Also

tag() method
public static string tag(string $tag, array $htmlOptions=array ( ), mixed $content=false, boolean $closeTag=true)
$tag string the tag name
$htmlOptions array the element attributes. The values will be HTML-encoded using encode(). Since version 1.0.5, if an 'encode' attribute is given and its value is false, the rest of the attribute values will NOT be HTML-encoded.
$content mixed the content to be enclosed between open and close element tags. It will not be HTML-encoded. If false, it means there is no body content.
$closeTag boolean whether to generate the close tag.
{return} string the generated HTML element tag
Source Code: framework/web/helpers/CHtml.php#111 (show)
public static function tag($tag,$htmlOptions=array(),$content=false,$closeTag=true)
{
    
$html='<' $tag self::renderAttributes($htmlOptions);
    if(
$content===false)
        return 
$closeTag $html.' />' $html.'>';
    else
        return 
$closeTag $html.'>'.$content.'</'.$tag.'>' $html.'>'.$content;
}

Generates an HTML element.

textArea() method
public static string textArea(string $name, string $value='', array $htmlOptions=array ( ))
$name string the input name
$value string the input value
$htmlOptions array additional HTML attributes. Besides normal HTML attributes, a few special attributes are also recognized (see clientChange and tag for more details.)
{return} string the generated text area
Source Code: framework/web/helpers/CHtml.php#575 (show)
public static function textArea($name,$value='',$htmlOptions=array())
{
    
$htmlOptions['name']=$name;
    if(!isset(
$htmlOptions['id']))
        
$htmlOptions['id']=self::getIdByName($name);
    
self::clientChange('change',$htmlOptions);
    return 
self::tag('textarea',$htmlOptions,isset($htmlOptions['encode']) && !$htmlOptions['encode'] ? $value self::encode($value));
}

Generates a text area input.

textField() method
public static string textField(string $name, string $value='', array $htmlOptions=array ( ))
$name string the input name
$value string the input value
$htmlOptions array additional HTML attributes. Besides normal HTML attributes, a few special attributes are also recognized (see clientChange and tag for more details.)
{return} string the generated input field
Source Code: framework/web/helpers/CHtml.php#514 (show)
public static function textField($name,$value='',$htmlOptions=array())
{
    
self::clientChange('change',$htmlOptions);
    return 
self::inputField('text',$name,$value,$htmlOptions);
}

Generates a text field input.

value() method (available since v1.0.5)
public static mixed value(mixed $model, string $attribute, mixed $defaultValue=NULL)
$model mixed the model. This can be either an object or an array.
$attribute string the attribute name (use dot to concatenate multiple attributes)
$defaultValue mixed the default value to return when the attribute does not exist
{return} mixed the attribute value
Source Code: framework/web/helpers/CHtml.php#1554 (show)
public static function value($model,$attribute,$defaultValue=null)
{
    foreach(
explode('.',$attribute) as $name)
    {
        if(
is_object($model))
            
$model=$model->$name;
        else if(
is_array($model) && isset($model[$name]))
            
$model=$model[$name];
        else
            return 
$defaultValue;
    }
    return 
$model;
}

Evaluates the value of the specified attribute for the given model. The attribute name can be given in a dot syntax. For example, if the attribute is "author.firstName", this method will return the value of "$model->author->firstName". A default value (passed as the last parameter) will be returned if the attribute does not exist or is broken in the middle (e.g. $model->author is null). The model can be either an object or an array. If the latter, the attribute is treated as a key of the array. For the example of "author.firstName", if would mean the array value "$model['author']['firstName']".