CHtml
| Package |
system.web.helpers |
| Inheritance |
class CHtml |
| Since |
1.0 |
| Version |
$Id: CHtml.php 2210 2010-06-17 19:47:26Z qiang.xue $ |
CHtml is a static class that provides a collection of helper methods for creating HTML views.
Property Details
public static string $afterRequiredLabel;
the HTML code to be appended to the required label.
public static string $beforeRequiredLabel;
the HTML code to be prepended to the required label.
public static integer $count;
the counter for generating automatic input field names.
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.
public static string $errorMessageCss;
the CSS class for displaying error messages (see error).
public static string $errorSummaryCss;
the CSS class for displaying error summaries (see errorSummary).
public static string $requiredCss;
the CSS class for required labels. Defaults to 'required'.
Method Details
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 |
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.
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 |
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.
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:
- 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 drop down list |
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.
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 |
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).
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 |
Generates a hidden input for a model attribute.
public static string activeId( CModel $model, string $attribute)
|
| $model |
CModel |
the data model |
| $attribute |
string |
the attribute |
| {return} |
string |
the generated input field ID |
Generates input field ID for a model attribute.
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 |
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.
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. The following special options are recognized:
- 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. This option has been available since version 1.0.2.
- label: this specifies the label to be displayed. If this is not set,
CModel::getAttributeLabel will be called to get the label for display.
If the label is specified as false, no label will be rendered.
This option has been available since version 1.0.4.
|
| {return} |
string |
the generated label tag |
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.
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 |
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.
If so, it will add a CSS class CHtml::requiredCss to the label,
and decorate the label with CHtml::beforeRequiredLabel and
CHtml::afterRequiredLabel.
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:
- 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 box |
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.
public static string activeName( CModel $model, string $attribute)
|
| $model |
CModel |
the data model |
| $attribute |
string |
the attribute |
| {return} |
string |
the generated input field name |
Generates input field name for a model attribute.
Unlike resolveName, this method does NOT modify the attribute name.
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 |
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.
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 |
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.
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 radio button 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 |
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.
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 |
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.
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 |
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.
|
protected static void addErrorCss(array $htmlOptions)
|
| $htmlOptions |
array |
HTML options to be modified |
Appends errorCss to the 'class' attribute.
|
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 |
Generates the JavaScript that initiates an AJAX request.
|
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 |
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 |
Generates a link that can initiate AJAX requests.
|
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 |
Generates a push button that can submit the current form in POST 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 |
Generates the URL for the published assets.
|
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. |
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.
|
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 |
Generates a button.
|
public static string cdata(string $text)
|
| $text |
string |
the string to be enclosed |
| {return} |
string |
the CDATA tag with the enclosed content. |
Encloses the given string within a CDATA tag.
|
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.)
Since version 1.1.2, a special option named 'uncheckValue' is available that can be used to specify
the value returned when the checkbox is not checked. When set, a hidden field is rendered so that
when the checkbox is not checked, we can still obtain the posted uncheck value.
If 'uncheckValue' is not set or set to NULL, the hidden field will not be rendered. |
| {return} |
string |
the generated check box |
Generates a check box.
|
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 |
Generates a check box list.
A check box list allows multiple selection, like listBox.
As a result, the corresponding POST value is an array.
|
protected static void clientChange(string $event, array $htmlOptions, boolean $live=true)
|
| $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).
|
| $live |
boolean |
whether the event should be "live" (a jquery event concept). Defaults to true.
This parameter has been available since version 1.1.1. |
Generates the JavaScript with the specified client changes.
|
public static string closeTag(string $tag)
|
| $tag |
string |
the tag name |
| {return} |
string |
the generated HTML element tag |
Generates a close HTML element.
|
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 |
Encloses the given CSS content with a CSS tag.
|
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. |
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:
- 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 drop down list |
Generates a drop down list.
|
public static string encode(string $text)
|
| $text |
string |
data to be encoded |
| {return} |
string |
the encoded data |
Encodes special characters into HTML entities.
The application charset will be used for encoding.
|
public static array encodeArray(array $data)
|
| $data |
array |
data to be encoded |
| {return} |
array |
the encoded data |
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.
|
public static string endForm()
|
| {return} |
string |
the generated tag |
Generates a closing form tag.
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. |
Displays the first validation error for a model attribute.
|
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.
A special option named 'firstError' is recognized, which when set true, will
make the error summary to show only the first error message of each attribute.
If this is not set or is false, all error messages will be displayed.
This option has been available since version 1.1.3. |
| {return} |
string |
the error summary. Empty if no errors are found. |
Displays a summary of validation errors for one or several models.
|
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 |
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).
|
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. |
Generates an opening form tag.
This is a shortcut to beginForm.
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. |
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.
|
public static string getIdByName($name)
|
| $name |
|
|
| {return} |
string |
the ID generated based on name. |
Generates a valid HTML ID based the name.
|
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 |
Generates a hidden input.
|
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 |
Generates a button using HTML button tag.
This method is similar to button except that it generates a 'button'
tag instead of 'input' tag.
|
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 |
Generates an image tag.
|
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 |
Generates an image submit button.
|
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 |
Generates an input HTML tag.
This method generates an input HTML tag based on the given input name and value.
|
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:
|
| {return} |
string |
the generated label tag |
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 |
Generates a hyperlink tag.
|
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 |
Generates a link submit button.
|
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 |
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.
|
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:
- 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 box |
Generates a list box.
|
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 |
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.
|
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.
- key: string, specifies the name of key attribute of the selection object(s).
This is used when the selection is represented in terms of objects. In this case,
the property named by the key option of the objects will be treated as the actual selection value.
This option defaults to 'primaryKey', meaning using the 'primaryKey' property value of the objects in the selection.
This option has been available since version 1.1.3.
|
| {return} |
string |
the generated list options |
Generates the list options.
|
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 |
Generates a mailto link.
|
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 |
Generates a meta tag that can be inserted in the head section of HTML page.
|
public static void normalizeUrl(mixed $url)
|
| $url |
mixed |
the parameter to be used to generate a valid 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.
|
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 |
Generates an open HTML element.
|
public static string pageStateField(string $value)
|
| $value |
string |
the persistent page states in serialized format |
| {return} |
string |
the generated hidden field |
Generates a hidden field for storing persistent page states.
This method is internally used by statefulForm.
|
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 |
Generates a password field input.
|
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.)
Since version 1.1.2, a special option named 'uncheckValue' is available that can be used to specify
the value returned when the radiobutton is not checked. When set, a hidden field is rendered so that
when the radiobutton is not checked, we can still obtain the posted uncheck value.
If 'uncheckValue' is not set or set to NULL, the hidden field will not be rendered. |
| {return} |
string |
the generated radio button |
Generates a radio button.
|
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 radio button input. The following special options are recognized:
- template: string, specifies how each radio button 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 |
Generates a radio button list.
A radio button list is like a check box list, except that
it only allows single selection.
|
public static void refresh(integer $seconds, string $url='')
|
| $seconds |
integer |
the number of seconds to wait before refreshing the page |
| $url |
string |
the URL to which the page should be redirected to. If empty, it means the current page. |
Registers a 'refresh' meta tag.
This method can be invoked anywhere in a view. It will register a 'refresh'
meta tag with CClientScript so that the page can be refreshed in
the specified seconds.
|
protected static string renderAttributes(array $htmlOptions)
|
| $htmlOptions |
array |
attributes to be rendered |
| {return} |
string |
the rendering result |
Renders the HTML tag attributes.
|
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 |
Generates a reset button.
public static string resolveName( CModel $model, string $attribute)
|
| $model |
CModel |
the data model |
| $attribute |
string |
the attribute |
| {return} |
string |
the input name |
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) before the real attribute name.
public static void resolveNameID( CModel $model, string $attribute, array $htmlOptions)
|
| $model |
CModel |
the data model |
| $attribute |
string |
the attribute |
| $htmlOptions |
array |
the HTML options |
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).
public static mixed resolveValue( CModel $model, string $attribute)
|
| $model |
CModel |
the data model |
| $attribute |
string |
the attribute name |
| {return} |
mixed |
the attribute value |
Evaluates the attribute value of the model.
This method can recognize the attribute name written in array format.
For example, if the attribute name is 'name[a][b]', the value "$model->name['a']['b']" will be returned.
|
public static string script(string $text)
|
| $text |
string |
the JavaScript to be enclosed |
| {return} |
string |
the enclosed JavaScript |
Encloses the given JavaScript within a script tag.
|
public static string scriptFile(string $url)
|
| $url |
string |
URL for the JavaScript file |
| {return} |
string |
the JavaScript file tag |
Includes a JavaScript file.
|
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. |
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.
|
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 |
Generates a submit button.
|
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 |
Generates an HTML element.
|
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 |
Generates a text area input.
|
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 |
Generates a text field input.
|
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 |
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']".
To use
CHtml::listData(), make sure to return the posts you want to show, not just the model class itself.CHtml::listData(MyModel::model()->findAll(), 'MyModelID', 'Value');