0 追蹤者

類別 yii\helpers\BaseHtml

繼承yii\helpers\BaseHtml
子類別yii\helpers\Html
自版本起可用2.0
原始碼 https://github.com/yiisoft/yii2/blob/master/framework/helpers/BaseHtml.php

BaseHtml 為 yii\helpers\Html 提供具體實作。

請勿使用 BaseHtml。請改用 yii\helpers\Html

公開屬性

隱藏繼承的屬性

屬性 類型 描述 定義於
$attributeOrder 陣列 標籤中屬性的偏好順序。 yii\helpers\BaseHtml
$attributeRegex 字串 用於屬性名稱驗證的正規表示式。 yii\helpers\BaseHtml
$dataAttributes 陣列 當值為陣列類型時,應特別處理的標籤屬性列表。 yii\helpers\BaseHtml
$normalizeClassAttribute 布林值 是否移除標籤屬性 class 中的重複類別名稱 yii\helpers\BaseHtml
$voidElements 陣列 空元素列表 (元素名稱 => 1) yii\helpers\BaseHtml

公開方法

隱藏繼承的方法

方法 描述 定義於
a() 產生超連結標籤。 yii\helpers\BaseHtml
activeCheckbox() 為給定的模型屬性產生核取方塊標籤以及標籤。 yii\helpers\BaseHtml
activeCheckboxList() 產生核取方塊列表。 yii\helpers\BaseHtml
activeDropDownList() 為給定的模型屬性產生下拉式列表。 yii\helpers\BaseHtml
activeFileInput() 為給定的模型屬性產生檔案輸入標籤。 yii\helpers\BaseHtml
activeHiddenInput() 為給定的模型屬性產生隱藏輸入標籤。 yii\helpers\BaseHtml
activeHint() 為給定的模型屬性產生提示標籤。 yii\helpers\BaseHtml
activeInput() 為給定的模型屬性產生輸入標籤。 yii\helpers\BaseHtml
activeLabel() 產生模型屬性的標籤。 yii\helpers\BaseHtml
activeListBox() 產生列表方塊。 yii\helpers\BaseHtml
activePasswordInput() 為給定的模型屬性產生密碼輸入標籤。 yii\helpers\BaseHtml
activeRadio() 為給定的模型屬性產生單選按鈕標籤以及標籤。 yii\helpers\BaseHtml
activeRadioList() 產生單選按鈕列表。 yii\helpers\BaseHtml
activeTextInput() 為給定的模型屬性產生文字輸入標籤。 yii\helpers\BaseHtml
activeTextarea() 為給定的模型屬性產生文字區域標籤。 yii\helpers\BaseHtml
addCssClass() 將 CSS 類別 (或多個類別) 新增至指定的選項。 yii\helpers\BaseHtml
addCssStyle() 將指定的 CSS 樣式新增至 HTML 選項。 yii\helpers\BaseHtml
beginForm() 產生表單開始標籤。 yii\helpers\BaseHtml
beginTag() 產生開始標籤。 yii\helpers\BaseHtml
button() 產生按鈕標籤。 yii\helpers\BaseHtml
buttonInput() 產生輸入按鈕。 yii\helpers\BaseHtml
checkbox() 產生核取方塊輸入。 yii\helpers\BaseHtml
checkboxList() 產生核取方塊列表。 yii\helpers\BaseHtml
csrfMetaTags() 產生包含 CSRF 權杖資訊的中繼標籤。 yii\helpers\BaseHtml
cssFile() 產生指向外部 CSS 檔案的連結標籤。 yii\helpers\BaseHtml
cssStyleFromArray() 將 CSS 樣式陣列轉換為字串表示法。 yii\helpers\BaseHtml
cssStyleToArray() 將 CSS 樣式字串轉換為陣列表示法。 yii\helpers\BaseHtml
decode() 將特殊的 HTML 實體解碼回對應的字元。 yii\helpers\BaseHtml
encode() 將特殊字元編碼為 HTML 實體。 yii\helpers\BaseHtml
endForm() 產生表單結束標籤。 yii\helpers\BaseHtml
endTag() 產生結束標籤。 yii\helpers\BaseHtml
error() 產生包含指定模型屬性的第一個驗證錯誤的標籤。 yii\helpers\BaseHtml
errorSummary() 產生驗證錯誤的摘要。 yii\helpers\BaseHtml
escapeJsRegularExpression() 跳脫正規表示式以在 JavaScript 中使用。 yii\helpers\BaseHtml
fileInput() 產生檔案輸入欄位。 yii\helpers\BaseHtml
getAttributeName() 從給定的屬性表達式傳回真實的屬性名稱。 yii\helpers\BaseHtml
getAttributeValue() 傳回指定屬性名稱或表達式的值。 yii\helpers\BaseHtml
getInputId() 為指定的屬性名稱或表達式產生適當的輸入 ID。 yii\helpers\BaseHtml
getInputIdByName() 將輸入名稱轉換為 ID。 yii\helpers\BaseHtml
getInputName() 為指定的屬性名稱或表達式產生適當的輸入名稱。 yii\helpers\BaseHtml
hiddenInput() 產生隱藏輸入欄位。 yii\helpers\BaseHtml
img() 產生圖片標籤。 yii\helpers\BaseHtml
input() 產生給定類型的輸入類型。 yii\helpers\BaseHtml
jsFile() 產生指向外部 JavaScript 檔案的 script 標籤。 yii\helpers\BaseHtml
label() 產生標籤標籤。 yii\helpers\BaseHtml
listBox() 產生列表方塊。 yii\helpers\BaseHtml
mailto() 產生 mailto 超連結。 yii\helpers\BaseHtml
ol() 產生有序列表。 yii\helpers\BaseHtml
passwordInput() 產生密碼輸入欄位。 yii\helpers\BaseHtml
radio() 產生單選按鈕輸入。 yii\helpers\BaseHtml
radioList() 產生單選按鈕列表。 yii\helpers\BaseHtml
removeCssClass() 從指定的選項中移除 CSS 類別。 yii\helpers\BaseHtml
removeCssStyle() 從 HTML 選項中移除指定的 CSS 樣式。 yii\helpers\BaseHtml
renderSelectOptions() 呈現可由 dropDownList()listBox() 使用的選項標籤。 yii\helpers\BaseHtml
renderTagAttributes() 呈現 HTML 標籤屬性。 yii\helpers\BaseHtml
resetButton() 產生重設按鈕標籤。 yii\helpers\BaseHtml
resetInput() 產生重設輸入按鈕。 yii\helpers\BaseHtml
script() 產生 script 標籤。 yii\helpers\BaseHtml
style() 產生 style 標籤。 yii\helpers\BaseHtml
submitButton() 產生送出按鈕標籤。 yii\helpers\BaseHtml
submitInput() 產生送出輸入按鈕。 yii\helpers\BaseHtml
tag() 產生完整的 HTML 標籤。 yii\helpers\BaseHtml
textInput() 產生文字輸入欄位。 yii\helpers\BaseHtml
textarea() 產生文字區域輸入。 yii\helpers\BaseHtml
ul() 產生無序列表。 yii\helpers\BaseHtml

受保護的方法

隱藏繼承的方法

方法 描述 定義於
activeBooleanInput() 產生布林值輸入。此方法主要由 activeCheckbox()activeRadio() 呼叫。 yii\helpers\BaseHtml
activeListInput() 產生輸入欄位列表。 yii\helpers\BaseHtml
booleanInput() 產生布林值輸入。 yii\helpers\BaseHtml
setActivePlaceholder() 從模型屬性標籤產生佔位符。 yii\helpers\BaseHtml

屬性詳細資訊

隱藏繼承的屬性

$attributeOrder public static property

標籤中屬性的偏好順序。這主要影響由 renderTagAttributes() 呈現的屬性順序。

public static array $attributeOrder = [
    
'type',
    
'id',
    
'class',
    
'name',
    
'value',
    
'href',
    
'src',
    
'srcset',
    
'form',
    
'action',
    
'method',
    
'selected',
    
'checked',
    
'readonly',
    
'disabled',
    
'multiple',
    
'size',
    
'maxlength',
    
'width',
    
'height',
    
'rows',
    
'cols',
    
'alt',
    
'title',
    
'rel',
    
'media',
]
$attributeRegex public static property (自版本 2.0.12 起可用)

用於屬性名稱驗證的正規表示式。

public static string $attributeRegex '/(^|.*\])([\w\.\+]+)(\[.*|$)/u'
$dataAttributes public static property (自版本 2.0.3 起可用)

當值為陣列類型時,應特別處理的標籤屬性列表。 特別是,如果 data 屬性的值為 ['name' => 'xyz', 'age' => 13],則會產生兩個屬性而不是一個:data-name="xyz" data-age="13"

public static array $dataAttributes = [
    
'aria',
    
'data',
    
'data-ng',
    
'ng',
]
$normalizeClassAttribute public static property (自版本 2.0.44 起可用)

是否移除標籤屬性 class 中的重複類別名稱

參見

public static boolean $normalizeClassAttribute false
$voidElements public static property

空元素列表 (元素名稱 => 1)

另請參閱 https://html.spec.whatwg.org/multipage/syntax.html#void-element

public static array $voidElements = [
    
'area' => 1,
    
'base' => 1,
    
'br' => 1,
    
'col' => 1,
    
'command' => 1,
    
'embed' => 1,
    
'hr' => 1,
    
'img' => 1,
    
'input' => 1,
    
'keygen' => 1,
    
'link' => 1,
    
'meta' => 1,
    
'param' => 1,
    
'source' => 1,
    
'track' => 1,
    
'wbr' => 1,
]

方法詳細資訊

隱藏繼承的方法

a() public static method

產生超連結標籤。

另請參閱 yii\helpers\Url::to()

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,您可以自己在將 URL 傳遞給此方法之前呼叫 yii\helpers\Url::to(),如下所示

Html::a('link text', Url::to($url, true))
$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);
}

            
activeBooleanInput() protected static method (自版本 2.0.9 起可用)

產生布林值輸入。此方法主要由 activeCheckbox()activeRadio() 呼叫。

protected static string activeBooleanInput ( $type, $model, $attribute, $options = [] )
$type 字串

輸入類型。 這可以是 radiocheckbox

$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);
}

            
activeCheckbox() public static method

為給定的模型屬性產生核取方塊標籤以及標籤。

此方法將根據模型屬性值產生 "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);
}

            
activeCheckboxList() public static method

產生核取方塊列表。

核取方塊列表允許多重選擇,例如 listBox()。 因此,對應的提交值是一個陣列。 核取方塊列表的選擇取自模型屬性的值。

public static string activeCheckboxList ( $model, $attribute, $items, $options = [] )
$model yii\base\Model

模型物件

$attribute 字串

屬性名稱或表達式。 有關屬性表達式的格式,請參閱 getAttributeName()

$items 陣列

用於產生核取方塊的資料項目。 陣列鍵是核取方塊值,而陣列值是相應的標籤。

$options 陣列

核取方塊列表容器標籤的選項 (名稱 => 配置)。 以下選項經過特殊處理

  • tag:string|false,容器元素的標籤名稱。 False 表示呈現不含容器的核取方塊。 另請參閱 tag()
  • unselect:string,當未選取任何核取方塊時應提交的值。 您可以將此選項設定為 null 以防止提交預設值。 如果未設定此選項,則將提交空字串。
  • encode:boolean,是否對核取方塊標籤進行 HTML 編碼。 預設為 true。 如果設定了 item 選項,則會忽略此選項。
  • separator:string,分隔項目的 HTML 程式碼。
  • itemOptions:array,使用 checkbox() 產生核取方塊標籤的選項。
  • item:callable,可用於自訂 $items 中單個項目對應的 HTML 程式碼產生的回呼。 此回呼的簽名必須為

    function ($index, $label, $name, $checked, $value)
    

    其中 $index 是整個列表中核取方塊的從零開始的索引; $label 是核取方塊的標籤; 而 $name、$value 和 $checked 代表核取方塊輸入的名稱、值和已核取狀態。

有關屬性如何呈現的詳細資訊,請參閱 renderTagAttributes()

return 字串

產生的核取方塊列表

                public static function activeCheckboxList($model, $attribute, $items, $options = [])
{
    return static::activeListInput('checkboxList', $model, $attribute, $items, $options);
}

            
activeDropDownList() public static method

為給定的模型屬性產生下拉式列表。

下拉式列表的選擇取自模型屬性的值。

public static string activeDropDownList ( $model, $attribute, $items, $options = [] )
$model yii\base\Model

模型物件

$attribute 字串

屬性名稱或表達式。 有關屬性表達式的格式,請參閱 getAttributeName()

$items 陣列

選項資料項目。 陣列鍵是選項值,而陣列值是相應的選項標籤。 陣列也可以是巢狀的 (即,某些陣列值也是陣列)。 對於每個子陣列,將產生一個選項組,其標籤是與子陣列關聯的鍵。 如果您有資料模型列表,您可以使用 yii\helpers\ArrayHelper::map() 將它們轉換為上述格式。

請注意,此方法會自動對值和標籤進行 HTML 編碼,並且標籤中的空白字元也將進行 HTML 編碼。

$options 陣列

以名稱-值組表示的標籤選項。 以下選項經過特殊處理

  • prompt:string,要顯示為第一個選項的提示文字。 自版本 2.0.11 起,您可以使用陣列來覆寫值並設定其他標籤屬性

    ['text' => 'Please select', 'options' => ['value' => 'none', 'class' => 'prompt', 'label' => 'Select']],
    
  • options:array,select 選項標籤的屬性。 陣列鍵必須是有效的選項值,而陣列值是相應選項標籤的額外屬性。 例如,

    [
        'value1' => ['disabled' => true],
        'value2' => ['label' => 'value 2'],
    ];
    
  • groups:array,optgroup 標籤的屬性。 此結構與 'options' 的結構類似,不同之處在於陣列鍵代表 $items 中指定的 optgroup 標籤。
  • encodeSpaces:bool,是否使用   字元對選項提示和選項值中的空格進行編碼。 預設為 false。
  • encode:bool,是否對選項提示和選項值字元進行編碼。 預設為 true。 此選項自 2.0.3 起可用。

其餘選項將呈現為結果標籤的屬性。 這些值將使用 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);
}

            
activeFileInput() public static method

為給定的模型屬性產生檔案輸入標籤。

除非在 $options 中明確指定,否則此方法將自動為模型屬性產生 "name" 和 "value" 標籤屬性。 此外,如果在 $options 內定義了單獨的 HTML 選項陣列,且索引鍵名為 hiddenOptions,則會將其作為自己的 $options 參數傳遞給 activeHiddenInput 欄位。

public static string activeFileInput ( $model, $attribute, $options = [] )
$model yii\base\Model

模型物件

$attribute 字串

屬性名稱或表達式。 有關屬性表達式的格式,請參閱 getAttributeName()

$options 陣列

以名稱-值組表示的標籤選項。 這些將呈現為結果標籤的屬性。 這些值將使用 encode() 進行 HTML 編碼。 有關屬性如何呈現的詳細資訊,請參閱 renderTagAttributes()。 如果定義了另一個 HTML 選項陣列的 hiddenOptions 參數,它將從 $options 中提取以用於隱藏輸入。

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);
}

            
activeHiddenInput() public static method

為給定的模型屬性產生隱藏輸入標籤。

除非在 $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);
}

            
activeHint() public static method (自版本 2.0.4 起可用)

為給定的模型屬性產生提示標籤。

提示文字是與屬性相關聯的提示,透過 yii\base\Model::getAttributeHint() 取得。如果沒有提示內容可以取得,此方法將返回空字串。

public static string activeHint ( $model, $attribute, $options = [] )
$model yii\base\Model

模型物件

$attribute 字串

屬性名稱或表達式。 有關屬性表達式的格式,請參閱 getAttributeName()

$options 陣列

以名稱-值配對表示的標籤選項。這些選項將會被渲染為結果標籤的屬性。這些值將會使用 encode() 進行 HTML 編碼。如果值為 null,則對應的屬性將不會被渲染。以下選項會被特別處理

  • hint: 這指定要顯示的提示。請注意,這將不會被 encoded 編碼。如果未設定此項,將會呼叫 yii\base\Model::getAttributeHint() 以取得要顯示的提示(不進行編碼)。

有關屬性如何呈現的詳細資訊,請參閱 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);
}

            
activeInput() public static method

為給定的模型屬性產生輸入標籤。

除非在 $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);
}

            
activeLabel() public static method

產生模型屬性的標籤。

標籤文字是與屬性相關聯的標籤,透過 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);
}

            
activeListBox() public static method

產生列表方塊。

列表框的選取是從模型屬性的值取得。

public static string activeListBox ( $model, $attribute, $items, $options = [] )
$model yii\base\Model

模型物件

$attribute 字串

屬性名稱或表達式。 有關屬性表達式的格式,請參閱 getAttributeName()

$items 陣列

選項資料項目。 陣列鍵是選項值,而陣列值是相應的選項標籤。 陣列也可以是巢狀的 (即,某些陣列值也是陣列)。 對於每個子陣列,將產生一個選項組,其標籤是與子陣列關聯的鍵。 如果您有資料模型列表,您可以使用 yii\helpers\ArrayHelper::map() 將它們轉換為上述格式。

請注意,此方法會自動對值和標籤進行 HTML 編碼,並且標籤中的空白字元也將進行 HTML 編碼。

$options 陣列

以名稱-值組表示的標籤選項。 以下選項經過特殊處理

  • prompt:string,要顯示為第一個選項的提示文字。 自版本 2.0.11 起,您可以使用陣列來覆寫值並設定其他標籤屬性

    ['text' => 'Please select', 'options' => ['value' => 'none', 'class' => 'prompt', 'label' => 'Select']],
    
  • options:array,select 選項標籤的屬性。 陣列鍵必須是有效的選項值,而陣列值是相應選項標籤的額外屬性。 例如,

    [
        'value1' => ['disabled' => true],
        'value2' => ['label' => 'value 2'],
    ];
    
  • groups:array,optgroup 標籤的屬性。 此結構與 'options' 的結構類似,不同之處在於陣列鍵代表 $items 中指定的 optgroup 標籤。
  • unselect: 字串,當未選取任何選項時將提交的值。當設定此屬性時,將會產生一個隱藏欄位,以便在多選模式下未選取任何選項時,我們仍然可以取得已提交的 unselect 值。
  • encodeSpaces:bool,是否使用   字元對選項提示和選項值中的空格進行編碼。 預設為 false。
  • encode:bool,是否對選項提示和選項值字元進行編碼。 預設為 true。 此選項自 2.0.3 起可用。

其餘選項將呈現為結果標籤的屬性。 這些值將使用 encode() 進行 HTML 編碼。 如果值為 null,則不會呈現對應的屬性。 有關屬性如何呈現的詳細資訊,請參閱 renderTagAttributes()

return 字串

產生的列表框標籤

                public static function activeListBox($model, $attribute, $items, $options = [])
{
    return static::activeListInput('listBox', $model, $attribute, $items, $options);
}

            
activeListInput() protected static method

產生輸入欄位列表。

此方法主要由 activeListBox(), activeRadioList()activeCheckboxList() 呼叫。

protected static string activeListInput ( $type, $model, $attribute, $items, $options = [] )
$type 字串

輸入類型。這可以是 'listBox'、'radioList' 或 'checkBoxList'。

$model yii\base\Model

模型物件

$attribute 字串

屬性名稱或表達式。 有關屬性表達式的格式,請參閱 getAttributeName()

$items 陣列

用於產生輸入欄位的資料項目。陣列鍵是輸入值,而陣列值是相應的標籤。

$options 陣列

輸入列表的選項(name => config)。支援的特殊選項取決於 $type 指定的輸入類型。

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);
}

            
activePasswordInput() public static method

為給定的模型屬性產生密碼輸入標籤。

除非在 $options 中明確指定,否則此方法將自動為模型屬性產生 "name" 和 "value" 標籤屬性。

public static string activePasswordInput ( $model, $attribute, $options = [] )
$model yii\base\Model

模型物件

$attribute 字串

屬性名稱或表達式。 有關屬性表達式的格式,請參閱 getAttributeName()

$options 陣列

以名稱-值配對表示的標籤選項。這些選項將會被渲染為結果標籤的屬性。這些值將會使用 encode() 進行 HTML 編碼。有關屬性如何被渲染的詳細資訊,請參閱 renderTagAttributes()。以下特殊選項被識別

  • maxlength: integer|boolean,當 maxlength 設定為 true 且模型屬性通過字串驗證器驗證時,maxlength 選項將採用 yii\validators\StringValidator::$max 和 [[\yii\validators\StringValidator::length] 的最大值。自 2.0.6 版本起可用,並自 2.0.42 版本起改進了對 length 的考量。
  • placeholder: string|boolean,當 placeholder 等於 true 時,來自 $model 的屬性標籤將被用作 placeholder(此行為自 2.0.14 版本起可用)。
return 字串

產生的輸入標籤

                public static function activePasswordInput($model, $attribute, $options = [])
{
    return static::activeInput('password', $model, $attribute, $options);
}

            
activeRadio() public static method

為給定的模型屬性產生單選按鈕標籤以及標籤。

此方法將根據模型屬性值產生 "checked" 標籤屬性。

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);
}

            
activeRadioList() public static method

產生單選按鈕列表。

單選按鈕列表類似於複選框列表,不同之處在於它只允許單選。單選按鈕的選取是從模型屬性的值取得。

public static string activeRadioList ( $model, $attribute, $items, $options = [] )
$model yii\base\Model

模型物件

$attribute 字串

屬性名稱或表達式。 有關屬性表達式的格式,請參閱 getAttributeName()

$items 陣列

用於產生單選按鈕的資料項目。陣列鍵是單選值,而陣列值是相應的標籤。

$options 陣列

單選按鈕列表容器標籤的選項(name => config)。以下選項會被特別處理

  • tag: string|false,容器元素的標籤名稱。False 表示渲染單選按鈕時不包含容器。另請參閱 tag()
  • unselect: 字串,當未選取任何單選按鈕時應提交的值。您可以將此選項設定為 null 以防止提交預設值。如果未設定此選項,則會提交空字串。
  • encode:boolean,是否對核取方塊標籤進行 HTML 編碼。 預設為 true。 如果設定了 item 選項,則會忽略此選項。
  • separator:string,分隔項目的 HTML 程式碼。
  • itemOptions: 陣列,用於使用 radio() 產生單選按鈕標籤的選項。
  • item:callable,可用於自訂 $items 中單個項目對應的 HTML 程式碼產生的回呼。 此回呼的簽名必須為

    function ($index, $label, $name, $checked, $value)
    

    其中 $index 是單選按鈕在整個列表中的從零開始的索引;$label 是單選按鈕的標籤;而 $name、$value 和 $checked 代表單選按鈕輸入的名稱、值和選取狀態。

有關屬性如何呈現的詳細資訊,請參閱 renderTagAttributes()

return 字串

產生的單選按鈕列表

                public static function activeRadioList($model, $attribute, $items, $options = [])
{
    return static::activeListInput('radioList', $model, $attribute, $items, $options);
}

            
activeTextInput() public static method

為給定的模型屬性產生文字輸入標籤。

除非在 $options 中明確指定,否則此方法將自動為模型屬性產生 "name" 和 "value" 標籤屬性。

public static string activeTextInput ( $model, $attribute, $options = [] )
$model yii\base\Model

模型物件

$attribute 字串

屬性名稱或表達式。 有關屬性表達式的格式,請參閱 getAttributeName()

$options 陣列

以名稱-值配對表示的標籤選項。這些選項將會被渲染為結果標籤的屬性。這些值將會使用 encode() 進行 HTML 編碼。有關屬性如何被渲染的詳細資訊,請參閱 renderTagAttributes()。以下特殊選項被識別

  • maxlength: integer|boolean,當 maxlength 設定為 true 且模型屬性通過字串驗證器驗證時,maxlength 選項將採用 yii\validators\StringValidator::$max 和 [[\yii\validators\StringValidator::length] 的最大值。自 2.0.3 版本起可用,並自 2.0.42 版本起改進了對 length 的考量。
  • placeholder: string|boolean,當 placeholder 等於 true 時,來自 $model 的屬性標籤將被用作 placeholder(此行為自 2.0.14 版本起可用)。
return 字串

產生的輸入標籤

                public static function activeTextInput($model, $attribute, $options = [])
{
    return static::activeInput('text', $model, $attribute, $options);
}

            
activeTextarea() public static method

為給定的模型屬性產生文字區域標籤。

模型屬性值將用作 textarea 中的內容。

public static string activeTextarea ( $model, $attribute, $options = [] )
$model yii\base\Model

模型物件

$attribute 字串

屬性名稱或表達式。 有關屬性表達式的格式,請參閱 getAttributeName()

$options 陣列

以名稱-值配對表示的標籤選項。這些選項將會被渲染為結果標籤的屬性。這些值將會使用 encode() 進行 HTML 編碼。有關屬性如何被渲染的詳細資訊,請參閱 renderTagAttributes()。以下特殊選項被識別

  • maxlength: integer|boolean,當 maxlength 設定為 true 且模型屬性通過字串驗證器驗證時,maxlength 選項將採用 yii\validators\StringValidator::$max 和 [[\yii\validators\StringValidator::length] 的最大值。自 2.0.6 版本起可用,並自 2.0.42 版本起改進了對 length 的考量。
  • placeholder: string|boolean,當 placeholder 等於 true 時,來自 $model 的屬性標籤將被用作 placeholder(此行為自 2.0.14 版本起可用)。
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);
}

            
addCssClass() public static method

將 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;
    }
}

            
addCssStyle() public static method

將指定的 CSS 樣式新增至 HTML 選項。

如果選項已包含 style 元素,則新的樣式將與現有的樣式合併。如果 CSS 屬性同時存在於新樣式和舊樣式中,則如果 $overwrite 為 true,則舊樣式可能會被覆寫。

例如,

Html::addCssStyle($options, 'width: 100px; height: 200px');

參見

public static void addCssStyle ( &$options, $style, $overwrite true )
$options 陣列

要修改的 HTML 選項。

$style string|array

新的樣式字串(例如 'width: 100px; height: 200px')或陣列(例如 ['width' => '100px', 'height' => '200px'])。

$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;
}

            
beginForm() public static method

產生表單開始標籤。

另請參閱 endForm()

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" 模擬,並將新增一個隱藏的輸入,其中包含實際的方法類型。有關更多詳細資訊,請參閱 yii\web\Request::$methodParam

$options 陣列

以名稱-值組表示的標籤選項。 這些將呈現為結果標籤的屬性。 這些值將使用 encode() 進行 HTML 編碼。 如果值為 null,則不會呈現對應的屬性。 有關屬性如何呈現的詳細資訊,請參閱 renderTagAttributes()

特殊選項

  • csrf:是否產生 CSRF 隱藏輸入。預設為 true。
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;
}

            
beginTag() public static method

產生開始標籤。

參見

public static string beginTag ( $name, $options = [] )
$name string|boolean|null

標籤名稱。如果 $name 為 nullfalse,則對應的內容將在不含任何標籤的情況下渲染。

$options 陣列

以名稱-值組表示的標籤選項。 這些將呈現為結果標籤的屬性。 這些值將使用 encode() 進行 HTML 編碼。 如果值為 null,則不會呈現對應的屬性。 有關屬性如何呈現的詳細資訊,請參閱 renderTagAttributes()

return 字串

產生的開始標籤

                public static function beginTag($name, $options = [])
{
    if ($name === null || $name === false) {
        return '';
    }
    return "<$name" . static::renderTagAttributes($options) . '>';
}

            
booleanInput() protected static method (available since version 2.0.9)

產生布林值輸入。

protected static string booleanInput ( $type, $name, $checked false, $options = [] )
$type 字串

輸入類型。 這可以是 radiocheckbox

$name 字串

name 屬性。

$checked 布林值

是否應勾選複選框。

$options 陣列

以名稱-值組表示的標籤選項。 以下選項經過特殊處理

  • uncheck: 字串,與複選框的未勾選狀態相關聯的值。當此屬性存在時,將會產生一個隱藏的輸入,以便在未勾選複選框並提交時,此屬性的值仍然會透過隱藏的輸入提交到伺服器。
  • label: 字串,顯示在複選框旁邊的標籤。它將不會進行 HTML 編碼。因此,您可以傳入 HTML 代碼,例如圖像標籤。如果這來自最終使用者,您應該使用 encode() 進行編碼,以防止 XSS 攻擊。當指定此選項時,複選框將被 label 標籤包圍。
  • labelOptions: 陣列,label 標籤的 HTML 屬性。除非您設定了 "label" 選項,否則請勿設定此選項。

其餘選項將被渲染為結果複選框標籤的屬性。這些值將會使用 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);
}

            
button() public static method

產生按鈕標籤。

public static string button ( $content 'Button', $options = [] )
$content 字串

按鈕標籤內封閉的內容。它將不會進行 HTML 編碼。因此,您可以傳入 HTML 代碼,例如圖像標籤。如果這來自最終使用者,您應該考慮使用 encode() 進行編碼,以防止 XSS 攻擊。

$options 陣列

以名稱-值組表示的標籤選項。 這些將呈現為結果標籤的屬性。 這些值將使用 encode() 進行 HTML 編碼。 如果值為 null,則不會呈現對應的屬性。 有關屬性如何呈現的詳細資訊,請參閱 renderTagAttributes()

return 字串

產生的按鈕標籤

                public static function button($content = 'Button', $options = [])
{
    if (!isset($options['type'])) {
        $options['type'] = 'button';
    }
    return static::tag('button', $content, $options);
}

            
buttonInput() public static method

產生輸入按鈕。

public static string buttonInput ( $label 'Button', $options = [] )
$label string|null

value 屬性。如果為 null,則不會產生 value 屬性。

$options 陣列

以名稱-值組表示的標籤選項。 這些將呈現為結果標籤的屬性。 這些值將使用 encode() 進行 HTML 編碼。 如果值為 null,則不會呈現對應的屬性。 有關屬性如何呈現的詳細資訊,請參閱 renderTagAttributes()

return 字串

產生的按鈕標籤

                public static function buttonInput($label = 'Button', $options = [])
{
    $options['type'] = 'button';
    $options['value'] = $label;
    return static::tag('input', '', $options);
}

            
checkbox() public static method

產生核取方塊輸入。

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);
}

            
checkboxList() public static method

產生核取方塊列表。

複選框列表允許多選,類似於 listBox()。因此,對應的提交值是一個陣列。

public static string checkboxList ( $name, $selection null, $items = [], $options = [] )
$name 字串

每個複選框的 name 屬性。

$selection string|array|null

選定的值。字串用於單選,陣列用於多選。

$items 陣列

用於產生複選框的資料項目。陣列鍵是複選框值,而陣列值是相應的標籤。

$options 陣列

核取方塊列表容器標籤的選項 (名稱 => 配置)。 以下選項經過特殊處理

  • tag:string|false,容器元素的標籤名稱。 False 表示呈現不含容器的核取方塊。 另請參閱 tag()
  • unselect: 字串,當未選取任何複選框時應提交的值。透過設定此選項,將會產生一個隱藏的輸入。
  • disabled: boolean,是否應停用由 unselect 選項產生的隱藏輸入。預設為 false。此選項自 2.0.16 版本起可用。
  • encode:boolean,是否對核取方塊標籤進行 HTML 編碼。 預設為 true。 如果設定了 item 選項,則會忽略此選項。
  • strict: boolean,如果 $selection 是一個陣列且此值為 true,則將對 $items 鍵執行嚴格比較。預設為 false。此選項自 2.0.37 版本起可用。
  • separator:string,分隔項目的 HTML 程式碼。
  • itemOptions:array,使用 checkbox() 產生核取方塊標籤的選項。
  • item:callable,可用於自訂 $items 中單個項目對應的 HTML 程式碼產生的回呼。 此回呼的簽名必須為

    function ($index, $label, $name, $checked, $value)
    

    其中 $index 是複選框在整個列表中的從零開始的索引;$label 是複選框的標籤;而 $name、$value 和 $checked 分別代表複選框輸入的名稱、值和選取狀態。

有關屬性如何呈現的詳細資訊,請參閱 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);
}

            
csrfMetaTags() public static method

產生包含 CSRF 權杖資訊的中繼標籤。

另請參閱 yii\web\Request::$enableCsrfValidation

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 '';
}

            
cssFile() public static method

產生指向外部 CSS 檔案的連結標籤。

另請參閱 yii\helpers\Url::to()

public static string cssFile ( $url, $options = [] )
$url array|string

外部 CSS 檔案的 URL。此參數將由 yii\helpers\Url::to() 處理。

$options 陣列

以名稱-值組表示的標籤選項。 以下選項經過特殊處理

  • condition:指定 IE 的條件註解,例如 lt IE 9。當指定此項時,產生的 link 標籤將被封閉在條件註解內。這主要用於支援舊版本的 IE 瀏覽器。
  • noscript:如果設定為 true,link 標籤將被包裝在 <noscript> 標籤中。

其餘選項將被渲染為結果 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);
}

            
cssStyleFromArray() public static method

將 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);
}

            
cssStyleToArray() public static method

將 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;
}

            
decode() public static method

將特殊的 HTML 實體解碼回對應的字元。

這與 encode() 的功能相反。

參見

public static string decode ( $content )
$content 字串

要解碼的內容

return 字串

已解碼的內容

                public static function decode($content)
{
    return htmlspecialchars_decode($content, ENT_QUOTES);
}

            
encode() public static method

將特殊字元編碼為 HTML 實體。

應用程式的 字元集 將用於編碼。

參見

public static string encode ( $content, $doubleEncode true )
$content 字串

要編碼的內容

$doubleEncode 布林值

是否編碼 $content 中的 HTML 實體。若為 false,則 $content 中的 HTML 實體將不會被進一步編碼。

return 字串

已編碼的內容

                public static function encode($content, $doubleEncode = true)
{
    return htmlspecialchars((string)$content, ENT_QUOTES | ENT_SUBSTITUTE, Yii::$app ? Yii::$app->charset : 'UTF-8', $doubleEncode);
}

            
endForm() public static method

產生表單結束標籤。

另請參閱 beginForm()

public static string endForm ( )
return 字串

產生的標籤

                public static function endForm()
{
    return '</form>';
}

            
endTag() public static method

產生結束標籤。

參見

public static string endTag ( $name )
$name string|boolean|null

標籤名稱。如果 $name 為 nullfalse,則對應的內容將在不含任何標籤的情況下渲染。

return 字串

產生的結束標籤

                public static function endTag($name)
{
    if ($name === null || $name === false) {
        return '';
    }
    return "</$name>";
}

            
error() public static method

產生包含指定模型屬性的第一個驗證錯誤的標籤。

請注意,即使沒有驗證錯誤,此方法仍會傳回空的錯誤標籤。

public static string error ( $model, $attribute, $options = [] )
$model yii\base\Model

模型物件

$attribute 字串

屬性名稱或表達式。 有關屬性表達式的格式,請參閱 getAttributeName()

$options 陣列

標籤選項,以名稱-值對的形式呈現。這些值將使用 encode() 進行 HTML 編碼。如果值為 null,則不會呈現對應的屬性。

以下選項會被特殊處理

  • tag:這指定了標籤名稱。如果未設定,將使用 "div"。另請參閱 tag()
  • encode:布林值,若設定為 false,則錯誤訊息將不會被編碼。
  • errorSource (自 2.0.14 版起):\Closure|callable,將被呼叫以取得錯誤訊息的回呼函式。回呼函式的簽章必須為:function ($model, $attribute) 並傳回字串。若未設定,將呼叫 $model->getFirstError() 方法。

有關屬性如何呈現的詳細資訊,請參閱 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);
}

            
errorSummary() public static method

產生驗證錯誤的摘要。

如果沒有驗證錯誤,仍會產生空的錯誤摘要標記,但它將會被隱藏。

public static string errorSummary ( $models, $options = [] )
$models yii\base\Model|yii\base\Model[]

要顯示其驗證錯誤的模型。

$options 陣列

以名稱-值組表示的標籤選項。 以下選項經過特殊處理

  • header:字串,錯誤摘要的標題 HTML。如果未設定,將使用預設提示字串。
  • footer:字串,錯誤摘要的頁尾 HTML。預設為空字串。
  • encode:布林值,若設定為 false,則錯誤訊息將不會被編碼。預設為 true
  • showAllErrors:布林值,若設定為 true,則每個屬性的所有錯誤訊息都會顯示,否則只會顯示每個屬性的第一個錯誤訊息。預設為 false。此選項自 2.0.10 版起可用。
  • emptyClass:字串,新增至空摘要的類別名稱。

其餘選項將呈現為容器標籤的屬性。

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);
}

            
escapeJsRegularExpression() public static method (自 2.0.6 版本起可用)

跳脫正規表示式以在 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;
}

            
fileInput() public static method

產生檔案輸入欄位。

若要使用檔案輸入欄位,您應該將封閉表單的 "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 字串

產生的檔案輸入標籤

                public static function fileInput($name, $value = null, $options = [])
{
    return static::input('file', $name, $value, $options);
}

            
getAttributeName() public static method

從給定的屬性表達式傳回真實的屬性名稱。

屬性運算式是屬性名稱,其前綴和/或後綴帶有陣列索引。它主要用於表格資料輸入和/或陣列類型的輸入。以下是一些範例

  • [0]content 用於表格資料輸入,表示表格輸入中第一個模型的 "content" 屬性;
  • dates[0] 表示 "dates" 屬性的第一個陣列元素;
  • [0]dates[0] 表示表格輸入中第一個模型的 "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.');
}

            
getAttributeValue() public static method

傳回指定屬性名稱或表達式的值。

對於像 [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;
}

            
getInputId() public static method

為指定的屬性名稱或表達式產生適當的輸入 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);
}

            
getInputIdByName() public static method (自 2.0.43 版本起可用)

將輸入名稱轉換為 ID。

例如,如果 $namePost[content],此方法將傳回 post-content

public static string getInputIdByName ( $name )
$name 字串

輸入名稱

return 字串

產生的輸入 ID

                public static function getInputIdByName($name)
{
    $charset = Yii::$app ? Yii::$app->charset : 'UTF-8';
    $name = mb_strtolower($name, $charset);
    return str_replace(['[]', '][', '[', ']', ' ', '.', '--'], ['', '-', '-', '', '-', '-', '-'], $name);
}

            
getInputName() public static method

為指定的屬性名稱或表達式產生適當的輸入名稱。

此方法產生一個名稱,可用作輸入名稱,以收集指定屬性的使用者輸入。名稱是根據模型的 表單名稱 和給定的屬性名稱產生的。例如,如果 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.');
}

            
hiddenInput() public static method

產生隱藏輸入欄位。

public static string hiddenInput ( $name, $value null, $options = [] )
$name 字串

name 屬性。

$value string|null

value 屬性。如果為 null,則不會產生 value 屬性。

$options 陣列

以名稱-值組表示的標籤選項。 這些將呈現為結果標籤的屬性。 這些值將使用 encode() 進行 HTML 編碼。 如果值為 null,則不會呈現對應的屬性。 有關屬性如何呈現的詳細資訊,請參閱 renderTagAttributes()

return 字串

產生的隱藏輸入標籤

                public static function hiddenInput($name, $value = null, $options = [])
{
    return static::input('hidden', $name, $value, $options);
}

            
img() public static method

產生圖片標籤。

public static string img ( $src, $options = [] )
$src array|string

圖片 URL。此參數將由 yii\helpers\Url::to() 處理。

$options 陣列

以名稱-值組表示的標籤選項。 這些將呈現為結果標籤的屬性。 這些值將使用 encode() 進行 HTML 編碼。 如果值為 null,則不會呈現對應的屬性。 有關屬性如何呈現的詳細資訊,請參閱 renderTagAttributes()

自 2.0.12 版本起,可以將 srcset 選項作為陣列傳遞,其鍵為描述符,值為 URL。所有 URL 都將由 yii\helpers\Url::to() 處理。

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);
}

            
input() public static method

產生給定類型的輸入類型。

public static string input ( $type, $name null, $value null, $options = [] )
$type 字串

type 屬性。

$name string|null

name 屬性。如果為 null,則不會產生 name 屬性。

$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);
}

            
jsFile() public static method

產生指向外部 JavaScript 檔案的 script 標籤。

另請參閱 yii\helpers\Url::to()

public static string jsFile ( $url, $options = [] )
$url 字串

外部 JavaScript 檔案的 URL。此參數將由 yii\helpers\Url::to() 處理。

$options 陣列

標籤選項,以名稱-值對的形式呈現。以下選項會被特殊處理

  • condition:指定 IE 的條件註解,例如 lt IE 9。當指定此項時,產生的 script 標籤將會被包含在條件註解中。這主要用於支援舊版本的 IE 瀏覽器。

其餘選項將呈現為結果 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);
}

            
label() public static method

產生標籤標籤。

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);
}

            
listBox() public static method

產生列表方塊。

public static string listBox ( $name, $selection null, $items = [], $options = [] )
$name 字串

輸入名稱

$selection string|boolean|array|null

選定的值。字串用於單選,陣列用於多選。

$items 陣列

選項資料項目。 陣列鍵是選項值,而陣列值是相應的選項標籤。 陣列也可以是巢狀的 (即,某些陣列值也是陣列)。 對於每個子陣列,將產生一個選項組,其標籤是與子陣列關聯的鍵。 如果您有資料模型列表,您可以使用 yii\helpers\ArrayHelper::map() 將它們轉換為上述格式。

請注意,此方法會自動對值和標籤進行 HTML 編碼,並且標籤中的空白字元也將進行 HTML 編碼。

$options 陣列

以名稱-值組表示的標籤選項。 以下選項經過特殊處理

  • prompt:string,要顯示為第一個選項的提示文字。 自版本 2.0.11 起,您可以使用陣列來覆寫值並設定其他標籤屬性

    ['text' => 'Please select', 'options' => ['value' => 'none', 'class' => 'prompt', 'label' => 'Select']],
    
  • options:array,select 選項標籤的屬性。 陣列鍵必須是有效的選項值,而陣列值是相應選項標籤的額外屬性。 例如,

    [
        'value1' => ['disabled' => true],
        'value2' => ['label' => 'value 2'],
    ];
    
  • groups:array,optgroup 標籤的屬性。 此結構與 'options' 的結構類似,不同之處在於陣列鍵代表 $items 中指定的 optgroup 標籤。
  • unselect: 字串,當未選取任何選項時將提交的值。當設定此屬性時,將會產生一個隱藏欄位,以便在多選模式下未選取任何選項時,我們仍然可以取得已提交的 unselect 值。
  • encodeSpaces:bool,是否使用 &nbsp; 字元對選項提示和選項值中的空格進行編碼。 預設為 false。
  • encode:bool,是否對選項提示和選項值字元進行編碼。 預設為 true。 此選項自 2.0.3 起可用。
  • strict: boolean,如果 $selection 是一個陣列且此值為 true,則將對 $items 鍵執行嚴格比較。預設為 false。此選項自 2.0.37 版本起可用。

其餘選項將呈現為結果標籤的屬性。 這些值將使用 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);
}

            
mailto() public static method

產生 mailto 超連結。

public static string mailto ( $text, $email null, $options = [] )
$text 字串

連結主體。它將不會進行 HTML 編碼。 因此,您可以傳入 HTML 程式碼,例如圖片標籤。 如果這來自終端使用者,您應該考慮 encode() 以防止 XSS 攻擊。

$email 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);
}

            
ol() public static method

產生有序列表。

public static string ol ( $items, $options = [] )
$items array|Traversable

用於產生清單的項目。每個項目都會產生一個清單項目。請注意,如果未設定 $options['encode'] 或為 true,則項目將會自動進行 HTML 編碼。

$options 陣列

選項 (name => config) 用於單選按鈕清單。支援以下選項

  • encode:布林值,是否對項目進行 HTML 編碼。預設為 true。如果指定了 item 選項,則會忽略此選項。
  • itemOptions:陣列,li 標籤的 HTML 屬性。如果指定了 item 選項,則會忽略此選項。
  • item:可呼叫的函式,用於產生每個個別的清單項目。此回呼函式的簽章必須為

    function ($item, $index)
    

    其中 $index 是 $items 中與 $item 對應的陣列鍵。此回呼函式應傳回整個清單項目標籤。

有關屬性如何呈現的詳細資訊,請參閱 renderTagAttributes()

return 字串

產生的有序清單。如果 $items 為空,則會傳回空字串。

                public static function ol($items, $options = [])
{
    $options['tag'] = 'ol';
    return static::ul($items, $options);
}

            
passwordInput() public static method

產生密碼輸入欄位。

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);
}

            
radio() public static method

產生單選按鈕輸入。

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);
}

            
radioList() public static method

產生單選按鈕列表。

單選按鈕清單與核取方塊清單類似,不同之處在於它僅允許單選。

public static string radioList ( $name, $selection null, $items = [], $options = [] )
$name 字串

每個單選按鈕的 name 屬性。

$selection string|array|null

選定的值。字串用於單選,陣列用於多選。

$items 陣列

用於產生單選按鈕的資料項目。陣列鍵是單選按鈕的值,而陣列值是相對應的標籤。

$options 陣列

單選按鈕列表容器標籤的選項(name => config)。以下選項會被特別處理

  • tag:string|false,容器元素的標籤名稱。False 表示在不使用容器的情況下呈現單選按鈕。另請參閱 tag()
  • unselect:字串,當未選取任何單選按鈕時應提交的值。透過設定此選項,將會產生一個隱藏的輸入。
  • disabled: boolean,是否應停用由 unselect 選項產生的隱藏輸入。預設為 false。此選項自 2.0.16 版本起可用。
  • encode:boolean,是否對核取方塊標籤進行 HTML 編碼。 預設為 true。 如果設定了 item 選項,則會忽略此選項。
  • strict: boolean,如果 $selection 是一個陣列且此值為 true,則將對 $items 鍵執行嚴格比較。預設為 false。此選項自 2.0.37 版本起可用。
  • separator:string,分隔項目的 HTML 程式碼。
  • itemOptions: 陣列,用於使用 radio() 產生單選按鈕標籤的選項。
  • item:callable,可用於自訂 $items 中單個項目對應的 HTML 程式碼產生的回呼。 此回呼的簽名必須為

    function ($index, $label, $name, $checked, $value)
    

    其中 $index 是整個清單中單選按鈕的從零開始的索引;$label 是單選按鈕的標籤;而 $name、$value 和 $checked 分別代表單選按鈕輸入的名稱、值和選取狀態。

有關屬性如何呈現的詳細資訊,請參閱 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);
}

            
removeCssClass() public static method

從指定的選項中移除 CSS 類別。

另請參閱 addCssClass()

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);
            }
        }
    }
}

            
removeCssStyle() public static method

從 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);
    }
}

            
renderSelectOptions() public static method

呈現可由 dropDownList()listBox() 使用的選項標籤。

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(' ', '&nbsp;', $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(' ', '&nbsp;', $text);
            }
            $lines[] = static::tag('option', $text, $attrs);
        }
    }
    return implode("\n", $lines);
}

            
renderTagAttributes() public static method

呈現 HTML 標籤屬性。

值為布林類型的屬性將被視為布林屬性

值為 null 的屬性將不會被渲染。

屬性的值將使用 encode() 進行 HTML 編碼。

ariadata 屬性設定為陣列值時,會進行特殊處理。 在這些情況下,陣列將被「展開」並渲染 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;
}

            
resetButton() public static method

產生重設按鈕標籤。

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);
}

            
resetInput() public static method

產生重設輸入按鈕。

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);
}

            
script() public static method

產生 script 標籤。

public static string script ( $content, $options = [] )
$content 字串

腳本內容

$options 陣列

以名稱-值組表示的標籤選項。 這些將呈現為結果標籤的屬性。 這些值將使用 encode() 進行 HTML 編碼。 如果值為 null,則不會呈現對應的屬性。 有關屬性如何呈現的詳細資訊,請參閱 renderTagAttributes()

return 字串

產生的 script 標籤

                public static function script($content, $options = [])
{
    return static::tag('script', $content, $options);
}

            
setActivePlaceholder() protected static method (自 2.0.14 版本起可用)

從模型屬性標籤產生佔位符。

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);
    }
}

            
style() public static method

產生 style 標籤。

public static string style ( $content, $options = [] )
$content 字串

樣式內容

$options 陣列

以名稱-值組表示的標籤選項。 這些將呈現為結果標籤的屬性。 這些值將使用 encode() 進行 HTML 編碼。 如果值為 null,則不會呈現對應的屬性。 有關屬性如何呈現的詳細資訊,請參閱 renderTagAttributes()

return 字串

產生的樣式標籤

                public static function style($content, $options = [])
{
    return static::tag('style', $content, $options);
}

            
submitButton() public static method

產生送出按鈕標籤。

命名表單元素(例如提交按鈕)時請小心。 根據 jQuery 文件,有些保留名稱可能會導致衝突,例如 submitlengthmethod

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);
}

            
submitInput() public static method

產生送出輸入按鈕。

命名表單元素(例如提交按鈕)時請小心。 根據 jQuery 文件,有些保留名稱可能會導致衝突,例如 submitlengthmethod

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);
}

            
tag() public static method

產生完整的 HTML 標籤。

參見

public static string tag ( $name, $content '', $options = [] )
$name string|boolean|null

標籤名稱。如果 $name 為 nullfalse,則對應的內容將在不含任何標籤的情況下渲染。

$content 字串

要包含在開始和結束標籤之間的內容。 它不會進行 HTML 編碼。 如果內容來自最終使用者,您應考慮使用 encode() 進行編碼,以防止 XSS 攻擊。

$options 陣列

HTML 標籤屬性(HTML 選項),以名稱-值對形式表示。 這些將被渲染為結果標籤的屬性。 這些值將使用 encode() 進行 HTML 編碼。 如果值為 null,則不會渲染對應的屬性。

例如,當使用 ['class' => 'my-class', 'target' => '_blank', 'value' => null] 時,它將產生如下 HTML 屬性:class="my-class" target="_blank"

有關屬性如何呈現的詳細資訊,請參閱 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>";
}

            
textInput() public static method

產生文字輸入欄位。

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);
}

            
textarea() public static method

產生文字區域輸入。

public static string textarea ( $name, $value '', $options = [] )
$name 字串

輸入名稱

$value 字串

輸入值。 請注意,它將使用 encode() 進行編碼。

$options 陣列

以名稱-值對形式表示的標籤選項。 這些將被渲染為結果標籤的屬性。 這些值將使用 encode() 進行 HTML 編碼。 如果值為 null,則不會渲染對應的屬性。 有關屬性如何渲染的詳細資訊,請參閱 renderTagAttributes()。 以下是可辨識的特殊選項

  • doubleEncode:是否對 $value 中的 HTML 實體進行雙重編碼。 如果為 false,則 $value 中的 HTML 實體將不會被進一步編碼。 此選項自 2.0.11 版本起可用。
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);
}

            
ul() public static method

產生無序列表。

public static string ul ( $items, $options = [] )
$items array|Traversable

用於產生清單的項目。每個項目都會產生一個清單項目。請注意,如果未設定 $options['encode'] 或為 true,則項目將會自動進行 HTML 編碼。

$options 陣列

選項 (name => config) 用於單選按鈕清單。支援以下選項

  • encode:布林值,是否對項目進行 HTML 編碼。預設為 true。如果指定了 item 選項,則會忽略此選項。
  • separator: string,分隔項目的 HTML 程式碼。 預設為簡單的換行符 ("\n")。 此選項自 2.0.7 版本起可用。
  • itemOptions:陣列,li 標籤的 HTML 屬性。如果指定了 item 選項,則會忽略此選項。
  • item:可呼叫的函式,用於產生每個個別的清單項目。此回呼函式的簽章必須為

    function ($item, $index)
    

    其中 $index 是 $items 中與 $item 對應的陣列鍵。此回呼函式應傳回整個清單項目標籤。

有關屬性如何呈現的詳細資訊,請參閱 renderTagAttributes()

return 字串

產生的無序列表。 如果 $items 為空,將返回空的列表標籤。

                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
    );
}