1 位追蹤者

類別 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 元素指定 classstyleid。請參閱 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

公共方法

隱藏繼承的方法

方法 描述 定義於
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 令牌資訊的 meta 標籤。 yii\helpers\BaseHtml
cssFile() 生成指向外部 CSS 檔案的 link 標籤。 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() 產生 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() 使用的 option 標籤。 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

Protected Methods

隱藏繼承的方法

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

Method Details

隱藏繼承的方法

a() public static method

Defined in: yii\helpers\BaseHtml::a()

生成超連結標籤。

另請參閱 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,您可以自行呼叫 yii\helpers\Url::to(),然後再將 URL 傳遞給此方法,如下所示

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 起可用)

Defined in: yii\helpers\BaseHtml::activeBooleanInput()

產生布林輸入。此方法主要由 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

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

            
activeCheckboxList() public static method

Defined in: yii\helpers\BaseHtml::activeCheckboxList()

生成核取方塊列表。

核取方塊列表允許多重選擇,類似於 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

Defined in: yii\helpers\BaseHtml::activeDropDownList()

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

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

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

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 選項陣列 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

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

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

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

            
activeInput() public static method

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

            
activeLabel() public static method

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

            
activeListBox() public static method

Defined in: yii\helpers\BaseHtml::activeListBox()

生成列表方塊。

列表方塊的選擇取自模型屬性的值。

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: string,當未選取任何選項時將提交的值。設定此屬性後,將產生一個隱藏欄位,以便在多重模式下未選取任何選項時,我們仍然可以取得張貼的未選取值。
  • 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

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 陣列

輸入列表的選項(名稱 => 組態)。支援的特殊選項取決於 $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

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() 以了解有關屬性如何呈現的詳細資訊。以下特殊選項被識別

  • 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

Defined in: yii\helpers\BaseHtml::activeRadio()

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

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

Defined in: yii\helpers\BaseHtml::activeRadioList()

生成單選按鈕列表。

單選按鈕列表類似於核取方塊列表,不同之處在於它僅允許單選。單選按鈕的選擇取自模型屬性的值。

public static string activeRadioList ( $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,使用 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

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() 以了解有關屬性如何呈現的詳細資訊。以下特殊選項被識別

  • 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

Defined in: yii\helpers\BaseHtml::activeTextarea()

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

模型屬性值將用作 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

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

            
addCssStyle() public static method (公有靜態方法)

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

新的樣式字串 (例如 '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 (公有靜態方法)

Defined in: yii\helpers\BaseHtml::beginForm()

生成表單開始標籤。

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

$options 陣列

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

特殊選項

  • csrf:是否產生 CSRF 隱藏 input。預設為 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 (公有靜態方法)

Defined in: yii\helpers\BaseHtml::beginTag()

生成開始標籤。

參見

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 (保護靜態方法) (自版本 2.0.9 起可用)

Defined in: yii\helpers\BaseHtml::booleanInput()

產生布林輸入。

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

輸入類型。可以是 radiocheckbox

$name 字串

name 屬性。

$checked 布林值

核取方塊是否應被選取。

$options 陣列

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

  • uncheck: string,與核取方塊未選取狀態相關聯的值。當此屬性存在時,將會產生一個隱藏的 input,以便在核取方塊未選取且提交時,此屬性的值仍然會透過隱藏的 input 提交到伺服器。
  • label: string,顯示在核取方塊旁邊的標籤。它將 *不會* 進行 HTML 編碼。因此,您可以傳入 HTML 程式碼,例如圖片標籤。如果這來自終端使用者,您應該 encode() 以防止 XSS 攻擊。當指定此選項時,核取方塊將會被標籤標籤包圍。
  • labelOptions: array,標籤標籤的 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 (公有靜態方法)

Defined in: yii\helpers\BaseHtml::button()

生成按鈕標籤。

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 (公有靜態方法)

Defined in: yii\helpers\BaseHtml::buttonInput()

生成輸入按鈕。

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 (公有靜態方法)

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

            
checkboxList() public static method (公有靜態方法)

Defined in: yii\helpers\BaseHtml::checkboxList()

生成核取方塊列表。

核取方塊列表允許多重選擇,類似於 listBox()。因此,對應的提交值是一個陣列。

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

每個核取方塊的 name 屬性。

$selection string|array|null

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

$items 陣列

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

$options 陣列

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

  • tag: string|false,容器元素的標籤名稱。False 表示呈現核取方塊而不使用容器。另請參閱 tag()
  • unselect: string,當沒有選取任何核取方塊時應提交的值。透過設定此選項,將會產生一個隱藏的 input。
  • disabled: boolean,是否應停用由 unselect 選項產生的隱藏 input。預設為 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 分別代表核取方塊 input 的名稱、值和選取狀態。

有關屬性如何呈現的詳細資訊,請參閱 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 (公有靜態方法)

Defined in: yii\helpers\BaseHtml::csrfMetaTags()

生成包含 CSRF 令牌資訊的 meta 標籤。

參見 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 (公有靜態方法)

Defined in: yii\helpers\BaseHtml::cssFile()

生成指向外部 CSS 檔案的 link 標籤。

另請參閱 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 (公有靜態方法)

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

            
cssStyleToArray() public static method (公有靜態方法)

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

            
decode() public static method (公有靜態方法)

Defined in: yii\helpers\BaseHtml::decode()

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

這與 encode() 相反。

參見

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

要解碼的內容

return 字串

已解碼的內容

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

            
encode() public static method (公有靜態方法)
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 (公有靜態方法)

Defined in: yii\helpers\BaseHtml::endForm()

生成表單結束標籤。

參見 beginForm()

public static string endForm ( )
return 字串

產生的標籤

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

            
endTag() public static method (公有靜態方法)

Defined in: yii\helpers\BaseHtml::endTag()

生成結束標籤。

參見

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 (公有靜態方法)

Defined in: yii\helpers\BaseHtml::error()

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

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

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

模型物件

$attribute 字串

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

$options 陣列

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

以下選項會被特殊處理

  • tag:這指定了標籤名稱。如果未設定,將會使用 "div"。另請參閱 tag()
  • encode:boolean,如果設定為 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 (公有靜態方法)

Defined in: yii\helpers\BaseHtml::errorSummary()

生成驗證錯誤的摘要。

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

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

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

$options 陣列

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

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

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

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 起可用)

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

            
fileInput() public static method (公有靜態方法)

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

            
getAttributeName() public static method (公有靜態方法)

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

            
getAttributeValue() public static method (公有靜態方法)

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

            
getInputId() public static method

定義於: 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);
}

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

定義於: yii\helpers\BaseHtml::getInputIdByName()

將輸入名稱轉換為 ID。

例如,如果 $namePost[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);
}

            
getInputName() public static method

定義於: 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.');
}

            
hiddenInput() public static method

定義於: yii\helpers\BaseHtml::hiddenInput()

產生隱藏的輸入欄位。

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

定義於: 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 起,可以將 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

定義於: 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);
}

            
jsFile() public static method

定義於: yii\helpers\BaseHtml::jsFile()

產生引用外部 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

定義於: 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);
}

            
listBox() public static method

定義於: 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 陣列

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

  • 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: string,當未選取任何選項時將提交的值。設定此屬性後,將產生一個隱藏欄位,以便在多重模式下未選取任何選項時,我們仍然可以取得張貼的未選取值。
  • 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

定義於: yii\helpers\BaseHtml::mailto()

產生 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

定義於: yii\helpers\BaseHtml::ol()

產生有序列表。

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

定義於: 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);
}

            
radio() public static method

定義於: 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);
}

            
radioList() public static method

定義於: yii\helpers\BaseHtml::radioList()

生成單選按鈕列表。

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

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

每個單選按鈕的名稱屬性。

$selection string|array|null

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

$items 陣列

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

$options 陣列

單選按鈕列表容器標籤的選項(名稱 => 組態)。以下選項經過特殊處理

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

定義於: yii\helpers\BaseHtml::removeCssClass()

從指定的選項中移除 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

定義於: 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);
    }
}

            
renderSelectOptions() public static method

定義於: 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(' ', '&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

定義於: yii\helpers\BaseHtml::renderTagAttributes()

呈現 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

定義於: 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);
}

            
resetInput() public static method

定義於: 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);
}

            
script() public static method

定義於: 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);
}

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

定義於: 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);
    }
}

            
style() public static method

定義於: 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);
}

            
submitButton() public static method

定義於: yii\helpers\BaseHtml::submitButton()

產生送出按鈕標籤。

命名表單元素 (例如提交按鈕) 時請小心。根據 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

定義於: yii\helpers\BaseHtml::submitInput()

產生送出輸入按鈕。

命名表單元素 (例如提交按鈕) 時請小心。根據 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

Defined in: yii\helpers\BaseHtml::tag()

產生完整的 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

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

            
textarea() public static method

Defined in: yii\helpers\BaseHtml::textarea()

產生文字區域輸入。

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

input 名稱

$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

Defined in: yii\helpers\BaseHtml::ul()

產生無序列表。

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