類別 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 |
公開方法
受保護的方法
方法 | 描述 | 定義於 |
---|---|---|
activeBooleanInput() | 產生布林值輸入。此方法主要由 activeCheckbox() 和 activeRadio() 呼叫。 | yii\helpers\BaseHtml |
activeListInput() | 產生輸入欄位列表。 | yii\helpers\BaseHtml |
booleanInput() | 產生布林值輸入。 | yii\helpers\BaseHtml |
setActivePlaceholder() | 從模型屬性標籤產生佔位符。 | yii\helpers\BaseHtml |
屬性詳細資訊
標籤中屬性的偏好順序。這主要影響由 renderTagAttributes() 呈現的屬性順序。
'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',
]
用於屬性名稱驗證的正規表示式。
當值為陣列類型時,應特別處理的標籤屬性列表。 特別是,如果 data
屬性的值為 ['name' => 'xyz', 'age' => 13]
,則會產生兩個屬性而不是一個:data-name="xyz" data-age="13"
。
空元素列表 (元素名稱 => 1)
另請參閱 https://html.spec.whatwg.org/multipage/syntax.html#void-element。
'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,
]
方法詳細資訊
產生超連結標籤。
另請參閱 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(),如下所示
|
$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);
}
產生布林值輸入。此方法主要由 activeCheckbox() 和 activeRadio() 呼叫。
protected static string activeBooleanInput ( $type, $model, $attribute, $options = [] ) | ||
$type | 字串 |
輸入類型。 這可以是 |
$model | yii\base\Model |
模型物件 |
$attribute | 字串 |
屬性名稱或表達式。 有關屬性表達式的格式,請參閱 getAttributeName()。 |
$options | 陣列 |
以名稱-值組表示的標籤選項。 有關接受的屬性的詳細資訊,請參閱 booleanInput()。 |
return | 字串 |
產生的輸入元素 |
---|
protected static function activeBooleanInput($type, $model, $attribute, $options = [])
{
$name = isset($options['name']) ? $options['name'] : static::getInputName($model, $attribute);
$value = static::getAttributeValue($model, $attribute);
if (!array_key_exists('value', $options)) {
$options['value'] = '1';
}
if (!array_key_exists('uncheck', $options)) {
$options['uncheck'] = '0';
} elseif ($options['uncheck'] === false) {
unset($options['uncheck']);
}
if (!array_key_exists('label', $options)) {
$options['label'] = static::encode($model->getAttributeLabel(static::getAttributeName($attribute)));
} elseif ($options['label'] === false) {
unset($options['label']);
}
$checked = "$value" === "{$options['value']}";
if (!array_key_exists('id', $options)) {
$options['id'] = static::getInputId($model, $attribute);
}
return static::$type($name, $checked, $options);
}
為給定的模型屬性產生核取方塊標籤以及標籤。
此方法將根據模型屬性值產生 "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);
}
產生核取方塊列表。
核取方塊列表允許多重選擇,例如 listBox()。 因此,對應的提交值是一個陣列。 核取方塊列表的選擇取自模型屬性的值。
public static string activeCheckboxList ( $model, $attribute, $items, $options = [] ) | ||
$model | yii\base\Model |
模型物件 |
$attribute | 字串 |
屬性名稱或表達式。 有關屬性表達式的格式,請參閱 getAttributeName()。 |
$items | 陣列 |
用於產生核取方塊的資料項目。 陣列鍵是核取方塊值,而陣列值是相應的標籤。 |
$options | 陣列 |
核取方塊列表容器標籤的選項 (名稱 => 配置)。 以下選項經過特殊處理
有關屬性如何呈現的詳細資訊,請參閱 renderTagAttributes()。 |
return | 字串 |
產生的核取方塊列表 |
---|
public static function activeCheckboxList($model, $attribute, $items, $options = [])
{
return static::activeListInput('checkboxList', $model, $attribute, $items, $options);
}
為給定的模型屬性產生下拉式列表。
下拉式列表的選擇取自模型屬性的值。
public static string activeDropDownList ( $model, $attribute, $items, $options = [] ) | ||
$model | yii\base\Model |
模型物件 |
$attribute | 字串 |
屬性名稱或表達式。 有關屬性表達式的格式,請參閱 getAttributeName()。 |
$items | 陣列 |
選項資料項目。 陣列鍵是選項值,而陣列值是相應的選項標籤。 陣列也可以是巢狀的 (即,某些陣列值也是陣列)。 對於每個子陣列,將產生一個選項組,其標籤是與子陣列關聯的鍵。 如果您有資料模型列表,您可以使用 yii\helpers\ArrayHelper::map() 將它們轉換為上述格式。 請注意,此方法會自動對值和標籤進行 HTML 編碼,並且標籤中的空白字元也將進行 HTML 編碼。 |
$options | 陣列 |
以名稱-值組表示的標籤選項。 以下選項經過特殊處理
其餘選項將呈現為結果標籤的屬性。 這些值將使用 encode() 進行 HTML 編碼。 如果值為 null,則不會呈現對應的屬性。 有關屬性如何呈現的詳細資訊,請參閱 renderTagAttributes()。 |
return | 字串 |
產生的下拉式列表標籤 |
---|
public static function activeDropDownList($model, $attribute, $items, $options = [])
{
if (empty($options['multiple'])) {
return static::activeListInput('dropDownList', $model, $attribute, $items, $options);
}
return static::activeListBox($model, $attribute, $items, $options);
}
為給定的模型屬性產生檔案輸入標籤。
除非在 $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 選項陣列的 |
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);
}
為給定的模型屬性產生隱藏輸入標籤。
除非在 $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);
}
為給定的模型屬性產生提示標籤。
提示文字是與屬性相關聯的提示,透過 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);
}
為給定的模型屬性產生輸入標籤。
除非在 $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);
}
產生模型屬性的標籤。
標籤文字是與屬性相關聯的標籤,透過 yii\base\Model::getAttributeLabel() 取得。
public static string activeLabel ( $model, $attribute, $options = [] ) | ||
$model | yii\base\Model |
模型物件 |
$attribute | 字串 |
屬性名稱或表達式。 有關屬性表達式的格式,請參閱 getAttributeName()。 |
$options | 陣列 |
以名稱-值配對表示的標籤選項。這些選項將會被渲染為結果標籤的屬性。這些值將會使用 encode() 進行 HTML 編碼。如果值為 null,則對應的屬性將不會被渲染。以下選項會被特別處理
有關屬性如何呈現的詳細資訊,請參閱 renderTagAttributes()。 |
return | 字串 |
產生的標籤標籤 |
---|
public static function activeLabel($model, $attribute, $options = [])
{
$for = ArrayHelper::remove($options, 'for', static::getInputId($model, $attribute));
$attribute = static::getAttributeName($attribute);
$label = ArrayHelper::remove($options, 'label', static::encode($model->getAttributeLabel($attribute)));
return static::label($label, $for, $options);
}
產生列表方塊。
列表框的選取是從模型屬性的值取得。
public static string activeListBox ( $model, $attribute, $items, $options = [] ) | ||
$model | yii\base\Model |
模型物件 |
$attribute | 字串 |
屬性名稱或表達式。 有關屬性表達式的格式,請參閱 getAttributeName()。 |
$items | 陣列 |
選項資料項目。 陣列鍵是選項值,而陣列值是相應的選項標籤。 陣列也可以是巢狀的 (即,某些陣列值也是陣列)。 對於每個子陣列,將產生一個選項組,其標籤是與子陣列關聯的鍵。 如果您有資料模型列表,您可以使用 yii\helpers\ArrayHelper::map() 將它們轉換為上述格式。 請注意,此方法會自動對值和標籤進行 HTML 編碼,並且標籤中的空白字元也將進行 HTML 編碼。 |
$options | 陣列 |
以名稱-值組表示的標籤選項。 以下選項經過特殊處理
其餘選項將呈現為結果標籤的屬性。 這些值將使用 encode() 進行 HTML 編碼。 如果值為 null,則不會呈現對應的屬性。 有關屬性如何呈現的詳細資訊,請參閱 renderTagAttributes()。 |
return | 字串 |
產生的列表框標籤 |
---|
public static function activeListBox($model, $attribute, $items, $options = [])
{
return static::activeListInput('listBox', $model, $attribute, $items, $options);
}
產生輸入欄位列表。
此方法主要由 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)。支援的特殊選項取決於 |
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);
}
為給定的模型屬性產生密碼輸入標籤。
除非在 $options
中明確指定,否則此方法將自動為模型屬性產生 "name" 和 "value" 標籤屬性。
public static string activePasswordInput ( $model, $attribute, $options = [] ) | ||
$model | yii\base\Model |
模型物件 |
$attribute | 字串 |
屬性名稱或表達式。 有關屬性表達式的格式,請參閱 getAttributeName()。 |
$options | 陣列 |
以名稱-值配對表示的標籤選項。這些選項將會被渲染為結果標籤的屬性。這些值將會使用 encode() 進行 HTML 編碼。有關屬性如何被渲染的詳細資訊,請參閱 renderTagAttributes()。以下特殊選項被識別
|
return | 字串 |
產生的輸入標籤 |
---|
public static function activePasswordInput($model, $attribute, $options = [])
{
return static::activeInput('password', $model, $attribute, $options);
}
為給定的模型屬性產生單選按鈕標籤以及標籤。
此方法將根據模型屬性值產生 "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);
}
產生單選按鈕列表。
單選按鈕列表類似於複選框列表,不同之處在於它只允許單選。單選按鈕的選取是從模型屬性的值取得。
public static string activeRadioList ( $model, $attribute, $items, $options = [] ) | ||
$model | yii\base\Model |
模型物件 |
$attribute | 字串 |
屬性名稱或表達式。 有關屬性表達式的格式,請參閱 getAttributeName()。 |
$items | 陣列 |
用於產生單選按鈕的資料項目。陣列鍵是單選值,而陣列值是相應的標籤。 |
$options | 陣列 |
單選按鈕列表容器標籤的選項(name => config)。以下選項會被特別處理
有關屬性如何呈現的詳細資訊,請參閱 renderTagAttributes()。 |
return | 字串 |
產生的單選按鈕列表 |
---|
public static function activeRadioList($model, $attribute, $items, $options = [])
{
return static::activeListInput('radioList', $model, $attribute, $items, $options);
}
為給定的模型屬性產生文字輸入標籤。
除非在 $options
中明確指定,否則此方法將自動為模型屬性產生 "name" 和 "value" 標籤屬性。
public static string activeTextInput ( $model, $attribute, $options = [] ) | ||
$model | yii\base\Model |
模型物件 |
$attribute | 字串 |
屬性名稱或表達式。 有關屬性表達式的格式,請參閱 getAttributeName()。 |
$options | 陣列 |
以名稱-值配對表示的標籤選項。這些選項將會被渲染為結果標籤的屬性。這些值將會使用 encode() 進行 HTML 編碼。有關屬性如何被渲染的詳細資訊,請參閱 renderTagAttributes()。以下特殊選項被識別
|
return | 字串 |
產生的輸入標籤 |
---|
public static function activeTextInput($model, $attribute, $options = [])
{
return static::activeInput('text', $model, $attribute, $options);
}
為給定的模型屬性產生文字區域標籤。
模型屬性值將用作 textarea 中的內容。
public static string activeTextarea ( $model, $attribute, $options = [] ) | ||
$model | yii\base\Model |
模型物件 |
$attribute | 字串 |
屬性名稱或表達式。 有關屬性表達式的格式,請參閱 getAttributeName()。 |
$options | 陣列 |
以名稱-值配對表示的標籤選項。這些選項將會被渲染為結果標籤的屬性。這些值將會使用 encode() 進行 HTML 編碼。有關屬性如何被渲染的詳細資訊,請參閱 renderTagAttributes()。以下特殊選項被識別
|
return | 字串 |
產生的 textarea 標籤 |
---|
public static function activeTextarea($model, $attribute, $options = [])
{
$name = isset($options['name']) ? $options['name'] : static::getInputName($model, $attribute);
if (isset($options['value'])) {
$value = $options['value'];
unset($options['value']);
} else {
$value = static::getAttributeValue($model, $attribute);
}
if (!array_key_exists('id', $options)) {
$options['id'] = static::getInputId($model, $attribute);
}
self::normalizeMaxLength($model, $attribute, $options);
static::setActivePlaceholder($model, $attribute, $options);
return static::textarea($name, $value, $options);
}
將 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;
}
}
將指定的 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 |
新的樣式字串(例如 |
$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;
}
產生表單開始標籤。
另請參閱 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()。 特殊選項
|
return | 字串 |
產生的表單開始標籤。 |
---|
public static function beginForm($action = '', $method = 'post', $options = [])
{
$action = Url::to($action);
$hiddenInputs = [];
$request = Yii::$app->getRequest();
if ($request instanceof Request) {
if (strcasecmp($method, 'get') && strcasecmp($method, 'post')) {
// simulate PUT, DELETE, etc. via POST
$hiddenInputs[] = static::hiddenInput($request->methodParam, $method);
$method = 'post';
}
$csrf = ArrayHelper::remove($options, 'csrf', true);
if ($csrf && $request->enableCsrfValidation && strcasecmp($method, 'post') === 0) {
$hiddenInputs[] = static::hiddenInput($request->csrfParam, $request->getCsrfToken());
}
}
if (!strcasecmp($method, 'get') && ($pos = strpos($action, '?')) !== false) {
// query parameters in the action are ignored for GET method
// we use hidden fields to add them back
foreach (explode('&', substr($action, $pos + 1)) as $pair) {
if (($pos1 = strpos($pair, '=')) !== false) {
$hiddenInputs[] = static::hiddenInput(
urldecode(substr($pair, 0, $pos1)),
urldecode(substr($pair, $pos1 + 1))
);
} else {
$hiddenInputs[] = static::hiddenInput(urldecode($pair), '');
}
}
$action = substr($action, 0, $pos);
}
$options['action'] = $action;
$options['method'] = $method;
$form = static::beginTag('form', $options);
if (!empty($hiddenInputs)) {
$form .= "\n" . implode("\n", $hiddenInputs);
}
return $form;
}
public static string beginTag ( $name, $options = [] ) | ||
$name | string|boolean|null |
標籤名稱。如果 $name 為 |
$options | 陣列 |
以名稱-值組表示的標籤選項。 這些將呈現為結果標籤的屬性。 這些值將使用 encode() 進行 HTML 編碼。 如果值為 null,則不會呈現對應的屬性。 有關屬性如何呈現的詳細資訊,請參閱 renderTagAttributes()。 |
return | 字串 |
產生的開始標籤 |
---|
public static function beginTag($name, $options = [])
{
if ($name === null || $name === false) {
return '';
}
return "<$name" . static::renderTagAttributes($options) . '>';
}
產生布林值輸入。
protected static string booleanInput ( $type, $name, $checked = false, $options = [] ) | ||
$type | 字串 |
輸入類型。 這可以是 |
$name | 字串 |
name 屬性。 |
$checked | 布林值 |
是否應勾選複選框。 |
$options | 陣列 |
以名稱-值組表示的標籤選項。 以下選項經過特殊處理
其餘選項將被渲染為結果複選框標籤的屬性。這些值將會使用 encode() 進行 HTML 編碼。如果值為 null,則對應的屬性將不會被渲染。有關屬性如何被渲染的詳細資訊,請參閱 renderTagAttributes()。 |
return | 字串 |
產生的核取方塊標籤 |
---|
protected static function booleanInput($type, $name, $checked = false, $options = [])
{
// 'checked' option has priority over $checked argument
if (!isset($options['checked'])) {
$options['checked'] = (bool) $checked;
}
$value = array_key_exists('value', $options) ? $options['value'] : '1';
if (isset($options['uncheck'])) {
// add a hidden field so that if the checkbox is not selected, it still submits a value
$hiddenOptions = [];
if (isset($options['form'])) {
$hiddenOptions['form'] = $options['form'];
}
// make sure disabled input is not sending any value
if (!empty($options['disabled'])) {
$hiddenOptions['disabled'] = $options['disabled'];
}
$hidden = static::hiddenInput($name, $options['uncheck'], $hiddenOptions);
unset($options['uncheck']);
} else {
$hidden = '';
}
if (isset($options['label'])) {
$label = $options['label'];
$labelOptions = isset($options['labelOptions']) ? $options['labelOptions'] : [];
unset($options['label'], $options['labelOptions']);
$content = static::label(static::input($type, $name, $value, $options) . ' ' . $label, null, $labelOptions);
return $hidden . $content;
}
return $hidden . static::input($type, $name, $value, $options);
}
產生核取方塊輸入。
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);
}
產生核取方塊列表。
複選框列表允許多選,類似於 listBox()。因此,對應的提交值是一個陣列。
public static string checkboxList ( $name, $selection = null, $items = [], $options = [] ) | ||
$name | 字串 |
每個複選框的 name 屬性。 |
$selection | string|array|null |
選定的值。字串用於單選,陣列用於多選。 |
$items | 陣列 |
用於產生複選框的資料項目。陣列鍵是複選框值,而陣列值是相應的標籤。 |
$options | 陣列 |
核取方塊列表容器標籤的選項 (名稱 => 配置)。 以下選項經過特殊處理
有關屬性如何呈現的詳細資訊,請參閱 renderTagAttributes()。 |
return | 字串 |
產生的核取方塊列表 |
---|
public static function checkboxList($name, $selection = null, $items = [], $options = [])
{
if (substr($name, -2) !== '[]') {
$name .= '[]';
}
if (ArrayHelper::isTraversable($selection)) {
$selection = array_map('strval', ArrayHelper::toArray($selection));
}
$formatter = ArrayHelper::remove($options, 'item');
$itemOptions = ArrayHelper::remove($options, 'itemOptions', []);
$encode = ArrayHelper::remove($options, 'encode', true);
$separator = ArrayHelper::remove($options, 'separator', "\n");
$tag = ArrayHelper::remove($options, 'tag', 'div');
$strict = ArrayHelper::remove($options, 'strict', false);
$lines = [];
$index = 0;
foreach ($items as $value => $label) {
$checked = $selection !== null &&
(!ArrayHelper::isTraversable($selection) && !strcmp($value, $selection)
|| ArrayHelper::isTraversable($selection) && ArrayHelper::isIn((string)$value, $selection, $strict));
if ($formatter !== null) {
$lines[] = call_user_func($formatter, $index, $label, $name, $checked, $value);
} else {
$lines[] = static::checkbox($name, $checked, array_merge([
'value' => $value,
'label' => $encode ? static::encode($label) : $label,
], $itemOptions));
}
$index++;
}
if (isset($options['unselect'])) {
// add a hidden field so that if the list box has no option being selected, it still submits a value
$name2 = substr($name, -2) === '[]' ? substr($name, 0, -2) : $name;
$hiddenOptions = [];
// make sure disabled input is not sending any value
if (!empty($options['disabled'])) {
$hiddenOptions['disabled'] = $options['disabled'];
}
$hidden = static::hiddenInput($name2, $options['unselect'], $hiddenOptions);
unset($options['unselect'], $options['disabled']);
} else {
$hidden = '';
}
$visibleContent = implode($separator, $lines);
if ($tag === false) {
return $hidden . $visibleContent;
}
return $hidden . static::tag($tag, $visibleContent, $options);
}
產生包含 CSRF 權杖資訊的中繼標籤。
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 '';
}
產生指向外部 CSS 檔案的連結標籤。
另請參閱 yii\helpers\Url::to()。
public static string cssFile ( $url, $options = [] ) | ||
$url | array|string |
外部 CSS 檔案的 URL。此參數將由 yii\helpers\Url::to() 處理。 |
$options | 陣列 |
以名稱-值組表示的標籤選項。 以下選項經過特殊處理
其餘選項將被渲染為結果 link 標籤的屬性。這些值將會使用 encode() 進行 HTML 編碼。如果值為 null,則對應的屬性將不會被渲染。有關屬性如何被渲染的詳細資訊,請參閱 renderTagAttributes()。 |
return | 字串 |
產生的 link 標籤 |
---|
public static function cssFile($url, $options = [])
{
if (!isset($options['rel'])) {
$options['rel'] = 'stylesheet';
}
$options['href'] = Url::to($url);
if (isset($options['condition'])) {
$condition = $options['condition'];
unset($options['condition']);
return self::wrapIntoCondition(static::tag('link', '', $options), $condition);
} elseif (isset($options['noscript']) && $options['noscript'] === true) {
unset($options['noscript']);
return '<noscript>' . static::tag('link', '', $options) . '</noscript>';
}
return static::tag('link', '', $options);
}
將 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);
}
將 CSS 樣式字串轉換為陣列表示法。
陣列的鍵是 CSS 屬性名稱,而陣列的值是相對應的 CSS 屬性值。
例如,
print_r(Html::cssStyleToArray('width: 100px; height: 200px;'));
// will display: ['width' => '100px', 'height' => '200px']
public static array cssStyleToArray ( $style ) | ||
$style | 字串 |
CSS 樣式字串 |
return | 陣列 |
CSS 樣式的陣列表示法 |
---|
public static function cssStyleToArray($style)
{
$result = [];
foreach (explode(';', $style) as $property) {
$property = explode(':', $property);
if (count($property) > 1) {
$result[trim($property[0])] = trim($property[1]);
}
}
return $result;
}
public static string decode ( $content ) | ||
$content | 字串 |
要解碼的內容 |
return | 字串 |
已解碼的內容 |
---|
public static function decode($content)
{
return htmlspecialchars_decode($content, ENT_QUOTES);
}
產生下拉式列表。
public static string dropDownList ( $name, $selection = null, $items = [], $options = [] ) | ||
$name | 字串 |
輸入名稱 |
$selection | string|boolean|array|null |
選取的值。字串/布林值用於單一選取,陣列用於多重選取。 |
$items | 陣列 |
選項資料項目。 陣列鍵是選項值,而陣列值是相應的選項標籤。 陣列也可以是巢狀的 (即,某些陣列值也是陣列)。 對於每個子陣列,將產生一個選項組,其標籤是與子陣列關聯的鍵。 如果您有資料模型列表,您可以使用 yii\helpers\ArrayHelper::map() 將它們轉換為上述格式。 請注意,此方法會自動對值和標籤進行 HTML 編碼,並且標籤中的空白字元也將進行 HTML 編碼。 |
$options | 陣列 |
以名稱-值組表示的標籤選項。 以下選項經過特殊處理
其餘選項將呈現為結果標籤的屬性。 這些值將使用 encode() 進行 HTML 編碼。 如果值為 null,則不會呈現對應的屬性。 有關屬性如何呈現的詳細資訊,請參閱 renderTagAttributes()。 |
return | 字串 |
產生的下拉式列表標籤 |
---|
public static function dropDownList($name, $selection = null, $items = [], $options = [])
{
if (!empty($options['multiple'])) {
return static::listBox($name, $selection, $items, $options);
}
$options['name'] = $name;
unset($options['unselect']);
$selectOptions = static::renderSelectOptions($selection, $items, $options);
return static::tag('select', "\n" . $selectOptions . "\n", $options);
}
public static string encode ( $content, $doubleEncode = true ) | ||
$content | 字串 |
要編碼的內容 |
$doubleEncode | 布林值 |
是否編碼 |
return | 字串 |
已編碼的內容 |
---|
public static function encode($content, $doubleEncode = true)
{
return htmlspecialchars((string)$content, ENT_QUOTES | ENT_SUBSTITUTE, Yii::$app ? Yii::$app->charset : 'UTF-8', $doubleEncode);
}
產生表單結束標籤。
另請參閱 beginForm()。
public static string endForm ( ) | ||
return | 字串 |
產生的標籤 |
---|
public static function endForm()
{
return '</form>';
}
public static string endTag ( $name ) | ||
$name | string|boolean|null |
標籤名稱。如果 $name 為 |
return | 字串 |
產生的結束標籤 |
---|
public static function endTag($name)
{
if ($name === null || $name === false) {
return '';
}
return "</$name>";
}
產生包含指定模型屬性的第一個驗證錯誤的標籤。
請注意,即使沒有驗證錯誤,此方法仍會傳回空的錯誤標籤。
public static string error ( $model, $attribute, $options = [] ) | ||
$model | yii\base\Model |
模型物件 |
$attribute | 字串 |
屬性名稱或表達式。 有關屬性表達式的格式,請參閱 getAttributeName()。 |
$options | 陣列 |
標籤選項,以名稱-值對的形式呈現。這些值將使用 encode() 進行 HTML 編碼。如果值為 null,則不會呈現對應的屬性。 以下選項會被特殊處理
有關屬性如何呈現的詳細資訊,請參閱 renderTagAttributes()。 |
return | 字串 |
產生的標籤標籤 |
---|
public static function error($model, $attribute, $options = [])
{
$attribute = static::getAttributeName($attribute);
$errorSource = ArrayHelper::remove($options, 'errorSource');
if ($errorSource !== null) {
$error = call_user_func($errorSource, $model, $attribute);
} else {
$error = $model->getFirstError($attribute);
}
$tag = ArrayHelper::remove($options, 'tag', 'div');
$encode = ArrayHelper::remove($options, 'encode', true);
return Html::tag($tag, $encode ? Html::encode($error) : $error, $options);
}
產生驗證錯誤的摘要。
如果沒有驗證錯誤,仍會產生空的錯誤摘要標記,但它將會被隱藏。
public static string errorSummary ( $models, $options = [] ) | ||
$models | yii\base\Model|yii\base\Model[] |
要顯示其驗證錯誤的模型。 |
$options | 陣列 |
以名稱-值組表示的標籤選項。 以下選項經過特殊處理
其餘選項將呈現為容器標籤的屬性。 |
return | 字串 |
產生的錯誤摘要 |
---|
public static function errorSummary($models, $options = [])
{
$header = isset($options['header']) ? $options['header'] : '<p>' . Yii::t('yii', 'Please fix the following errors:') . '</p>';
$footer = ArrayHelper::remove($options, 'footer', '');
$encode = ArrayHelper::remove($options, 'encode', true);
$showAllErrors = ArrayHelper::remove($options, 'showAllErrors', false);
$emptyClass = ArrayHelper::remove($options, 'emptyClass', null);
unset($options['header']);
$lines = self::collectErrors($models, $encode, $showAllErrors);
if (empty($lines)) {
// still render the placeholder for client-side validation use
$content = '<ul></ul>';
if ($emptyClass !== null) {
$options['class'] = $emptyClass;
} else {
$options['style'] = isset($options['style']) ? rtrim($options['style'], ';') . '; display:none' : 'display:none';
}
} else {
$content = '<ul><li>' . implode("</li>\n<li>", $lines) . '</li></ul>';
}
return Html::tag('div', $header . $content . $footer, $options);
}
跳脫正規表示式以在 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;
}
產生檔案輸入欄位。
若要使用檔案輸入欄位,您應該將封閉表單的 "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);
}
從給定的屬性表達式傳回真實的屬性名稱。
屬性運算式是屬性名稱,其前綴和/或後綴帶有陣列索引。它主要用於表格資料輸入和/或陣列類型的輸入。以下是一些範例
[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.');
}
傳回指定屬性名稱或表達式的值。
對於像 [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;
}
為指定的屬性名稱或表達式產生適當的輸入 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);
}
將輸入名稱轉換為 ID。
例如,如果 $name
是 Post[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);
}
為指定的屬性名稱或表達式產生適當的輸入名稱。
此方法產生一個名稱,可用作輸入名稱,以收集指定屬性的使用者輸入。名稱是根據模型的 表單名稱 和給定的屬性名稱產生的。例如,如果 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.');
}
產生圖片標籤。
public static string img ( $src, $options = [] ) | ||
$src | array|string |
圖片 URL。此參數將由 yii\helpers\Url::to() 處理。 |
$options | 陣列 |
以名稱-值組表示的標籤選項。 這些將呈現為結果標籤的屬性。 這些值將使用 encode() 進行 HTML 編碼。 如果值為 null,則不會呈現對應的屬性。 有關屬性如何呈現的詳細資訊,請參閱 renderTagAttributes()。 自 2.0.12 版本起,可以將 |
return | 字串 |
產生的圖片標籤。 |
---|
public static function img($src, $options = [])
{
$options['src'] = Url::to($src);
if (isset($options['srcset']) && is_array($options['srcset'])) {
$srcset = [];
foreach ($options['srcset'] as $descriptor => $url) {
$srcset[] = Url::to($url) . ' ' . $descriptor;
}
$options['srcset'] = implode(',', $srcset);
}
if (!isset($options['alt'])) {
$options['alt'] = '';
}
return static::tag('img', '', $options);
}
產生給定類型的輸入類型。
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);
}
產生指向外部 JavaScript 檔案的 script 標籤。
另請參閱 yii\helpers\Url::to()。
public static string jsFile ( $url, $options = [] ) | ||
$url | 字串 |
外部 JavaScript 檔案的 URL。此參數將由 yii\helpers\Url::to() 處理。 |
$options | 陣列 |
標籤選項,以名稱-值對的形式呈現。以下選項會被特殊處理
其餘選項將呈現為結果 script 標籤的屬性。這些值將使用 encode() 進行 HTML 編碼。如果值為 null,則不會呈現對應的屬性。有關屬性如何呈現的詳細資訊,請參閱 renderTagAttributes()。 |
return | 字串 |
產生的 script 標籤 |
---|
public static function jsFile($url, $options = [])
{
$options['src'] = Url::to($url);
if (isset($options['condition'])) {
$condition = $options['condition'];
unset($options['condition']);
return self::wrapIntoCondition(static::tag('script', '', $options), $condition);
}
return static::tag('script', '', $options);
}
產生標籤標籤。
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);
}
產生列表方塊。
public static string listBox ( $name, $selection = null, $items = [], $options = [] ) | ||
$name | 字串 |
輸入名稱 |
$selection | string|boolean|array|null |
選定的值。字串用於單選,陣列用於多選。 |
$items | 陣列 |
選項資料項目。 陣列鍵是選項值,而陣列值是相應的選項標籤。 陣列也可以是巢狀的 (即,某些陣列值也是陣列)。 對於每個子陣列,將產生一個選項組,其標籤是與子陣列關聯的鍵。 如果您有資料模型列表,您可以使用 yii\helpers\ArrayHelper::map() 將它們轉換為上述格式。 請注意,此方法會自動對值和標籤進行 HTML 編碼,並且標籤中的空白字元也將進行 HTML 編碼。 |
$options | 陣列 |
以名稱-值組表示的標籤選項。 以下選項經過特殊處理
其餘選項將呈現為結果標籤的屬性。 這些值將使用 encode() 進行 HTML 編碼。 如果值為 null,則不會呈現對應的屬性。 有關屬性如何呈現的詳細資訊,請參閱 renderTagAttributes()。 |
return | 字串 |
產生的列表框標籤 |
---|
public static function listBox($name, $selection = null, $items = [], $options = [])
{
if (!array_key_exists('size', $options)) {
$options['size'] = 4;
}
if (!empty($options['multiple']) && !empty($name) && substr_compare($name, '[]', -2, 2)) {
$name .= '[]';
}
$options['name'] = $name;
if (isset($options['unselect'])) {
// add a hidden field so that if the list box has no option being selected, it still submits a value
if (!empty($name) && substr_compare($name, '[]', -2, 2) === 0) {
$name = substr($name, 0, -2);
}
$hiddenOptions = [];
// make sure disabled input is not sending any value
if (!empty($options['disabled'])) {
$hiddenOptions['disabled'] = $options['disabled'];
}
$hidden = static::hiddenInput($name, $options['unselect'], $hiddenOptions);
unset($options['unselect']);
} else {
$hidden = '';
}
$selectOptions = static::renderSelectOptions($selection, $items, $options);
return $hidden . static::tag('select', "\n" . $selectOptions . "\n", $options);
}
產生 mailto 超連結。
public static string mailto ( $text, $email = null, $options = [] ) | ||
$text | 字串 |
連結主體。它將不會進行 HTML 編碼。 因此,您可以傳入 HTML 程式碼,例如圖片標籤。 如果這來自終端使用者,您應該考慮 encode() 以防止 XSS 攻擊。 |
string|null |
電子郵件地址。如果為 null,則第一個參數 (連結主體) 將被視為電子郵件地址並使用。 |
|
$options | 陣列 |
以名稱-值組表示的標籤選項。 這些將呈現為結果標籤的屬性。 這些值將使用 encode() 進行 HTML 編碼。 如果值為 null,則不會呈現對應的屬性。 有關屬性如何呈現的詳細資訊,請參閱 renderTagAttributes()。 |
return | 字串 |
產生的 mailto 連結 |
---|
public static function mailto($text, $email = null, $options = [])
{
$options['href'] = 'mailto:' . ($email === null ? $text : $email);
return static::tag('a', $text, $options);
}
產生有序列表。
public static string ol ( $items, $options = [] ) | ||
$items | array|Traversable |
用於產生清單的項目。每個項目都會產生一個清單項目。請注意,如果未設定 |
$options | 陣列 |
選項 (name => config) 用於單選按鈕清單。支援以下選項
有關屬性如何呈現的詳細資訊,請參閱 renderTagAttributes()。 |
return | 字串 |
產生的有序清單。如果 |
---|
public static function ol($items, $options = [])
{
$options['tag'] = 'ol';
return static::ul($items, $options);
}
產生密碼輸入欄位。
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);
}
產生單選按鈕輸入。
public static string radio ( $name, $checked = false, $options = [] ) | ||
$name | 字串 |
name 屬性。 |
$checked | 布林值 |
是否應選取單選按鈕。 |
$options | 陣列 |
以名稱-值組表示的標籤選項。 有關接受的屬性的詳細資訊,請參閱 booleanInput()。 |
return | 字串 |
產生的單選按鈕標籤 |
---|
public static function radio($name, $checked = false, $options = [])
{
return static::booleanInput('radio', $name, $checked, $options);
}
產生單選按鈕列表。
單選按鈕清單與核取方塊清單類似,不同之處在於它僅允許單選。
public static string radioList ( $name, $selection = null, $items = [], $options = [] ) | ||
$name | 字串 |
每個單選按鈕的 name 屬性。 |
$selection | string|array|null |
選定的值。字串用於單選,陣列用於多選。 |
$items | 陣列 |
用於產生單選按鈕的資料項目。陣列鍵是單選按鈕的值,而陣列值是相對應的標籤。 |
$options | 陣列 |
單選按鈕列表容器標籤的選項(name => config)。以下選項會被特別處理
有關屬性如何呈現的詳細資訊,請參閱 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);
}
從指定的選項中移除 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);
}
}
}
}
從 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);
}
}
呈現可由 dropDownList() 和 listBox() 使用的選項標籤。
public static string renderSelectOptions ( $selection, $items, &$tagOptions = [] ) | ||
$selection | string|array|boolean|null |
選取的值。字串/布林值用於單一選取,陣列用於多重選取。 |
$items | 陣列 |
選項資料項目。 陣列鍵是選項值,而陣列值是相應的選項標籤。 陣列也可以是巢狀的 (即,某些陣列值也是陣列)。 對於每個子陣列,將產生一個選項組,其標籤是與子陣列關聯的鍵。 如果您有資料模型列表,您可以使用 yii\helpers\ArrayHelper::map() 將它們轉換為上述格式。 請注意,此方法會自動對值和標籤進行 HTML 編碼,並且標籤中的空白字元也將進行 HTML 編碼。 |
$tagOptions | 陣列 |
傳遞至 dropDownList() 或 listBox() 呼叫的 |
return | 字串 |
產生的列表選項 |
---|
public static function renderSelectOptions($selection, $items, &$tagOptions = [])
{
if (ArrayHelper::isTraversable($selection)) {
$normalizedSelection = [];
foreach (ArrayHelper::toArray($selection) as $selectionItem) {
if (is_bool($selectionItem)) {
$normalizedSelection[] = $selectionItem ? '1' : '0';
} else {
$normalizedSelection[] = (string)$selectionItem;
}
}
$selection = $normalizedSelection;
} elseif (is_bool($selection)) {
$selection = $selection ? '1' : '0';
}
$lines = [];
$encodeSpaces = ArrayHelper::remove($tagOptions, 'encodeSpaces', false);
$encode = ArrayHelper::remove($tagOptions, 'encode', true);
$strict = ArrayHelper::remove($tagOptions, 'strict', false);
if (isset($tagOptions['prompt'])) {
$promptOptions = ['value' => ''];
if (is_string($tagOptions['prompt'])) {
$promptText = $tagOptions['prompt'];
} else {
$promptText = $tagOptions['prompt']['text'];
$promptOptions = array_merge($promptOptions, $tagOptions['prompt']['options']);
}
$promptText = $encode ? static::encode($promptText) : $promptText;
if ($encodeSpaces) {
$promptText = str_replace(' ', ' ', $promptText);
}
$lines[] = static::tag('option', $promptText, $promptOptions);
}
$options = isset($tagOptions['options']) ? $tagOptions['options'] : [];
$groups = isset($tagOptions['groups']) ? $tagOptions['groups'] : [];
unset($tagOptions['prompt'], $tagOptions['options'], $tagOptions['groups']);
$options['encodeSpaces'] = ArrayHelper::getValue($options, 'encodeSpaces', $encodeSpaces);
$options['encode'] = ArrayHelper::getValue($options, 'encode', $encode);
foreach ($items as $key => $value) {
if (is_array($value)) {
$groupAttrs = isset($groups[$key]) ? $groups[$key] : [];
if (!isset($groupAttrs['label'])) {
$groupAttrs['label'] = $key;
}
$attrs = ['options' => $options, 'groups' => $groups, 'encodeSpaces' => $encodeSpaces, 'encode' => $encode, 'strict' => $strict];
$content = static::renderSelectOptions($selection, $value, $attrs);
$lines[] = static::tag('optgroup', "\n" . $content . "\n", $groupAttrs);
} else {
$attrs = isset($options[$key]) ? $options[$key] : [];
$attrs['value'] = (string) $key;
if (!array_key_exists('selected', $attrs)) {
$selected = false;
if ($selection !== null) {
if (ArrayHelper::isTraversable($selection)) {
$selected = ArrayHelper::isIn((string)$key, $selection, $strict);
} elseif ($key === '' || $selection === '') {
$selected = $selection === $key;
} elseif ($strict) {
$selected = !strcmp((string)$key, (string)$selection);
} else {
$selected = $selection == $key;
}
}
$attrs['selected'] = $selected;
}
$text = $encode ? static::encode($value) : $value;
if ($encodeSpaces) {
$text = str_replace(' ', ' ', $text);
}
$lines[] = static::tag('option', $text, $attrs);
}
}
return implode("\n", $lines);
}
呈現 HTML 標籤屬性。
值為布林類型的屬性將被視為布林屬性。
值為 null 的屬性將不會被渲染。
屬性的值將使用 encode() 進行 HTML 編碼。
當 aria
和 data
屬性設定為陣列值時,會進行特殊處理。 在這些情況下,陣列將被「展開」並渲染 ARIA/data 屬性列表。 例如,'aria' => ['role' => 'checkbox', 'value' => 'true']
將被渲染為 aria-role="checkbox" aria-value="true"
。
如果巢狀的 data
值設定為陣列,它將會被 JSON 編碼。 例如,'data' => ['params' => ['id' => 1, 'name' => 'yii']]
將被渲染為 data-params='{"id":1,"name":"yii"}'
。
另請參閱 addCssClass()。
public static string renderTagAttributes ( $attributes ) | ||
$attributes | 陣列 |
要渲染的屬性。 屬性值將使用 encode() 進行 HTML 編碼。 |
return | 字串 |
渲染結果。 如果屬性不為空,它們將被渲染成帶有前導空格的字串(以便它可以直接附加到標籤名稱中)。 如果沒有屬性,將返回空字串。 |
---|
public static function renderTagAttributes($attributes)
{
if (count($attributes) > 1) {
$sorted = [];
foreach (static::$attributeOrder as $name) {
if (isset($attributes[$name])) {
$sorted[$name] = $attributes[$name];
}
}
$attributes = array_merge($sorted, $attributes);
}
$html = '';
foreach ($attributes as $name => $value) {
if (is_bool($value)) {
if ($value) {
$html .= " $name";
}
} elseif (is_array($value)) {
if (in_array($name, static::$dataAttributes)) {
foreach ($value as $n => $v) {
if (is_array($v)) {
$html .= " $name-$n='" . Json::htmlEncode($v) . "'";
} elseif (is_bool($v)) {
if ($v) {
$html .= " $name-$n";
}
} elseif ($v !== null) {
$html .= " $name-$n=\"" . static::encode($v) . '"';
}
}
} elseif ($name === 'class') {
if (empty($value)) {
continue;
}
if (static::$normalizeClassAttribute === true && count($value) > 1) {
// removes duplicate classes
$value = explode(' ', implode(' ', $value));
$value = array_unique($value);
}
$html .= " $name=\"" . static::encode(implode(' ', array_filter($value))) . '"';
} elseif ($name === 'style') {
if (empty($value)) {
continue;
}
$html .= " $name=\"" . static::encode(static::cssStyleFromArray($value)) . '"';
} else {
$html .= " $name='" . Json::htmlEncode($value) . "'";
}
} elseif ($value !== null) {
$html .= " $name=\"" . static::encode($value) . '"';
}
}
return $html;
}
產生重設按鈕標籤。
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);
}
產生重設輸入按鈕。
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 string script ( $content, $options = [] ) | ||
$content | 字串 |
腳本內容 |
$options | 陣列 |
以名稱-值組表示的標籤選項。 這些將呈現為結果標籤的屬性。 這些值將使用 encode() 進行 HTML 編碼。 如果值為 null,則不會呈現對應的屬性。 有關屬性如何呈現的詳細資訊,請參閱 renderTagAttributes()。 |
return | 字串 |
產生的 script 標籤 |
---|
public static function script($content, $options = [])
{
return static::tag('script', $content, $options);
}
從模型屬性標籤產生佔位符。
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 string style ( $content, $options = [] ) | ||
$content | 字串 |
樣式內容 |
$options | 陣列 |
以名稱-值組表示的標籤選項。 這些將呈現為結果標籤的屬性。 這些值將使用 encode() 進行 HTML 編碼。 如果值為 null,則不會呈現對應的屬性。 有關屬性如何呈現的詳細資訊,請參閱 renderTagAttributes()。 |
return | 字串 |
產生的樣式標籤 |
---|
public static function style($content, $options = [])
{
return static::tag('style', $content, $options);
}
產生送出按鈕標籤。
命名表單元素(例如提交按鈕)時請小心。 根據 jQuery 文件,有些保留名稱可能會導致衝突,例如 submit
、length
或 method
。
public static string submitButton ( $content = 'Submit', $options = [] ) | ||
$content | 字串 |
按鈕標籤內封閉的內容。它將不會進行 HTML 編碼。因此,您可以傳入 HTML 代碼,例如圖像標籤。如果這來自最終使用者,您應該考慮使用 encode() 進行編碼,以防止 XSS 攻擊。 |
$options | 陣列 |
以名稱-值組表示的標籤選項。 這些將呈現為結果標籤的屬性。 這些值將使用 encode() 進行 HTML 編碼。 如果值為 null,則不會呈現對應的屬性。 有關屬性如何呈現的詳細資訊,請參閱 renderTagAttributes()。 |
return | 字串 |
產生的提交按鈕標籤 |
---|
public static function submitButton($content = 'Submit', $options = [])
{
$options['type'] = 'submit';
return static::button($content, $options);
}
產生送出輸入按鈕。
命名表單元素(例如提交按鈕)時請小心。 根據 jQuery 文件,有些保留名稱可能會導致衝突,例如 submit
、length
或 method
。
public static string submitInput ( $label = 'Submit', $options = [] ) | ||
$label | string|null |
value 屬性。如果為 null,則不會產生 value 屬性。 |
$options | 陣列 |
以名稱-值組表示的標籤選項。 這些將呈現為結果標籤的屬性。 這些值將使用 encode() 進行 HTML 編碼。 如果值為 null,則不會呈現對應的屬性。 有關屬性如何呈現的詳細資訊,請參閱 renderTagAttributes()。 |
return | 字串 |
產生的按鈕標籤 |
---|
public static function submitInput($label = 'Submit', $options = [])
{
$options['type'] = 'submit';
$options['value'] = $label;
return static::tag('input', '', $options);
}
public static string tag ( $name, $content = '', $options = [] ) | ||
$name | string|boolean|null |
標籤名稱。如果 $name 為 |
$content | 字串 |
要包含在開始和結束標籤之間的內容。 它不會進行 HTML 編碼。 如果內容來自最終使用者,您應考慮使用 encode() 進行編碼,以防止 XSS 攻擊。 |
$options | 陣列 |
HTML 標籤屬性(HTML 選項),以名稱-值對形式表示。 這些將被渲染為結果標籤的屬性。 這些值將使用 encode() 進行 HTML 編碼。 如果值為 null,則不會渲染對應的屬性。 例如,當使用 有關屬性如何呈現的詳細資訊,請參閱 renderTagAttributes()。 |
return | 字串 |
產生的 HTML 標籤 |
---|
public static function tag($name, $content = '', $options = [])
{
if ($name === null || $name === false) {
return $content;
}
$html = "<$name" . static::renderTagAttributes($options) . '>';
return isset(static::$voidElements[strtolower($name)]) ? $html : "$html$content</$name>";
}
產生文字輸入欄位。
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);
}
產生文字區域輸入。
public static string textarea ( $name, $value = '', $options = [] ) | ||
$name | 字串 |
輸入名稱 |
$value | 字串 |
輸入值。 請注意,它將使用 encode() 進行編碼。 |
$options | 陣列 |
以名稱-值對形式表示的標籤選項。 這些將被渲染為結果標籤的屬性。 這些值將使用 encode() 進行 HTML 編碼。 如果值為 null,則不會渲染對應的屬性。 有關屬性如何渲染的詳細資訊,請參閱 renderTagAttributes()。 以下是可辨識的特殊選項
|
return | 字串 |
產生的文字區域標籤 |
---|
public static function textarea($name, $value = '', $options = [])
{
$options['name'] = $name;
$doubleEncode = ArrayHelper::remove($options, 'doubleEncode', true);
return static::tag('textarea', static::encode($value, $doubleEncode), $options);
}
產生無序列表。
public static string ul ( $items, $options = [] ) | ||
$items | array|Traversable |
用於產生清單的項目。每個項目都會產生一個清單項目。請注意,如果未設定 |
$options | 陣列 |
選項 (name => config) 用於單選按鈕清單。支援以下選項
有關屬性如何呈現的詳細資訊,請參閱 renderTagAttributes()。 |
return | 字串 |
產生的無序列表。 如果 |
---|
public static function ul($items, $options = [])
{
$tag = ArrayHelper::remove($options, 'tag', 'ul');
$encode = ArrayHelper::remove($options, 'encode', true);
$formatter = ArrayHelper::remove($options, 'item');
$separator = ArrayHelper::remove($options, 'separator', "\n");
$itemOptions = ArrayHelper::remove($options, 'itemOptions', []);
if (empty($items)) {
return static::tag($tag, '', $options);
}
$results = [];
foreach ($items as $index => $item) {
if ($formatter !== null) {
$results[] = call_user_func($formatter, $item, $index);
} else {
$results[] = static::tag('li', $encode ? static::encode($item) : $item, $itemOptions);
}
}
return static::tag(
$tag,
$separator . implode($separator, $results) . $separator,
$options
);
}
註冊 或 登入 以發表評論。