類別 yii\helpers\Html
繼承關係 | yii\helpers\Html » yii\helpers\BaseHtml |
---|---|
自版本起可用 | 2.0 |
原始碼 | https://github.com/yiisoft/yii2/blob/master/framework/helpers/Html.php |
Html 提供了一組靜態方法,用於生成常用的 HTML 標籤。
這個類別中幾乎所有的方法都允許為它們生成的 HTML 標籤設定額外的 HTML 屬性。例如,您可以使用 $options
參數為 HTML 元素指定 class
、style
或 id
。請參閱 tag() 方法的文件以了解更多詳細資訊。
有關 Html 的更多詳細資訊和使用資訊,請參閱 html 輔助方法的指南文章。
公共屬性
屬性 | 類型 | 描述 | 定義於 |
---|---|---|---|
$attributeOrder | 陣列 | 標籤中屬性的偏好順序。 | yii\helpers\BaseHtml |
$attributeRegex | 字串 | 用於屬性名稱驗證的正規表示式。 | yii\helpers\BaseHtml |
$dataAttributes | 陣列 | 當值為陣列類型時,應特殊處理的標籤屬性列表。 | yii\helpers\BaseHtml |
$normalizeClassAttribute | 布林值 | 是否移除標籤屬性 class 中的重複類別名稱 |
yii\helpers\BaseHtml |
$voidElements | 陣列 | 空元素列表 (元素名稱 => 1) | yii\helpers\BaseHtml |
公共方法
Protected Methods
方法 | 描述 | 定義於 |
---|---|---|
activeBooleanInput() | 產生布林輸入。此方法主要由 activeCheckbox() 和 activeRadio() 呼叫。 | yii\helpers\BaseHtml |
activeListInput() | 產生輸入欄位列表。 | yii\helpers\BaseHtml |
booleanInput() | 產生布林輸入。 | yii\helpers\BaseHtml |
setActivePlaceholder() | 從模型屬性標籤產生 placeholder。 | yii\helpers\BaseHtml |
Method Details
public static string a ( $text, $url = null, $options = [] ) | ||
$text | 字串 |
連結主體。它將不會進行 HTML 編碼。因此,您可以傳入 HTML 程式碼,例如圖片標籤。如果這是來自終端使用者,您應考慮使用 encode() 進行編碼,以防止 XSS 攻擊。 |
$url | array|string|null |
超連結標籤的 URL。此參數將由 yii\helpers\Url::to() 處理,並將用於標籤的 "href" 屬性。如果此參數為 null,則不會產生 "href" 屬性。 如果您想使用絕對 URL,您可以自行呼叫 yii\helpers\Url::to(),然後再將 URL 傳遞給此方法,如下所示
|
$options | 陣列 |
以名稱-值對表示的標籤選項。這些將呈現為結果標籤的屬性。這些值將使用 encode() 進行 HTML 編碼。如果值為 null,則不會呈現對應的屬性。請參閱 renderTagAttributes() 以了解有關屬性如何呈現的詳細資訊。 |
return | 字串 |
產生的超連結 |
---|
public static function a($text, $url = null, $options = [])
{
if ($url !== null) {
$options['href'] = Url::to($url);
}
return static::tag('a', $text, $options);
}
Defined in: yii\helpers\BaseHtml::activeBooleanInput()
產生布林輸入。此方法主要由 activeCheckbox() 和 activeRadio() 呼叫。
protected static string activeBooleanInput ( $type, $model, $attribute, $options = [] ) | ||
$type | 字串 |
輸入類型。可以是 |
$model | yii\base\Model |
模型物件 |
$attribute | 字串 |
屬性名稱或表達式。有關屬性表達式的格式,請參閱 getAttributeName()。 |
$options | 陣列 |
以名稱-值對表示的標籤選項。有關接受的屬性的詳細資訊,請參閱 booleanInput()。 |
return | 字串 |
產生的輸入元素 |
---|
protected static function activeBooleanInput($type, $model, $attribute, $options = [])
{
$name = isset($options['name']) ? $options['name'] : static::getInputName($model, $attribute);
$value = static::getAttributeValue($model, $attribute);
if (!array_key_exists('value', $options)) {
$options['value'] = '1';
}
if (!array_key_exists('uncheck', $options)) {
$options['uncheck'] = '0';
} elseif ($options['uncheck'] === false) {
unset($options['uncheck']);
}
if (!array_key_exists('label', $options)) {
$options['label'] = static::encode($model->getAttributeLabel(static::getAttributeName($attribute)));
} elseif ($options['label'] === false) {
unset($options['label']);
}
$checked = "$value" === "{$options['value']}";
if (!array_key_exists('id', $options)) {
$options['id'] = static::getInputId($model, $attribute);
}
return static::$type($name, $checked, $options);
}
Defined in: yii\helpers\BaseHtml::activeCheckbox()
為給定的模型屬性生成核取方塊標籤以及標籤。
此方法將根據模型屬性值產生 "checked" 標籤屬性。
public static string activeCheckbox ( $model, $attribute, $options = [] ) | ||
$model | yii\base\Model |
模型物件 |
$attribute | 字串 |
屬性名稱或表達式。有關屬性表達式的格式,請參閱 getAttributeName()。 |
$options | 陣列 |
以名稱-值對表示的標籤選項。有關接受的屬性的詳細資訊,請參閱 booleanInput()。 |
return | 字串 |
產生的核取方塊標籤 |
---|
public static function activeCheckbox($model, $attribute, $options = [])
{
return static::activeBooleanInput('checkbox', $model, $attribute, $options);
}
Defined in: yii\helpers\BaseHtml::activeCheckboxList()
生成核取方塊列表。
核取方塊列表允許多重選擇,類似於 listBox()。因此,對應的提交值是一個陣列。核取方塊列表的選擇取自模型屬性的值。
public static string activeCheckboxList ( $model, $attribute, $items, $options = [] ) | ||
$model | yii\base\Model |
模型物件 |
$attribute | 字串 |
屬性名稱或表達式。有關屬性表達式的格式,請參閱 getAttributeName()。 |
$items | 陣列 |
用於產生核取方塊的資料項目。陣列鍵是核取方塊的值,陣列值是對應的標籤。 |
$options | 陣列 |
核取方塊列表容器標籤的選項(名稱 => 組態)。以下選項經過特殊處理
有關屬性如何呈現的詳細資訊,請參閱 renderTagAttributes()。 |
return | 字串 |
產生的核取方塊列表 |
---|
public static function activeCheckboxList($model, $attribute, $items, $options = [])
{
return static::activeListInput('checkboxList', $model, $attribute, $items, $options);
}
public static string activeDropDownList ( $model, $attribute, $items, $options = [] ) | ||
$model | yii\base\Model |
模型物件 |
$attribute | 字串 |
屬性名稱或表達式。有關屬性表達式的格式,請參閱 getAttributeName()。 |
$items | 陣列 |
選項資料項目。陣列鍵是選項值,陣列值是對應的選項標籤。陣列也可以是巢狀的(即,某些陣列值也是陣列)。對於每個子陣列,將產生一個選項群組,其標籤是與子陣列關聯的鍵。如果您有資料模型列表,則可以使用 yii\helpers\ArrayHelper::map() 將它們轉換為上述格式。 請注意,此方法會自動對值和標籤進行 HTML 編碼,並且標籤中的空白也會進行 HTML 編碼。 |
$options | 陣列 |
以名稱-值對表示的標籤選項。以下選項經過特殊處理
其餘選項將呈現為結果標籤的屬性。這些值將使用 encode() 進行 HTML 編碼。如果值為 null,則不會呈現對應的屬性。請參閱 renderTagAttributes() 以了解有關屬性如何呈現的詳細資訊。 |
return | 字串 |
產生的下拉式列表標籤 |
---|
public static function activeDropDownList($model, $attribute, $items, $options = [])
{
if (empty($options['multiple'])) {
return static::activeListInput('dropDownList', $model, $attribute, $items, $options);
}
return static::activeListBox($model, $attribute, $items, $options);
}
Defined in: yii\helpers\BaseHtml::activeFileInput()
為給定的模型屬性生成檔案輸入標籤。
除非在 $options
中明確指定,否則此方法將自動為模型屬性產生 "name" 和 "value" 標籤屬性。此外,如果在 $options
內定義了具有名為 hiddenOptions
的鍵的單獨 HTML 選項陣列,則會將其作為自己的 $options
參數傳遞給 activeHiddenInput
欄位。
public static string activeFileInput ( $model, $attribute, $options = [] ) | ||
$model | yii\base\Model |
模型物件 |
$attribute | 字串 |
屬性名稱或表達式。有關屬性表達式的格式,請參閱 getAttributeName()。 |
$options | 陣列 |
以名稱-值對表示的標籤選項。這些將呈現為結果標籤的屬性。這些值將使用 encode() 進行 HTML 編碼。請參閱 renderTagAttributes() 以了解有關屬性如何呈現的詳細資訊。如果定義了另一個 HTML 選項陣列 |
return | 字串 |
產生的輸入標籤 |
---|
public static function activeFileInput($model, $attribute, $options = [])
{
$hiddenOptions = ['id' => null, 'value' => ''];
if (isset($options['name'])) {
$hiddenOptions['name'] = $options['name'];
}
// make sure disabled input is not sending any value
if (!empty($options['disabled'])) {
$hiddenOptions['disabled'] = $options['disabled'];
}
$hiddenOptions = ArrayHelper::merge($hiddenOptions, ArrayHelper::remove($options, 'hiddenOptions', []));
// 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.
// The hidden input will be assigned its own set of html options via `$hiddenOptions`.
// This provides the possibility to interact with the hidden field via client script.
// Note: For file-field-only model with `disabled` option set to `true` input submitting detection won't work.
return static::activeHiddenInput($model, $attribute, $hiddenOptions)
. static::activeInput('file', $model, $attribute, $options);
}
Defined in: yii\helpers\BaseHtml::activeHiddenInput()
為給定的模型屬性生成隱藏輸入標籤。
除非在 $options
中明確指定,否則此方法將自動為模型屬性產生 "name" 和 "value" 標籤屬性。
public static string activeHiddenInput ( $model, $attribute, $options = [] ) | ||
$model | yii\base\Model |
模型物件 |
$attribute | 字串 |
屬性名稱或表達式。有關屬性表達式的格式,請參閱 getAttributeName()。 |
$options | 陣列 |
以名稱-值對表示的標籤選項。這些將呈現為結果標籤的屬性。這些值將使用 encode() 進行 HTML 編碼。請參閱 renderTagAttributes() 以了解有關屬性如何呈現的詳細資訊。 |
return | 字串 |
產生的輸入標籤 |
---|
public static function activeHiddenInput($model, $attribute, $options = [])
{
return static::activeInput('hidden', $model, $attribute, $options);
}
Defined in: yii\helpers\BaseHtml::activeHint()
為給定的模型屬性生成提示標籤。
提示文字是與屬性關聯的提示,透過 yii\base\Model::getAttributeHint() 取得。如果無法取得提示內容,方法將返回空字串。
public static string activeHint ( $model, $attribute, $options = [] ) | ||
$model | yii\base\Model |
模型物件 |
$attribute | 字串 |
屬性名稱或表達式。有關屬性表達式的格式,請參閱 getAttributeName()。 |
$options | 陣列 |
以名稱-值對表示的標籤選項。這些將呈現為結果標籤的屬性。這些值將使用 encode() 進行 HTML 編碼。如果值為 null,則不會呈現對應的屬性。以下選項經過特殊處理
有關屬性如何呈現的詳細資訊,請參閱 renderTagAttributes()。 |
return | 字串 |
產生的提示標籤 |
---|
public static function activeHint($model, $attribute, $options = [])
{
$attribute = static::getAttributeName($attribute);
$hint = isset($options['hint']) ? $options['hint'] : $model->getAttributeHint($attribute);
if (empty($hint)) {
return '';
}
$tag = ArrayHelper::remove($options, 'tag', 'div');
unset($options['hint']);
return static::tag($tag, $hint, $options);
}
Defined in: yii\helpers\BaseHtml::activeInput()
為給定的模型屬性生成輸入標籤。
除非在 $options
中明確指定,否則此方法將自動為模型屬性產生 "name" 和 "value" 標籤屬性。
public static string activeInput ( $type, $model, $attribute, $options = [] ) | ||
$type | 字串 |
輸入類型(例如 'text'、'password') |
$model | yii\base\Model |
模型物件 |
$attribute | 字串 |
屬性名稱或表達式。有關屬性表達式的格式,請參閱 getAttributeName()。 |
$options | 陣列 |
以名稱-值對表示的標籤選項。這些將呈現為結果標籤的屬性。這些值將使用 encode() 進行 HTML 編碼。請參閱 renderTagAttributes() 以了解有關屬性如何呈現的詳細資訊。 |
return | 字串 |
產生的輸入標籤 |
---|
public static function activeInput($type, $model, $attribute, $options = [])
{
$name = isset($options['name']) ? $options['name'] : static::getInputName($model, $attribute);
$value = isset($options['value']) ? $options['value'] : static::getAttributeValue($model, $attribute);
if (!array_key_exists('id', $options)) {
$options['id'] = static::getInputId($model, $attribute);
}
static::setActivePlaceholder($model, $attribute, $options);
self::normalizeMaxLength($model, $attribute, $options);
return static::input($type, $name, $value, $options);
}
Defined in: yii\helpers\BaseHtml::activeLabel()
為給定的模型屬性生成標籤。
標籤文字是與屬性關聯的標籤,透過 yii\base\Model::getAttributeLabel() 取得。
public static string activeLabel ( $model, $attribute, $options = [] ) | ||
$model | yii\base\Model |
模型物件 |
$attribute | 字串 |
屬性名稱或表達式。有關屬性表達式的格式,請參閱 getAttributeName()。 |
$options | 陣列 |
以名稱-值對表示的標籤選項。這些將呈現為結果標籤的屬性。這些值將使用 encode() 進行 HTML 編碼。如果值為 null,則不會呈現對應的屬性。以下選項經過特殊處理
有關屬性如何呈現的詳細資訊,請參閱 renderTagAttributes()。 |
return | 字串 |
產生的標籤標籤 |
---|
public static function activeLabel($model, $attribute, $options = [])
{
$for = ArrayHelper::remove($options, 'for', static::getInputId($model, $attribute));
$attribute = static::getAttributeName($attribute);
$label = ArrayHelper::remove($options, 'label', static::encode($model->getAttributeLabel($attribute)));
return static::label($label, $for, $options);
}
public static string activeListBox ( $model, $attribute, $items, $options = [] ) | ||
$model | yii\base\Model |
模型物件 |
$attribute | 字串 |
屬性名稱或表達式。有關屬性表達式的格式,請參閱 getAttributeName()。 |
$items | 陣列 |
選項資料項目。陣列鍵是選項值,陣列值是對應的選項標籤。陣列也可以是巢狀的(即,某些陣列值也是陣列)。對於每個子陣列,將產生一個選項群組,其標籤是與子陣列關聯的鍵。如果您有資料模型列表,則可以使用 yii\helpers\ArrayHelper::map() 將它們轉換為上述格式。 請注意,此方法會自動對值和標籤進行 HTML 編碼,並且標籤中的空白也會進行 HTML 編碼。 |
$options | 陣列 |
以名稱-值對表示的標籤選項。以下選項經過特殊處理
其餘選項將呈現為結果標籤的屬性。這些值將使用 encode() 進行 HTML 編碼。如果值為 null,則不會呈現對應的屬性。請參閱 renderTagAttributes() 以了解有關屬性如何呈現的詳細資訊。 |
return | 字串 |
產生的列表方塊標籤 |
---|
public static function activeListBox($model, $attribute, $items, $options = [])
{
return static::activeListInput('listBox', $model, $attribute, $items, $options);
}
Defined in: yii\helpers\BaseHtml::activeListInput()
產生輸入欄位列表。
此方法主要由 activeListBox()、activeRadioList() 和 activeCheckboxList() 呼叫。
protected static string activeListInput ( $type, $model, $attribute, $items, $options = [] ) | ||
$type | 字串 |
輸入類型。可以是 'listBox'、'radioList' 或 'checkBoxList'。 |
$model | yii\base\Model |
模型物件 |
$attribute | 字串 |
屬性名稱或表達式。有關屬性表達式的格式,請參閱 getAttributeName()。 |
$items | 陣列 |
用於產生輸入欄位的資料項目。陣列鍵是輸入值,陣列值是對應的標籤。 |
$options | 陣列 |
輸入列表的選項(名稱 => 組態)。支援的特殊選項取決於 |
return | 字串 |
產生的輸入列表 |
---|
protected static function activeListInput($type, $model, $attribute, $items, $options = [])
{
$name = ArrayHelper::remove($options, 'name', static::getInputName($model, $attribute));
$selection = ArrayHelper::remove($options, 'value', static::getAttributeValue($model, $attribute));
if (!array_key_exists('unselect', $options)) {
$options['unselect'] = '';
}
if (!array_key_exists('id', $options)) {
$options['id'] = static::getInputId($model, $attribute);
}
return static::$type($name, $selection, $items, $options);
}
Defined in: yii\helpers\BaseHtml::activePasswordInput()
為給定的模型屬性生成密碼輸入標籤。
除非在 $options
中明確指定,否則此方法將自動為模型屬性產生 "name" 和 "value" 標籤屬性。
public static string activePasswordInput ( $model, $attribute, $options = [] ) | ||
$model | yii\base\Model |
模型物件 |
$attribute | 字串 |
屬性名稱或表達式。有關屬性表達式的格式,請參閱 getAttributeName()。 |
$options | 陣列 |
以名稱-值對表示的標籤選項。這些將呈現為結果標籤的屬性。這些值將使用 encode() 進行 HTML 編碼。請參閱 renderTagAttributes() 以了解有關屬性如何呈現的詳細資訊。以下特殊選項被識別
|
return | 字串 |
產生的輸入標籤 |
---|
public static function activePasswordInput($model, $attribute, $options = [])
{
return static::activeInput('password', $model, $attribute, $options);
}
public static string activeRadio ( $model, $attribute, $options = [] ) | ||
$model | yii\base\Model |
模型物件 |
$attribute | 字串 |
屬性名稱或表達式。有關屬性表達式的格式,請參閱 getAttributeName()。 |
$options | 陣列 |
以名稱-值對表示的標籤選項。有關接受的屬性的詳細資訊,請參閱 booleanInput()。 |
return | 字串 |
產生的單選按鈕標籤 |
---|
public static function activeRadio($model, $attribute, $options = [])
{
return static::activeBooleanInput('radio', $model, $attribute, $options);
}
Defined in: yii\helpers\BaseHtml::activeRadioList()
生成單選按鈕列表。
單選按鈕列表類似於核取方塊列表,不同之處在於它僅允許單選。單選按鈕的選擇取自模型屬性的值。
public static string activeRadioList ( $model, $attribute, $items, $options = [] ) | ||
$model | yii\base\Model |
模型物件 |
$attribute | 字串 |
屬性名稱或表達式。有關屬性表達式的格式,請參閱 getAttributeName()。 |
$items | 陣列 |
用於產生單選按鈕的資料項目。陣列鍵是單選按鈕的值,陣列值是對應的標籤。 |
$options | 陣列 |
單選按鈕列表容器標籤的選項(名稱 => 組態)。以下選項經過特殊處理
有關屬性如何呈現的詳細資訊,請參閱 renderTagAttributes()。 |
return | 字串 |
產生的單選按鈕列表 |
---|
public static function activeRadioList($model, $attribute, $items, $options = [])
{
return static::activeListInput('radioList', $model, $attribute, $items, $options);
}
Defined in: yii\helpers\BaseHtml::activeTextInput()
為給定的模型屬性生成文字輸入標籤。
除非在 $options
中明確指定,否則此方法將自動為模型屬性產生 "name" 和 "value" 標籤屬性。
public static string activeTextInput ( $model, $attribute, $options = [] ) | ||
$model | yii\base\Model |
模型物件 |
$attribute | 字串 |
屬性名稱或表達式。有關屬性表達式的格式,請參閱 getAttributeName()。 |
$options | 陣列 |
以名稱-值對表示的標籤選項。這些將呈現為結果標籤的屬性。這些值將使用 encode() 進行 HTML 編碼。請參閱 renderTagAttributes() 以了解有關屬性如何呈現的詳細資訊。以下特殊選項被識別
|
return | 字串 |
產生的輸入標籤 |
---|
public static function activeTextInput($model, $attribute, $options = [])
{
return static::activeInput('text', $model, $attribute, $options);
}
public static string activeTextarea ( $model, $attribute, $options = [] ) | ||
$model | yii\base\Model |
模型物件 |
$attribute | 字串 |
屬性名稱或表達式。有關屬性表達式的格式,請參閱 getAttributeName()。 |
$options | 陣列 |
以名稱-值對表示的標籤選項。這些將呈現為結果標籤的屬性。這些值將使用 encode() 進行 HTML 編碼。請參閱 renderTagAttributes() 以了解有關屬性如何呈現的詳細資訊。以下特殊選項被識別
|
return | 字串 |
產生的 textarea 標籤 |
---|
public static function activeTextarea($model, $attribute, $options = [])
{
$name = isset($options['name']) ? $options['name'] : static::getInputName($model, $attribute);
if (isset($options['value'])) {
$value = $options['value'];
unset($options['value']);
} else {
$value = static::getAttributeValue($model, $attribute);
}
if (!array_key_exists('id', $options)) {
$options['id'] = static::getInputId($model, $attribute);
}
self::normalizeMaxLength($model, $attribute, $options);
static::setActivePlaceholder($model, $attribute, $options);
return static::textarea($name, $value, $options);
}
Defined in: yii\helpers\BaseHtml::addCssClass()
將 CSS 類別 (或多個類別) 新增到指定的選項。
如果 CSS 類別已在選項中,則不會再次新增。如果給定選項的類別規格是陣列,並且在該處使用命名(字串)鍵放置了一些類別,則覆寫此類鍵將不起作用。例如
$options = ['class' => ['persistent' => 'initial']];
Html::addCssClass($options, ['persistent' => 'override']);
var_dump($options['class']); // outputs: array('persistent' => 'initial');
另請參閱 removeCssClass()。
public static void addCssClass ( &$options, $class ) | ||
$options | 陣列 |
要修改的選項。 |
$class | string|array |
要新增的 CSS 類別 |
public static function addCssClass(&$options, $class)
{
if (isset($options['class'])) {
if (is_array($options['class'])) {
$options['class'] = self::mergeCssClasses($options['class'], (array) $class);
} else {
$classes = preg_split('/\s+/', $options['class'], -1, PREG_SPLIT_NO_EMPTY);
$options['class'] = implode(' ', self::mergeCssClasses($classes, (array) $class));
}
} else {
$options['class'] = $class;
}
}
Defined in: yii\helpers\BaseHtml::addCssStyle()
將指定的 CSS 樣式新增到 HTML 選項。
如果 options 已經包含 style
元素,新的樣式將會與現有的樣式合併。如果新的和舊的樣式中都存在相同的 CSS 屬性,且 $overwrite
為 true,則舊的樣式可能會被覆寫。
例如,
Html::addCssStyle($options, 'width: 100px; height: 200px');
參見
public static void addCssStyle ( &$options, $style, $overwrite = true ) | ||
$options | 陣列 |
要修改的 HTML 選項。 |
$style | string|array |
新的樣式字串 (例如 |
$overwrite | 布林值 |
是否在新的樣式也包含現有 CSS 屬性的情況下覆寫它們。 |
public static function addCssStyle(&$options, $style, $overwrite = true)
{
if (!empty($options['style'])) {
$oldStyle = is_array($options['style']) ? $options['style'] : static::cssStyleToArray($options['style']);
$newStyle = is_array($style) ? $style : static::cssStyleToArray($style);
if (!$overwrite) {
foreach ($newStyle as $property => $value) {
if (isset($oldStyle[$property])) {
unset($newStyle[$property]);
}
}
}
$style = array_merge($oldStyle, $newStyle);
}
$options['style'] = is_array($style) ? static::cssStyleFromArray($style) : $style;
}
public static string beginForm ( $action = '', $method = 'post', $options = [] ) | ||
$action | array|string |
表單 action URL。此參數將會由 yii\helpers\Url::to() 處理。 |
$method | 字串 |
表單提交方法,例如 "post"、"get"、"put"、"delete" (不區分大小寫)。由於大多數瀏覽器僅支援 "post" 和 "get",如果給定其他方法,它們將會使用 "post" 模擬,並新增一個隱藏的 input,其中包含實際的方法類型。請參閱 yii\web\Request::$methodParam 以取得更多詳細資訊。 |
$options | 陣列 |
以名稱-值對表示的標籤選項。這些將呈現為結果標籤的屬性。這些值將使用 encode() 進行 HTML 編碼。如果值為 null,則不會呈現對應的屬性。請參閱 renderTagAttributes() 以了解有關屬性如何呈現的詳細資訊。 特殊選項
|
return | 字串 |
產生的表單開始標籤。 |
---|
public static function beginForm($action = '', $method = 'post', $options = [])
{
$action = Url::to($action);
$hiddenInputs = [];
$request = Yii::$app->getRequest();
if ($request instanceof Request) {
if (strcasecmp($method, 'get') && strcasecmp($method, 'post')) {
// simulate PUT, DELETE, etc. via POST
$hiddenInputs[] = static::hiddenInput($request->methodParam, $method);
$method = 'post';
}
$csrf = ArrayHelper::remove($options, 'csrf', true);
if ($csrf && $request->enableCsrfValidation && strcasecmp($method, 'post') === 0) {
$hiddenInputs[] = static::hiddenInput($request->csrfParam, $request->getCsrfToken());
}
}
if (!strcasecmp($method, 'get') && ($pos = strpos($action, '?')) !== false) {
// query parameters in the action are ignored for GET method
// we use hidden fields to add them back
foreach (explode('&', substr($action, $pos + 1)) as $pair) {
if (($pos1 = strpos($pair, '=')) !== false) {
$hiddenInputs[] = static::hiddenInput(
urldecode(substr($pair, 0, $pos1)),
urldecode(substr($pair, $pos1 + 1))
);
} else {
$hiddenInputs[] = static::hiddenInput(urldecode($pair), '');
}
}
$action = substr($action, 0, $pos);
}
$options['action'] = $action;
$options['method'] = $method;
$form = static::beginTag('form', $options);
if (!empty($hiddenInputs)) {
$form .= "\n" . implode("\n", $hiddenInputs);
}
return $form;
}
public static string beginTag ( $name, $options = [] ) | ||
$name | string|boolean|null |
標籤名稱。如果 $name 為 |
$options | 陣列 |
以名稱-值對表示的標籤選項。這些將呈現為結果標籤的屬性。這些值將使用 encode() 進行 HTML 編碼。如果值為 null,則不會呈現對應的屬性。請參閱 renderTagAttributes() 以了解有關屬性如何呈現的詳細資訊。 |
return | 字串 |
產生的開始標籤 |
---|
public static function beginTag($name, $options = [])
{
if ($name === null || $name === false) {
return '';
}
return "<$name" . static::renderTagAttributes($options) . '>';
}
Defined in: yii\helpers\BaseHtml::booleanInput()
產生布林輸入。
protected static string booleanInput ( $type, $name, $checked = false, $options = [] ) | ||
$type | 字串 |
輸入類型。可以是 |
$name | 字串 |
name 屬性。 |
$checked | 布林值 |
核取方塊是否應被選取。 |
$options | 陣列 |
以名稱-值對表示的標籤選項。以下選項經過特殊處理
其餘選項將會呈現為產生的核取方塊標籤的屬性。這些值將會使用 encode() 進行 HTML 編碼。如果值為 null,則不會呈現對應的屬性。請參閱 renderTagAttributes() 以取得有關如何呈現屬性的詳細資訊。 |
return | 字串 |
產生的核取方塊標籤 |
---|
protected static function booleanInput($type, $name, $checked = false, $options = [])
{
// 'checked' option has priority over $checked argument
if (!isset($options['checked'])) {
$options['checked'] = (bool) $checked;
}
$value = array_key_exists('value', $options) ? $options['value'] : '1';
if (isset($options['uncheck'])) {
// add a hidden field so that if the checkbox is not selected, it still submits a value
$hiddenOptions = [];
if (isset($options['form'])) {
$hiddenOptions['form'] = $options['form'];
}
// make sure disabled input is not sending any value
if (!empty($options['disabled'])) {
$hiddenOptions['disabled'] = $options['disabled'];
}
$hidden = static::hiddenInput($name, $options['uncheck'], $hiddenOptions);
unset($options['uncheck']);
} else {
$hidden = '';
}
if (isset($options['label'])) {
$label = $options['label'];
$labelOptions = isset($options['labelOptions']) ? $options['labelOptions'] : [];
unset($options['label'], $options['labelOptions']);
$content = static::label(static::input($type, $name, $value, $options) . ' ' . $label, null, $labelOptions);
return $hidden . $content;
}
return $hidden . static::input($type, $name, $value, $options);
}
Defined in: yii\helpers\BaseHtml::checkbox()
生成核取方塊輸入。
public static string checkbox ( $name, $checked = false, $options = [] ) | ||
$name | 字串 |
name 屬性。 |
$checked | 布林值 |
核取方塊是否應被選取。 |
$options | 陣列 |
以名稱-值對表示的標籤選項。有關接受的屬性的詳細資訊,請參閱 booleanInput()。 |
return | 字串 |
產生的核取方塊標籤 |
---|
public static function checkbox($name, $checked = false, $options = [])
{
return static::booleanInput('checkbox', $name, $checked, $options);
}
Defined in: yii\helpers\BaseHtml::checkboxList()
生成核取方塊列表。
核取方塊列表允許多重選擇,類似於 listBox()。因此,對應的提交值是一個陣列。
public static string checkboxList ( $name, $selection = null, $items = [], $options = [] ) | ||
$name | 字串 |
每個核取方塊的 name 屬性。 |
$selection | string|array|null |
選定的值。單選為字串,多選為陣列。 |
$items | 陣列 |
用於產生核取方塊的資料項目。陣列鍵是核取方塊的值,而陣列值是相應的標籤。 |
$options | 陣列 |
核取方塊列表容器標籤的選項(名稱 => 組態)。以下選項經過特殊處理
有關屬性如何呈現的詳細資訊,請參閱 renderTagAttributes()。 |
return | 字串 |
產生的核取方塊列表 |
---|
public static function checkboxList($name, $selection = null, $items = [], $options = [])
{
if (substr($name, -2) !== '[]') {
$name .= '[]';
}
if (ArrayHelper::isTraversable($selection)) {
$selection = array_map('strval', ArrayHelper::toArray($selection));
}
$formatter = ArrayHelper::remove($options, 'item');
$itemOptions = ArrayHelper::remove($options, 'itemOptions', []);
$encode = ArrayHelper::remove($options, 'encode', true);
$separator = ArrayHelper::remove($options, 'separator', "\n");
$tag = ArrayHelper::remove($options, 'tag', 'div');
$strict = ArrayHelper::remove($options, 'strict', false);
$lines = [];
$index = 0;
foreach ($items as $value => $label) {
$checked = $selection !== null &&
(!ArrayHelper::isTraversable($selection) && !strcmp($value, $selection)
|| ArrayHelper::isTraversable($selection) && ArrayHelper::isIn((string)$value, $selection, $strict));
if ($formatter !== null) {
$lines[] = call_user_func($formatter, $index, $label, $name, $checked, $value);
} else {
$lines[] = static::checkbox($name, $checked, array_merge([
'value' => $value,
'label' => $encode ? static::encode($label) : $label,
], $itemOptions));
}
$index++;
}
if (isset($options['unselect'])) {
// add a hidden field so that if the list box has no option being selected, it still submits a value
$name2 = substr($name, -2) === '[]' ? substr($name, 0, -2) : $name;
$hiddenOptions = [];
// make sure disabled input is not sending any value
if (!empty($options['disabled'])) {
$hiddenOptions['disabled'] = $options['disabled'];
}
$hidden = static::hiddenInput($name2, $options['unselect'], $hiddenOptions);
unset($options['unselect'], $options['disabled']);
} else {
$hidden = '';
}
$visibleContent = implode($separator, $lines);
if ($tag === false) {
return $hidden . $visibleContent;
}
return $hidden . static::tag($tag, $visibleContent, $options);
}
Defined in: yii\helpers\BaseHtml::csrfMetaTags()
生成包含 CSRF 令牌資訊的 meta 標籤。
public static string csrfMetaTags ( ) | ||
return | 字串 |
產生的 meta 標籤 |
---|
public static function csrfMetaTags()
{
$request = Yii::$app->getRequest();
if ($request instanceof Request && $request->enableCsrfValidation) {
return static::tag('meta', '', ['name' => 'csrf-param', 'content' => $request->csrfParam]) . "\n"
. static::tag('meta', '', ['name' => 'csrf-token', 'content' => $request->getCsrfToken()]) . "\n";
}
return '';
}
public static string cssFile ( $url, $options = [] ) | ||
$url | array|string |
外部 CSS 檔案的 URL。此參數將會由 yii\helpers\Url::to() 處理。 |
$options | 陣列 |
以名稱-值對表示的標籤選項。以下選項經過特殊處理
其餘選項將會呈現為產生的 link 標籤的屬性。這些值將會使用 encode() 進行 HTML 編碼。如果值為 null,則不會呈現對應的屬性。請參閱 renderTagAttributes() 以取得有關如何呈現屬性的詳細資訊。 |
return | 字串 |
產生的 link 標籤 |
---|
public static function cssFile($url, $options = [])
{
if (!isset($options['rel'])) {
$options['rel'] = 'stylesheet';
}
$options['href'] = Url::to($url);
if (isset($options['condition'])) {
$condition = $options['condition'];
unset($options['condition']);
return self::wrapIntoCondition(static::tag('link', '', $options), $condition);
} elseif (isset($options['noscript']) && $options['noscript'] === true) {
unset($options['noscript']);
return '<noscript>' . static::tag('link', '', $options) . '</noscript>';
}
return static::tag('link', '', $options);
}
Defined in: yii\helpers\BaseHtml::cssStyleFromArray()
將 CSS 樣式陣列轉換為字串表示形式。
例如,
print_r(Html::cssStyleFromArray(['width' => '100px', 'height' => '200px']));
// will display: 'width: 100px; height: 200px;'
public static string cssStyleFromArray ( array $style ) | ||
$style | 陣列 |
CSS 樣式陣列。陣列鍵是 CSS 屬性名稱,而陣列值是相應的 CSS 屬性值。 |
return | 字串 |
CSS 樣式字串。如果 CSS 樣式為空,將會傳回 null。 |
---|
public static function cssStyleFromArray(array $style)
{
$result = '';
foreach ($style as $name => $value) {
$result .= "$name: $value; ";
}
// return null if empty to avoid rendering the "style" attribute
return $result === '' ? null : rtrim($result);
}
Defined in: yii\helpers\BaseHtml::cssStyleToArray()
將 CSS 樣式字串轉換為陣列表示形式。
陣列鍵是 CSS 屬性名稱,而陣列值是相應的 CSS 屬性值。
例如,
print_r(Html::cssStyleToArray('width: 100px; height: 200px;'));
// will display: ['width' => '100px', 'height' => '200px']
public static array cssStyleToArray ( $style ) | ||
$style | 字串 |
CSS 樣式字串 |
return | 陣列 |
CSS 樣式的陣列表示法 |
---|
public static function cssStyleToArray($style)
{
$result = [];
foreach (explode(';', $style) as $property) {
$property = explode(':', $property);
if (count($property) > 1) {
$result[trim($property[0])] = trim($property[1]);
}
}
return $result;
}
public static string decode ( $content ) | ||
$content | 字串 |
要解碼的內容 |
return | 字串 |
已解碼的內容 |
---|
public static function decode($content)
{
return htmlspecialchars_decode($content, ENT_QUOTES);
}
Defined in: yii\helpers\BaseHtml::dropDownList()
生成下拉式列表。
public static string dropDownList ( $name, $selection = null, $items = [], $options = [] ) | ||
$name | 字串 |
input 名稱 |
$selection | string|boolean|array|null |
選定的值。單選為字串/布林值,多選為陣列。 |
$items | 陣列 |
選項資料項目。陣列鍵是選項值,陣列值是對應的選項標籤。陣列也可以是巢狀的(即,某些陣列值也是陣列)。對於每個子陣列,將產生一個選項群組,其標籤是與子陣列關聯的鍵。如果您有資料模型列表,則可以使用 yii\helpers\ArrayHelper::map() 將它們轉換為上述格式。 請注意,此方法會自動對值和標籤進行 HTML 編碼,並且標籤中的空白也會進行 HTML 編碼。 |
$options | 陣列 |
以名稱-值對表示的標籤選項。以下選項經過特殊處理
其餘選項將呈現為結果標籤的屬性。這些值將使用 encode() 進行 HTML 編碼。如果值為 null,則不會呈現對應的屬性。請參閱 renderTagAttributes() 以了解有關屬性如何呈現的詳細資訊。 |
return | 字串 |
產生的下拉式列表標籤 |
---|
public static function dropDownList($name, $selection = null, $items = [], $options = [])
{
if (!empty($options['multiple'])) {
return static::listBox($name, $selection, $items, $options);
}
$options['name'] = $name;
unset($options['unselect']);
$selectOptions = static::renderSelectOptions($selection, $items, $options);
return static::tag('select', "\n" . $selectOptions . "\n", $options);
}
Defined in: yii\helpers\BaseHtml::encode()
將特殊字元編碼為 HTML 實體。
application charset (應用程式字元集) 將會用於編碼。
參見
public static string encode ( $content, $doubleEncode = true ) | ||
$content | 字串 |
要編碼的內容 |
$doubleEncode | 布林值 |
是否編碼 |
return | 字串 |
已編碼的內容 |
---|
public static function encode($content, $doubleEncode = true)
{
return htmlspecialchars((string)$content, ENT_QUOTES | ENT_SUBSTITUTE, Yii::$app ? Yii::$app->charset : 'UTF-8', $doubleEncode);
}
public static string endForm ( ) | ||
return | 字串 |
產生的標籤 |
---|
public static function endForm()
{
return '</form>';
}
public static string endTag ( $name ) | ||
$name | string|boolean|null |
標籤名稱。如果 $name 為 |
return | 字串 |
產生的結束標籤 |
---|
public static function endTag($name)
{
if ($name === null || $name === false) {
return '';
}
return "</$name>";
}
public static string error ( $model, $attribute, $options = [] ) | ||
$model | yii\base\Model |
模型物件 |
$attribute | 字串 |
屬性名稱或表達式。有關屬性表達式的格式,請參閱 getAttributeName()。 |
$options | 陣列 |
以名稱-值對表示的標籤選項。這些值將會使用 encode() 進行 HTML 編碼。如果值為 null,則不會呈現對應的屬性。 以下選項會被特殊處理
有關屬性如何呈現的詳細資訊,請參閱 renderTagAttributes()。 |
return | 字串 |
產生的標籤標籤 |
---|
public static function error($model, $attribute, $options = [])
{
$attribute = static::getAttributeName($attribute);
$errorSource = ArrayHelper::remove($options, 'errorSource');
if ($errorSource !== null) {
$error = call_user_func($errorSource, $model, $attribute);
} else {
$error = $model->getFirstError($attribute);
}
$tag = ArrayHelper::remove($options, 'tag', 'div');
$encode = ArrayHelper::remove($options, 'encode', true);
return Html::tag($tag, $encode ? Html::encode($error) : $error, $options);
}
public static string errorSummary ( $models, $options = [] ) | ||
$models | yii\base\Model|yii\base\Model[] |
要顯示其驗證錯誤的模型。 |
$options | 陣列 |
以名稱-值對表示的標籤選項。以下選項經過特殊處理
其餘選項將會呈現為容器標籤的屬性。 |
return | 字串 |
產生的錯誤摘要 |
---|
public static function errorSummary($models, $options = [])
{
$header = isset($options['header']) ? $options['header'] : '<p>' . Yii::t('yii', 'Please fix the following errors:') . '</p>';
$footer = ArrayHelper::remove($options, 'footer', '');
$encode = ArrayHelper::remove($options, 'encode', true);
$showAllErrors = ArrayHelper::remove($options, 'showAllErrors', false);
$emptyClass = ArrayHelper::remove($options, 'emptyClass', null);
unset($options['header']);
$lines = self::collectErrors($models, $encode, $showAllErrors);
if (empty($lines)) {
// still render the placeholder for client-side validation use
$content = '<ul></ul>';
if ($emptyClass !== null) {
$options['class'] = $emptyClass;
} else {
$options['style'] = isset($options['style']) ? rtrim($options['style'], ';') . '; display:none' : 'display:none';
}
} else {
$content = '<ul><li>' . implode("</li>\n<li>", $lines) . '</li></ul>';
}
return Html::tag('div', $header . $content . $footer, $options);
}
Defined in: yii\helpers\BaseHtml::escapeJsRegularExpression()
逸出在 JavaScript 中使用的正規表示式。
public static string escapeJsRegularExpression ( $regexp ) | ||
$regexp | 字串 |
要逸出的正規表示式。 |
return | 字串 |
逸出結果。 |
---|
public static function escapeJsRegularExpression($regexp)
{
$pattern = preg_replace('/\\\\x\{?([0-9a-fA-F]+)\}?/', '\u$1', $regexp);
$deliminator = substr($pattern, 0, 1);
$pos = strrpos($pattern, $deliminator, 1);
$flag = substr($pattern, $pos + 1);
if ($deliminator !== '/') {
$pattern = '/' . str_replace('/', '\\/', substr($pattern, 1, $pos - 1)) . '/';
} else {
$pattern = substr($pattern, 0, $pos + 1);
}
if (!empty($flag)) {
$pattern .= preg_replace('/[^igmu]/', '', $flag);
}
return $pattern;
}
Defined in: yii\helpers\BaseHtml::fileInput()
產生檔案輸入欄位。
要使用檔案 input 欄位,您應該將封閉表單的 "enctype" 屬性設定為 "multipart/form-data"。表單提交後,可以透過 $_FILES[$name] 取得上傳的檔案資訊 (請參閱 PHP 文件)。
public static string fileInput ( $name, $value = null, $options = [] ) | ||
$name | 字串 |
name 屬性。 |
$value | string|null |
value 屬性。如果為 null,則不會產生 value 屬性。 |
$options | 陣列 |
以名稱-值對表示的標籤選項。這些將呈現為結果標籤的屬性。這些值將使用 encode() 進行 HTML 編碼。如果值為 null,則不會呈現對應的屬性。請參閱 renderTagAttributes() 以了解有關屬性如何呈現的詳細資訊。 |
return | 字串 |
產生的檔案 input 標籤 |
---|
public static function fileInput($name, $value = null, $options = [])
{
return static::input('file', $name, $value, $options);
}
Defined in: yii\helpers\BaseHtml::getAttributeName()
從給定的屬性表達式返回真實的屬性名稱。
屬性運算式是一個屬性名稱,其前綴和/或後綴帶有陣列索引。它主要用於表格資料 input 和/或陣列類型 input。以下是一些範例
[0]content
用於表格資料 input 中,表示表格 input 中第一個模型的 "content" 屬性;dates[0]
表示 "dates" 屬性的第一個陣列元素;[0]dates[0]
表示表格 input 中第一個模型的 "dates" 屬性的第一個陣列元素。
如果 $attribute
沒有前綴和後綴,它將會不變地傳回。
public static string getAttributeName ( $attribute ) | ||
$attribute | 字串 |
屬性名稱或運算式 |
return | 字串 |
不帶前綴和後綴的屬性名稱。 |
---|---|---|
throws | yii\base\InvalidArgumentException |
如果屬性名稱包含非單字字元。 |
public static function getAttributeName($attribute)
{
if (preg_match(static::$attributeRegex, $attribute, $matches)) {
return $matches[2];
}
throw new InvalidArgumentException('Attribute name must contain word characters only.');
}
Defined in: yii\helpers\BaseHtml::getAttributeValue()
返回指定屬性名稱或表達式的值。
對於像 [0]dates[0]
這樣的屬性運算式,此方法將傳回 $model->dates[0]
的值。請參閱 getAttributeName() 以取得有關屬性運算式的更多詳細資訊。
如果屬性值是 yii\db\ActiveRecordInterface 的實例,或是此類實例的陣列,則會改為傳回 AR 實例的主鍵值。
public static string|array getAttributeValue ( $model, $attribute ) | ||
$model | yii\base\Model |
模型物件 |
$attribute | 字串 |
屬性名稱或運算式 |
return | string|array |
對應的屬性值 |
---|---|---|
throws | yii\base\InvalidArgumentException |
如果屬性名稱包含非單字字元。 |
public static function getAttributeValue($model, $attribute)
{
if (!preg_match(static::$attributeRegex, $attribute, $matches)) {
throw new InvalidArgumentException('Attribute name must contain word characters only.');
}
$attribute = $matches[2];
$value = $model->$attribute;
if ($matches[3] !== '') {
foreach (explode('][', trim($matches[3], '[]')) as $id) {
if ((is_array($value) || $value instanceof \ArrayAccess) && isset($value[$id])) {
$value = $value[$id];
} else {
return null;
}
}
}
// https://github.com/yiisoft/yii2/issues/1457
if (is_array($value)) {
foreach ($value as $i => $v) {
if ($v instanceof ActiveRecordInterface) {
$v = $v->getPrimaryKey(false);
$value[$i] = is_array($v) ? json_encode($v) : $v;
}
}
} elseif ($value instanceof ActiveRecordInterface) {
$value = $value->getPrimaryKey(false);
return is_array($value) ? json_encode($value) : $value;
}
return $value;
}
定義於: yii\helpers\BaseHtml::getInputId()
為指定的屬性名稱或表達式產生適當的輸入 ID。
public static string getInputId ( $model, $attribute ) | ||
$model | yii\base\Model |
模型物件 |
$attribute | 字串 |
屬性名稱或表達式。關於屬性表達式的說明,請參閱 getAttributeName()。 |
return | 字串 |
產生的輸入 ID。 |
---|---|---|
throws | yii\base\InvalidArgumentException |
如果屬性名稱包含非單字字元。 |
public static function getInputId($model, $attribute)
{
$name = static::getInputName($model, $attribute);
return static::getInputIdByName($name);
}
定義於: yii\helpers\BaseHtml::getInputIdByName()
將輸入名稱轉換為 ID。
例如,如果 $name
是 Post[content]
,此方法將會傳回 post-content
。
public static string getInputIdByName ( $name ) | ||
$name | 字串 |
input 名稱 |
return | 字串 |
產生的輸入 ID |
---|
public static function getInputIdByName($name)
{
$charset = Yii::$app ? Yii::$app->charset : 'UTF-8';
$name = mb_strtolower($name, $charset);
return str_replace(['[]', '][', '[', ']', ' ', '.', '--'], ['', '-', '-', '', '-', '-', '-'], $name);
}
定義於: yii\helpers\BaseHtml::getInputName()
為指定的屬性名稱或表達式產生適當的輸入名稱。
此方法產生一個名稱,可用作輸入名稱,以收集指定屬性的使用者輸入。名稱是根據模型的 表單名稱 和給定的屬性名稱產生的。例如,如果 Post
模型的表單名稱是 Post
,則為 content
屬性產生的輸入名稱會是 Post[content]
。
關於屬性表達式的說明,請參閱 getAttributeName()。
public static string getInputName ( $model, $attribute ) | ||
$model | yii\base\Model |
模型物件 |
$attribute | 字串 |
屬性名稱或運算式 |
return | 字串 |
產生的輸入名稱 |
---|---|---|
throws | yii\base\InvalidArgumentException |
如果屬性名稱包含非單字字元。 |
public static function getInputName($model, $attribute)
{
$formName = $model->formName();
if (!preg_match(static::$attributeRegex, $attribute, $matches)) {
throw new InvalidArgumentException('Attribute name must contain word characters only.');
}
$prefix = $matches[1];
$attribute = $matches[2];
$suffix = $matches[3];
if ($formName === '' && $prefix === '') {
return $attribute . $suffix;
} elseif ($formName !== '') {
return $formName . $prefix . "[$attribute]" . $suffix;
}
throw new InvalidArgumentException(get_class($model) . '::formName() cannot be empty for tabular inputs.');
}
定義於: yii\helpers\BaseHtml::img()
產生圖片標籤。
public static string img ( $src, $options = [] ) | ||
$src | array|string |
圖片 URL。此參數將由 yii\helpers\Url::to() 處理。 |
$options | 陣列 |
以名稱-值對表示的標籤選項。這些將呈現為結果標籤的屬性。這些值將使用 encode() 進行 HTML 編碼。如果值為 null,則不會呈現對應的屬性。請參閱 renderTagAttributes() 以了解有關屬性如何呈現的詳細資訊。 自版本 2.0.12 起,可以將 |
return | 字串 |
產生的圖片標籤。 |
---|
public static function img($src, $options = [])
{
$options['src'] = Url::to($src);
if (isset($options['srcset']) && is_array($options['srcset'])) {
$srcset = [];
foreach ($options['srcset'] as $descriptor => $url) {
$srcset[] = Url::to($url) . ' ' . $descriptor;
}
$options['srcset'] = implode(',', $srcset);
}
if (!isset($options['alt'])) {
$options['alt'] = '';
}
return static::tag('img', '', $options);
}
定義於: yii\helpers\BaseHtml::input()
產生指定類型的輸入類型。
public static string input ( $type, $name = null, $value = null, $options = [] ) | ||
$type | 字串 |
類型屬性。 |
$name | string|null |
名稱屬性。如果為 null,則不會產生名稱屬性。 |
$value | string|null |
value 屬性。如果為 null,則不會產生 value 屬性。 |
$options | 陣列 |
以名稱-值對表示的標籤選項。這些將呈現為結果標籤的屬性。這些值將使用 encode() 進行 HTML 編碼。如果值為 null,則不會呈現對應的屬性。請參閱 renderTagAttributes() 以了解有關屬性如何呈現的詳細資訊。 |
return | 字串 |
產生的輸入標籤 |
---|
public static function input($type, $name = null, $value = null, $options = [])
{
if (!isset($options['type'])) {
$options['type'] = $type;
}
$options['name'] = $name;
$options['value'] = $value === null ? null : (string) $value;
return static::tag('input', '', $options);
}
public static string jsFile ( $url, $options = [] ) | ||
$url | 字串 |
外部 JavaScript 檔案的 URL。此參數將由 yii\helpers\Url::to() 處理。 |
$options | 陣列 |
以名稱-值對形式呈現的標籤選項。以下選項經過特殊處理
其餘選項將呈現為結果 script 標籤的屬性。這些值將使用 encode() 進行 HTML 編碼。如果值為 null,則不會呈現對應的屬性。關於屬性如何呈現的詳細資訊,請參閱 renderTagAttributes()。 |
return | 字串 |
產生的 script 標籤 |
---|
public static function jsFile($url, $options = [])
{
$options['src'] = Url::to($url);
if (isset($options['condition'])) {
$condition = $options['condition'];
unset($options['condition']);
return self::wrapIntoCondition(static::tag('script', '', $options), $condition);
}
return static::tag('script', '', $options);
}
定義於: yii\helpers\BaseHtml::label()
產生 label 標籤。
public static string label ( $content, $for = null, $options = [] ) | ||
$content | 字串 |
標籤文字。它「不會」進行 HTML 編碼。因此,您可以傳入 HTML 程式碼,例如圖片標籤。如果文字來自終端使用者,您應該使用 encode() 進行編碼,以防止 XSS 攻擊。 |
$for | string|null |
此標籤關聯的 HTML 元素的 ID。如果為 null,則不會產生 "for" 屬性。 |
$options | 陣列 |
以名稱-值對表示的標籤選項。這些將呈現為結果標籤的屬性。這些值將使用 encode() 進行 HTML 編碼。如果值為 null,則不會呈現對應的屬性。請參閱 renderTagAttributes() 以了解有關屬性如何呈現的詳細資訊。 |
return | 字串 |
產生的標籤標籤 |
---|
public static function label($content, $for = null, $options = [])
{
$options['for'] = $for;
return static::tag('label', $content, $options);
}
定義於: yii\helpers\BaseHtml::listBox()
生成列表方塊。
public static string listBox ( $name, $selection = null, $items = [], $options = [] ) | ||
$name | 字串 |
input 名稱 |
$selection | string|boolean|array|null |
選定的值。單選為字串,多選為陣列。 |
$items | 陣列 |
選項資料項目。陣列鍵是選項值,陣列值是對應的選項標籤。陣列也可以是巢狀的(即,某些陣列值也是陣列)。對於每個子陣列,將產生一個選項群組,其標籤是與子陣列關聯的鍵。如果您有資料模型列表,則可以使用 yii\helpers\ArrayHelper::map() 將它們轉換為上述格式。 請注意,此方法會自動對值和標籤進行 HTML 編碼,並且標籤中的空白也會進行 HTML 編碼。 |
$options | 陣列 |
以名稱-值對表示的標籤選項。以下選項經過特殊處理
其餘選項將呈現為結果標籤的屬性。這些值將使用 encode() 進行 HTML 編碼。如果值為 null,則不會呈現對應的屬性。請參閱 renderTagAttributes() 以了解有關屬性如何呈現的詳細資訊。 |
return | 字串 |
產生的列表方塊標籤 |
---|
public static function listBox($name, $selection = null, $items = [], $options = [])
{
if (!array_key_exists('size', $options)) {
$options['size'] = 4;
}
if (!empty($options['multiple']) && !empty($name) && substr_compare($name, '[]', -2, 2)) {
$name .= '[]';
}
$options['name'] = $name;
if (isset($options['unselect'])) {
// add a hidden field so that if the list box has no option being selected, it still submits a value
if (!empty($name) && substr_compare($name, '[]', -2, 2) === 0) {
$name = substr($name, 0, -2);
}
$hiddenOptions = [];
// make sure disabled input is not sending any value
if (!empty($options['disabled'])) {
$hiddenOptions['disabled'] = $options['disabled'];
}
$hidden = static::hiddenInput($name, $options['unselect'], $hiddenOptions);
unset($options['unselect']);
} else {
$hidden = '';
}
$selectOptions = static::renderSelectOptions($selection, $items, $options);
return $hidden . static::tag('select', "\n" . $selectOptions . "\n", $options);
}
定義於: yii\helpers\BaseHtml::mailto()
產生 mailto 超連結。
public static string mailto ( $text, $email = null, $options = [] ) | ||
$text | 字串 |
連結主體。它將不會進行 HTML 編碼。因此,您可以傳入 HTML 程式碼,例如圖片標籤。如果這是來自終端使用者,您應考慮使用 encode() 進行編碼,以防止 XSS 攻擊。 |
string|null |
電子郵件地址。如果為 null,則第一個參數 (連結內文) 將被視為電子郵件地址並使用。 |
|
$options | 陣列 |
以名稱-值對表示的標籤選項。這些將呈現為結果標籤的屬性。這些值將使用 encode() 進行 HTML 編碼。如果值為 null,則不會呈現對應的屬性。請參閱 renderTagAttributes() 以了解有關屬性如何呈現的詳細資訊。 |
return | 字串 |
產生的 mailto 連結 |
---|
public static function mailto($text, $email = null, $options = [])
{
$options['href'] = 'mailto:' . ($email === null ? $text : $email);
return static::tag('a', $text, $options);
}
定義於: yii\helpers\BaseHtml::ol()
產生有序列表。
public static string ol ( $items, $options = [] ) | ||
$items | array|Traversable |
用於產生列表的項目。每個項目產生一個列表項目。請注意,如果未設定 |
$options | 陣列 |
單選按鈕列表的選項 (name => config)。支援以下選項
有關屬性如何呈現的詳細資訊,請參閱 renderTagAttributes()。 |
return | 字串 |
產生的有序列表。如果 |
---|
public static function ol($items, $options = [])
{
$options['tag'] = 'ol';
return static::ul($items, $options);
}
定義於: yii\helpers\BaseHtml::passwordInput()
產生密碼輸入欄位。
public static string passwordInput ( $name, $value = null, $options = [] ) | ||
$name | 字串 |
name 屬性。 |
$value | string|null |
value 屬性。如果為 null,則不會產生 value 屬性。 |
$options | 陣列 |
以名稱-值對表示的標籤選項。這些將呈現為結果標籤的屬性。這些值將使用 encode() 進行 HTML 編碼。如果值為 null,則不會呈現對應的屬性。請參閱 renderTagAttributes() 以了解有關屬性如何呈現的詳細資訊。 |
return | 字串 |
產生的密碼輸入標籤 |
---|
public static function passwordInput($name, $value = null, $options = [])
{
return static::input('password', $name, $value, $options);
}
定義於: yii\helpers\BaseHtml::radio()
產生單選按鈕輸入。
public static string radio ( $name, $checked = false, $options = [] ) | ||
$name | 字串 |
name 屬性。 |
$checked | 布林值 |
是否應勾選單選按鈕。 |
$options | 陣列 |
以名稱-值對表示的標籤選項。有關接受的屬性的詳細資訊,請參閱 booleanInput()。 |
return | 字串 |
產生的單選按鈕標籤 |
---|
public static function radio($name, $checked = false, $options = [])
{
return static::booleanInput('radio', $name, $checked, $options);
}
public static string radioList ( $name, $selection = null, $items = [], $options = [] ) | ||
$name | 字串 |
每個單選按鈕的名稱屬性。 |
$selection | string|array|null |
選定的值。單選為字串,多選為陣列。 |
$items | 陣列 |
用於產生單選按鈕的資料項目。陣列鍵是單選按鈕值,而陣列值是對應的標籤。 |
$options | 陣列 |
單選按鈕列表容器標籤的選項(名稱 => 組態)。以下選項經過特殊處理
有關屬性如何呈現的詳細資訊,請參閱 renderTagAttributes()。 |
return | 字串 |
產生的單選按鈕列表 |
---|
public static function radioList($name, $selection = null, $items = [], $options = [])
{
if (ArrayHelper::isTraversable($selection)) {
$selection = array_map('strval', ArrayHelper::toArray($selection));
}
$formatter = ArrayHelper::remove($options, 'item');
$itemOptions = ArrayHelper::remove($options, 'itemOptions', []);
$encode = ArrayHelper::remove($options, 'encode', true);
$separator = ArrayHelper::remove($options, 'separator', "\n");
$tag = ArrayHelper::remove($options, 'tag', 'div');
$strict = ArrayHelper::remove($options, 'strict', false);
$hidden = '';
if (isset($options['unselect'])) {
// add a hidden field so that if the list box has no option being selected, it still submits a value
$hiddenOptions = [];
// make sure disabled input is not sending any value
if (!empty($options['disabled'])) {
$hiddenOptions['disabled'] = $options['disabled'];
}
$hidden = static::hiddenInput($name, $options['unselect'], $hiddenOptions);
unset($options['unselect'], $options['disabled']);
}
$lines = [];
$index = 0;
foreach ($items as $value => $label) {
$checked = $selection !== null &&
(!ArrayHelper::isTraversable($selection) && !strcmp($value, $selection)
|| ArrayHelper::isTraversable($selection) && ArrayHelper::isIn((string)$value, $selection, $strict));
if ($formatter !== null) {
$lines[] = call_user_func($formatter, $index, $label, $name, $checked, $value);
} else {
$lines[] = static::radio($name, $checked, array_merge([
'value' => $value,
'label' => $encode ? static::encode($label) : $label,
], $itemOptions));
}
$index++;
}
$visibleContent = implode($separator, $lines);
if ($tag === false) {
return $hidden . $visibleContent;
}
return $hidden . static::tag($tag, $visibleContent, $options);
}
public static void removeCssClass ( &$options, $class ) | ||
$options | 陣列 |
要修改的選項。 |
$class | string|array |
要移除的 CSS 類別。 |
public static function removeCssClass(&$options, $class)
{
if (isset($options['class'])) {
if (is_array($options['class'])) {
$classes = array_diff($options['class'], (array) $class);
if (empty($classes)) {
unset($options['class']);
} else {
$options['class'] = $classes;
}
} else {
$classes = preg_split('/\s+/', $options['class'], -1, PREG_SPLIT_NO_EMPTY);
$classes = array_diff($classes, (array) $class);
if (empty($classes)) {
unset($options['class']);
} else {
$options['class'] = implode(' ', $classes);
}
}
}
}
定義於: yii\helpers\BaseHtml::removeCssStyle()
從 HTML 選項中移除指定的 CSS 樣式。
例如,
Html::removeCssStyle($options, ['width', 'height']);
另請參閱 addCssStyle()。
public static void removeCssStyle ( &$options, $properties ) | ||
$options | 陣列 |
要修改的 HTML 選項。 |
$properties | string|array |
要移除的 CSS 屬性。如果要移除單一屬性,您可以使用字串。 |
public static function removeCssStyle(&$options, $properties)
{
if (!empty($options['style'])) {
$style = is_array($options['style']) ? $options['style'] : static::cssStyleToArray($options['style']);
foreach ((array) $properties as $property) {
unset($style[$property]);
}
$options['style'] = static::cssStyleFromArray($style);
}
}
定義於: yii\helpers\BaseHtml::renderSelectOptions()
呈現可供 dropDownList() 和 listBox() 使用的 option 標籤。
public static string renderSelectOptions ( $selection, $items, &$tagOptions = [] ) | ||
$selection | string|array|boolean|null |
選定的值。單選為字串/布林值,多選為陣列。 |
$items | 陣列 |
選項資料項目。陣列鍵是選項值,陣列值是對應的選項標籤。陣列也可以是巢狀的(即,某些陣列值也是陣列)。對於每個子陣列,將產生一個選項群組,其標籤是與子陣列關聯的鍵。如果您有資料模型列表,則可以使用 yii\helpers\ArrayHelper::map() 將它們轉換為上述格式。 請注意,此方法會自動對值和標籤進行 HTML 編碼,並且標籤中的空白也會進行 HTML 編碼。 |
$tagOptions | 陣列 |
傳遞至 dropDownList() 或 listBox() 呼叫的 $options 參數。此方法將取出以下元素 (如果有的話):「prompt」、「options」和「groups」。關於這些元素的說明,請參閱 dropDownList() 中的詳細資訊。 |
return | 字串 |
產生的列表選項 |
---|
public static function renderSelectOptions($selection, $items, &$tagOptions = [])
{
if (ArrayHelper::isTraversable($selection)) {
$normalizedSelection = [];
foreach (ArrayHelper::toArray($selection) as $selectionItem) {
if (is_bool($selectionItem)) {
$normalizedSelection[] = $selectionItem ? '1' : '0';
} else {
$normalizedSelection[] = (string)$selectionItem;
}
}
$selection = $normalizedSelection;
} elseif (is_bool($selection)) {
$selection = $selection ? '1' : '0';
}
$lines = [];
$encodeSpaces = ArrayHelper::remove($tagOptions, 'encodeSpaces', false);
$encode = ArrayHelper::remove($tagOptions, 'encode', true);
$strict = ArrayHelper::remove($tagOptions, 'strict', false);
if (isset($tagOptions['prompt'])) {
$promptOptions = ['value' => ''];
if (is_string($tagOptions['prompt'])) {
$promptText = $tagOptions['prompt'];
} else {
$promptText = $tagOptions['prompt']['text'];
$promptOptions = array_merge($promptOptions, $tagOptions['prompt']['options']);
}
$promptText = $encode ? static::encode($promptText) : $promptText;
if ($encodeSpaces) {
$promptText = str_replace(' ', ' ', $promptText);
}
$lines[] = static::tag('option', $promptText, $promptOptions);
}
$options = isset($tagOptions['options']) ? $tagOptions['options'] : [];
$groups = isset($tagOptions['groups']) ? $tagOptions['groups'] : [];
unset($tagOptions['prompt'], $tagOptions['options'], $tagOptions['groups']);
$options['encodeSpaces'] = ArrayHelper::getValue($options, 'encodeSpaces', $encodeSpaces);
$options['encode'] = ArrayHelper::getValue($options, 'encode', $encode);
foreach ($items as $key => $value) {
if (is_array($value)) {
$groupAttrs = isset($groups[$key]) ? $groups[$key] : [];
if (!isset($groupAttrs['label'])) {
$groupAttrs['label'] = $key;
}
$attrs = ['options' => $options, 'groups' => $groups, 'encodeSpaces' => $encodeSpaces, 'encode' => $encode, 'strict' => $strict];
$content = static::renderSelectOptions($selection, $value, $attrs);
$lines[] = static::tag('optgroup', "\n" . $content . "\n", $groupAttrs);
} else {
$attrs = isset($options[$key]) ? $options[$key] : [];
$attrs['value'] = (string) $key;
if (!array_key_exists('selected', $attrs)) {
$selected = false;
if ($selection !== null) {
if (ArrayHelper::isTraversable($selection)) {
$selected = ArrayHelper::isIn((string)$key, $selection, $strict);
} elseif ($key === '' || $selection === '') {
$selected = $selection === $key;
} elseif ($strict) {
$selected = !strcmp((string)$key, (string)$selection);
} else {
$selected = $selection == $key;
}
}
$attrs['selected'] = $selected;
}
$text = $encode ? static::encode($value) : $value;
if ($encodeSpaces) {
$text = str_replace(' ', ' ', $text);
}
$lines[] = static::tag('option', $text, $attrs);
}
}
return implode("\n", $lines);
}
定義於: yii\helpers\BaseHtml::renderTagAttributes()
呈現 HTML 標籤屬性。
值為布林類型的屬性將被視為 布林屬性。
值為 null 的屬性將不會呈現。
屬性的值將使用 encode() 進行 HTML 編碼。
當 aria
和 data
屬性設定為陣列值時,會進行特殊處理。在這些情況下,陣列將會被「展開」,並呈現 ARIA/data 屬性列表。例如,'aria' => ['role' => 'checkbox', 'value' => 'true']
將呈現為 aria-role="checkbox" aria-value="true"
。
如果巢狀 data
值設定為陣列,則會進行 JSON 編碼。例如,'data' => ['params' => ['id' => 1, 'name' => 'yii']]
將呈現為 data-params='{"id":1,"name":"yii"}'
。
另請參閱 addCssClass()。
public static string renderTagAttributes ( $attributes ) | ||
$attributes | 陣列 |
要呈現的屬性。屬性值將使用 encode() 進行 HTML 編碼。 |
return | 字串 |
呈現結果。如果屬性不為空,它們將被呈現為一個字串,並帶有一個前導空格 (以便可以直接附加到標籤中的標籤名稱)。如果沒有屬性,則會傳回空字串。 |
---|
public static function renderTagAttributes($attributes)
{
if (count($attributes) > 1) {
$sorted = [];
foreach (static::$attributeOrder as $name) {
if (isset($attributes[$name])) {
$sorted[$name] = $attributes[$name];
}
}
$attributes = array_merge($sorted, $attributes);
}
$html = '';
foreach ($attributes as $name => $value) {
if (is_bool($value)) {
if ($value) {
$html .= " $name";
}
} elseif (is_array($value)) {
if (in_array($name, static::$dataAttributes)) {
foreach ($value as $n => $v) {
if (is_array($v)) {
$html .= " $name-$n='" . Json::htmlEncode($v) . "'";
} elseif (is_bool($v)) {
if ($v) {
$html .= " $name-$n";
}
} elseif ($v !== null) {
$html .= " $name-$n=\"" . static::encode($v) . '"';
}
}
} elseif ($name === 'class') {
if (empty($value)) {
continue;
}
if (static::$normalizeClassAttribute === true && count($value) > 1) {
// removes duplicate classes
$value = explode(' ', implode(' ', $value));
$value = array_unique($value);
}
$html .= " $name=\"" . static::encode(implode(' ', array_filter($value))) . '"';
} elseif ($name === 'style') {
if (empty($value)) {
continue;
}
$html .= " $name=\"" . static::encode(static::cssStyleFromArray($value)) . '"';
} else {
$html .= " $name='" . Json::htmlEncode($value) . "'";
}
} elseif ($value !== null) {
$html .= " $name=\"" . static::encode($value) . '"';
}
}
return $html;
}
定義於: yii\helpers\BaseHtml::resetButton()
產生重設按鈕標籤。
public static string resetButton ( $content = 'Reset', $options = [] ) | ||
$content | 字串 |
按鈕標籤內封閉的內容。它將 *不會* 進行 HTML 編碼。因此,您可以傳入 HTML 程式碼,例如圖片標籤。如果這來自終端使用者,您應該考慮 encode() 以防止 XSS 攻擊。 |
$options | 陣列 |
以名稱-值對表示的標籤選項。這些將呈現為結果標籤的屬性。這些值將使用 encode() 進行 HTML 編碼。如果值為 null,則不會呈現對應的屬性。請參閱 renderTagAttributes() 以了解有關屬性如何呈現的詳細資訊。 |
return | 字串 |
產生的重設按鈕標籤 |
---|
public static function resetButton($content = 'Reset', $options = [])
{
$options['type'] = 'reset';
return static::button($content, $options);
}
定義於: yii\helpers\BaseHtml::resetInput()
產生重設輸入按鈕。
public static string resetInput ( $label = 'Reset', $options = [] ) | ||
$label | string|null |
value 屬性。如果為 null,則不會產生 value 屬性。 |
$options | 陣列 |
按鈕標籤的屬性。這些值將使用 encode() 進行 HTML 編碼。值為 null 的屬性將被忽略,且不會放入傳回的標籤中。關於屬性如何呈現的詳細資訊,請參閱 renderTagAttributes()。 |
return | 字串 |
產生的按鈕標籤 |
---|
public static function resetInput($label = 'Reset', $options = [])
{
$options['type'] = 'reset';
$options['value'] = $label;
return static::tag('input', '', $options);
}
定義於: yii\helpers\BaseHtml::script()
產生 script 標籤。
public static string script ( $content, $options = [] ) | ||
$content | 字串 |
script 內容 |
$options | 陣列 |
以名稱-值對表示的標籤選項。這些將呈現為結果標籤的屬性。這些值將使用 encode() 進行 HTML 編碼。如果值為 null,則不會呈現對應的屬性。請參閱 renderTagAttributes() 以了解有關屬性如何呈現的詳細資訊。 |
return | 字串 |
產生的 script 標籤 |
---|
public static function script($content, $options = [])
{
return static::tag('script', $content, $options);
}
定義於: yii\helpers\BaseHtml::setActivePlaceholder()
從模型屬性標籤產生 placeholder。
protected static void setActivePlaceholder ( $model, $attribute, &$options = [] ) | ||
$model | yii\base\Model |
模型物件 |
$attribute | 字串 |
屬性名稱或表達式。有關屬性表達式的格式,請參閱 getAttributeName()。 |
$options | 陣列 |
以名稱-值對形式呈現的標籤選項。這些選項將呈現為結果標籤的屬性。這些值將使用 encode() 進行 HTML 編碼。 |
protected static function setActivePlaceholder($model, $attribute, &$options = [])
{
if (isset($options['placeholder']) && $options['placeholder'] === true) {
$attribute = static::getAttributeName($attribute);
$options['placeholder'] = $model->getAttributeLabel($attribute);
}
}
定義於: yii\helpers\BaseHtml::style()
產生 style 標籤。
public static string style ( $content, $options = [] ) | ||
$content | 字串 |
style 內容 |
$options | 陣列 |
以名稱-值對表示的標籤選項。這些將呈現為結果標籤的屬性。這些值將使用 encode() 進行 HTML 編碼。如果值為 null,則不會呈現對應的屬性。請參閱 renderTagAttributes() 以了解有關屬性如何呈現的詳細資訊。 |
return | 字串 |
產生的 style 標籤 |
---|
public static function style($content, $options = [])
{
return static::tag('style', $content, $options);
}
定義於: yii\helpers\BaseHtml::submitButton()
產生送出按鈕標籤。
命名表單元素 (例如提交按鈕) 時請小心。根據 jQuery 文件,有些保留名稱可能會導致衝突,例如 submit
、length
或 method
。
public static string submitButton ( $content = 'Submit', $options = [] ) | ||
$content | 字串 |
按鈕標籤內封閉的內容。它將 *不會* 進行 HTML 編碼。因此,您可以傳入 HTML 程式碼,例如圖片標籤。如果這來自終端使用者,您應該考慮 encode() 以防止 XSS 攻擊。 |
$options | 陣列 |
以名稱-值對表示的標籤選項。這些將呈現為結果標籤的屬性。這些值將使用 encode() 進行 HTML 編碼。如果值為 null,則不會呈現對應的屬性。請參閱 renderTagAttributes() 以了解有關屬性如何呈現的詳細資訊。 |
return | 字串 |
產生的提交按鈕標籤 |
---|
public static function submitButton($content = 'Submit', $options = [])
{
$options['type'] = 'submit';
return static::button($content, $options);
}
定義於: yii\helpers\BaseHtml::submitInput()
產生送出輸入按鈕。
命名表單元素 (例如提交按鈕) 時請小心。根據 jQuery 文件,有些保留名稱可能會導致衝突,例如 submit
、length
或 method
。
public static string submitInput ( $label = 'Submit', $options = [] ) | ||
$label | string|null |
value 屬性。如果為 null,則不會產生 value 屬性。 |
$options | 陣列 |
以名稱-值對表示的標籤選項。這些將呈現為結果標籤的屬性。這些值將使用 encode() 進行 HTML 編碼。如果值為 null,則不會呈現對應的屬性。請參閱 renderTagAttributes() 以了解有關屬性如何呈現的詳細資訊。 |
return | 字串 |
產生的按鈕標籤 |
---|
public static function submitInput($label = 'Submit', $options = [])
{
$options['type'] = 'submit';
$options['value'] = $label;
return static::tag('input', '', $options);
}
public static string tag ( $name, $content = '', $options = [] ) | ||
$name | string|boolean|null |
標籤名稱。如果 $name 為 |
$content | 字串 |
要包含在開始和結束標籤之間的內容。它不會經過 HTML 編碼。如果內容來自終端使用者,您應該考慮使用 encode() 進行編碼,以防止 XSS 攻擊。 |
$options | 陣列 |
HTML 標籤屬性(HTML 選項),以名稱-值對的形式表示。這些將被渲染為結果標籤的屬性。這些值將使用 encode() 進行 HTML 編碼。如果值為 null,則不會渲染相應的屬性。 例如,當使用 有關屬性如何呈現的詳細資訊,請參閱 renderTagAttributes()。 |
return | 字串 |
產生的 HTML 標籤 |
---|
public static function tag($name, $content = '', $options = [])
{
if ($name === null || $name === false) {
return $content;
}
$html = "<$name" . static::renderTagAttributes($options) . '>';
return isset(static::$voidElements[strtolower($name)]) ? $html : "$html$content</$name>";
}
Defined in: yii\helpers\BaseHtml::textInput()
產生文字輸入欄位。
public static string textInput ( $name, $value = null, $options = [] ) | ||
$name | 字串 |
name 屬性。 |
$value | string|null |
value 屬性。如果為 null,則不會產生 value 屬性。 |
$options | 陣列 |
以名稱-值對表示的標籤選項。這些將呈現為結果標籤的屬性。這些值將使用 encode() 進行 HTML 編碼。如果值為 null,則不會呈現對應的屬性。請參閱 renderTagAttributes() 以了解有關屬性如何呈現的詳細資訊。 |
return | 字串 |
產生的文字輸入標籤 |
---|
public static function textInput($name, $value = null, $options = [])
{
return static::input('text', $name, $value, $options);
}
Defined in: yii\helpers\BaseHtml::textarea()
產生文字區域輸入。
public static string textarea ( $name, $value = '', $options = [] ) | ||
$name | 字串 |
input 名稱 |
$value | 字串 |
輸入值。請注意,它將使用 encode() 進行編碼。 |
$options | 陣列 |
標籤選項,以名稱-值對的形式表示。這些將被渲染為結果標籤的屬性。這些值將使用 encode() 進行 HTML 編碼。如果值為 null,則不會渲染相應的屬性。有關屬性如何渲染的詳細資訊,請參閱 renderTagAttributes()。以下是可識別的特殊選項
|
return | 字串 |
產生的文字區域標籤 |
---|
public static function textarea($name, $value = '', $options = [])
{
$options['name'] = $name;
$doubleEncode = ArrayHelper::remove($options, 'doubleEncode', true);
return static::tag('textarea', static::encode($value, $doubleEncode), $options);
}
Defined in: yii\helpers\BaseHtml::ul()
產生無序列表。
public static string ul ( $items, $options = [] ) | ||
$items | array|Traversable |
用於產生列表的項目。每個項目產生一個列表項目。請注意,如果未設定 |
$options | 陣列 |
單選按鈕列表的選項 (name => config)。支援以下選項
有關屬性如何呈現的詳細資訊,請參閱 renderTagAttributes()。 |
return | 字串 |
產生的無序列表。如果 |
---|
public static function ul($items, $options = [])
{
$tag = ArrayHelper::remove($options, 'tag', 'ul');
$encode = ArrayHelper::remove($options, 'encode', true);
$formatter = ArrayHelper::remove($options, 'item');
$separator = ArrayHelper::remove($options, 'separator', "\n");
$itemOptions = ArrayHelper::remove($options, 'itemOptions', []);
if (empty($items)) {
return static::tag($tag, '', $options);
}
$results = [];
foreach ($items as $index => $item) {
if ($formatter !== null) {
$results[] = call_user_func($formatter, $item, $index);
} else {
$results[] = static::tag('li', $encode ? static::encode($item) : $item, $itemOptions);
}
}
return static::tag(
$tag,
$separator . implode($separator, $results) . $separator,
$options
);
}
註冊 或 登入 以發表評論。