クラス 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]
の場合、1 つの属性ではなく、data-name="xyz" data-age="13"
という 2 つの属性が生成されます。
空要素のリスト (要素名 => 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,
]
メソッドの詳細
ハイパーリンクタグを生成します。
public static string a ( $text, $url = null, $options = [] ) | ||
$text | 文字列 |
リンク本文。HTMLエンコードされません。そのため、画像タグなどのHTMLコードを渡すことができます。エンドユーザーからの入力を受け取る場合は、XSS攻撃を防ぐためにencode() を検討する必要があります。 |
$url | array|string|null |
ハイパーリンクタグのURL。このパラメータはyii\helpers\Url::to() によって処理され、タグの "href" 属性に使用されます。このパラメータがnullの場合、"href" 属性は生成されません。 絶対URLを使用したい場合は、このメソッドにURLを渡す前に、次のようにしてyii\helpers\Url::to() を自分で呼び出すことができます。
|
$options | 配列 |
名前と値のペアで表されるタグオプション。これらは、結果のタグの属性としてレンダリングされます。値はencode() を使用してHTMLエンコードされます。値がnullの場合、対応する属性はレンダリングされません。属性のレンダリング方法の詳細については、renderTagAttributes() を参照してください。 |
戻り値 | 文字列 |
生成されたハイパーリンク |
---|
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() で、受け入れられる属性の詳細を確認してください。 |
戻り値 | 文字列 |
生成された入力要素 |
---|
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() で、受け入れられる属性の詳細を確認してください。 |
戻り値 | 文字列 |
生成されるチェックボックスタグ |
---|
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 | 配列 |
チェックボックスリストコンテナタグのオプション(name => config)。以下のオプションは特別に処理されます。
属性のレンダリング方法の詳細については、renderTagAttributes()を参照してください。 |
戻り値 | 文字列 |
生成されるチェックボックスリスト |
---|
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()で、属性のレンダリング方法の詳細を参照してください。 |
戻り値 | 文字列 |
生成されるドロップダウンリストタグ |
---|
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」タグ属性を自動的に生成します。さらに、`hiddenOptions`というキーを持つ別々のHTMLオプション配列が`$options`内に定義されている場合、それは自身の`$options`パラメータとして`activeHiddenInput`フィールドに渡されます。
public static string activeFileInput ( $model, $attribute, $options = [] ) | ||
$model | yii\base\Model |
モデルオブジェクト |
$attribute | 文字列 |
属性名または式。getAttributeName() を参照して、属性式の形式を確認してください。 |
$options | 配列 |
名前と値のペアによるタグオプション。これらは、結果のタグの属性としてレンダリングされます。値はencode()を使用してHTMLエンコードされます。renderTagAttributes()で、属性のレンダリング方法の詳細を参照してください。別のHTMLオプション配列である`hiddenOptions`パラメータが定義されている場合、それは非表示入力に使用するために`$options`から抽出されます。 |
戻り値 | 文字列 |
生成される入力タグ |
---|
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()で、属性のレンダリング方法の詳細を参照してください。 |
戻り値 | 文字列 |
生成される入力タグ |
---|
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()を参照してください。 |
戻り値 | 文字列 |
生成されるヒントタグ |
---|
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()で、属性のレンダリング方法の詳細を参照してください。 |
戻り値 | 文字列 |
生成される入力タグ |
---|
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()を参照してください。 |
戻り値 | 文字列 |
生成されたラベルタグ |
---|
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()で、属性のレンダリング方法の詳細を参照してください。 |
戻り値 | 文字列 |
生成されたリストボックスタグ |
---|
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)。サポートされている特別なオプションは、 |
戻り値 | 文字列 |
生成された入力リスト |
---|
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()で、属性のレンダリング方法の詳細を参照してください。次の特別なオプションが認識されます。
|
戻り値 | 文字列 |
生成される入力タグ |
---|
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() で、受け入れられる属性の詳細を確認してください。 |
戻り値 | 文字列 |
生成されたラジオボタンタグ |
---|
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()を参照してください。 |
戻り値 | 文字列 |
生成されたラジオボタンリスト |
---|
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()で、属性のレンダリング方法の詳細を参照してください。次の特別なオプションが認識されます。
|
戻り値 | 文字列 |
生成される入力タグ |
---|
public static function activeTextInput($model, $attribute, $options = [])
{
return static::activeInput('text', $model, $attribute, $options);
}
指定されたモデル属性のテキストエリアタグを生成します。
モデル属性値は、テキストエリアの内容として使用されます。
public static string activeTextarea ( $model, $attribute, $options = [] ) | ||
$model | yii\base\Model |
モデルオブジェクト |
$attribute | 文字列 |
属性名または式。getAttributeName() を参照して、属性式の形式を確認してください。 |
$options | 配列 |
名前と値のペアによるタグオプション。これらは、結果のタグの属性としてレンダリングされます。値はencode()を使用してHTMLエンコードされます。renderTagAttributes()で、属性のレンダリング方法の詳細を参照してください。次の特別なオプションが認識されます。
|
戻り値 | 文字列 |
生成されたテキストエリアタグ |
---|
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 | 文字列|配列 |
追加する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 | 文字列|配列 |
新しいスタイル文字列(例: |
$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 文字列 beginForm ( $action = '', $method = 'post', $options = [] ) | ||
$action | 配列|文字列 |
フォームのアクションURL。このパラメータは yii\helpers\Url::to() によって処理されます。 |
$method | 文字列 |
フォームの送信方法(例:"post"、"get"、"put"、"delete"。大文字小文字は区別されません)。ほとんどのブラウザは"post"と"get"しかサポートしていないため、他のメソッドが指定された場合は"post"を使用してシミュレートされ、実際のメソッドタイプを含む非表示の入力フィールドが追加されます。yii\web\Request::$methodParam を参照してください。 |
$options | 配列 |
名前と値のペアで表されるタグオプション。これらは、結果のタグの属性としてレンダリングされます。値はencode() を使用してHTMLエンコードされます。値がnullの場合、対応する属性はレンダリングされません。属性のレンダリング方法の詳細については、renderTagAttributes() を参照してください。 特別なオプション
|
戻り値 | 文字列 |
生成されたフォームの開始タグ。 |
---|
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 文字列 beginTag ( $name, $options = [] ) | ||
$name | 文字列|ブール値|null |
タグ名。$nameが |
$options | 配列 |
名前と値のペアで表されるタグオプション。これらは、結果のタグの属性としてレンダリングされます。値はencode() を使用してHTMLエンコードされます。値がnullの場合、対応する属性はレンダリングされません。属性のレンダリング方法の詳細については、renderTagAttributes() を参照してください。 |
戻り値 | 文字列 |
生成された開始タグ |
---|
public static function beginTag($name, $options = [])
{
if ($name === null || $name === false) {
return '';
}
return "<$name" . static::renderTagAttributes($options) . '>';
}
ブール入力を生成します。
protected static 文字列 booleanInput ( $type, $name, $checked = false, $options = [] ) | ||
$type | 文字列 |
入力タイプ。 |
$name | 文字列 |
name属性。 |
$checked | ブール値 |
チェックボックスをチェックするかどうか。 |
$options | 配列 |
名前と値のペアによるタグオプション。以下のオプションは特別に処理されます。
残りのオプションは、結果のチェックボックスタグの属性としてレンダリングされます。値は encode() を使用してHTMLエンコードされます。値がnullの場合、対応する属性はレンダリングされません。属性のレンダリング方法の詳細については、renderTagAttributes() を参照してください。 |
戻り値 | 文字列 |
生成されるチェックボックスタグ |
---|
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 文字列 checkbox ( $name, $checked = false, $options = [] ) | ||
$name | 文字列 |
name属性。 |
$checked | ブール値 |
チェックボックスをチェックするかどうか。 |
$options | 配列 |
名前と値のペアで表されるタグオプション。booleanInput() で、受け入れられる属性の詳細を確認してください。 |
戻り値 | 文字列 |
生成されるチェックボックスタグ |
---|
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 | 配列 |
チェックボックスリストコンテナタグのオプション(name => config)。以下のオプションは特別に処理されます。
属性のレンダリング方法の詳細については、renderTagAttributes()を参照してください。 |
戻り値 | 文字列 |
生成されるチェックボックスリスト |
---|
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トークン情報を含むメタタグを生成します。
こちらも参照してください yii\web\Request::$enableCsrfValidation.
public static string csrfMetaTags ( ) | ||
戻り値 | 文字列 |
生成されたメタタグ |
---|
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ファイルを参照するリンクタグを生成します。
public static string cssFile ( $url, $options = [] ) | ||
$url | 配列|文字列 |
外部CSSファイルのURL。このパラメータはyii\helpers\Url::to()によって処理されます。 |
$options | 配列 |
名前と値のペアによるタグオプション。以下のオプションは特別に処理されます。
残りのオプションは、結果のlinkタグの属性としてレンダリングされます。値はencode()を使用してHTMLエンコードされます。値がnullの場合、対応する属性はレンダリングされません。renderTagAttributes()で属性のレンダリング方法の詳細を参照してください。 |
戻り値 | 文字列 |
生成された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プロパティ値です。 |
戻り値 | 文字列 |
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スタイル文字列 |
戻り値 | 配列 |
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 | 文字列 |
デコードされるコンテンツ |
戻り値 | 文字列 |
デコードされたコンテンツ |
---|
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()で、属性のレンダリング方法の詳細を参照してください。 |
戻り値 | 文字列 |
生成されるドロップダウンリストタグ |
---|
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 | ブール値 |
|
戻り値 | 文字列 |
エンコードされたコンテンツ |
---|
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 ( ) | ||
戻り値 | 文字列 |
生成されたタグ |
---|
public static function endForm()
{
return '</form>';
}
public static string endTag ( $name ) | ||
$name | 文字列|ブール値|null |
タグ名。$nameが |
戻り値 | 文字列 |
生成された終了タグ |
---|
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()を参照してください。 |
戻り値 | 文字列 |
生成されたラベルタグ |
---|
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 | 配列 |
名前と値のペアによるタグオプション。以下のオプションは特別に処理されます。
その他のオプションは、コンテナタグの属性としてレンダリングされます。 |
戻り値 | 文字列 |
生成されたエラーサマリー |
---|
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 | 文字列 |
エスケープする正規表現。 |
戻り値 | 文字列 |
エスケープされた結果。 |
---|
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 | 文字列|null |
value属性。nullの場合、value属性は生成されません。 |
$options | 配列 |
名前と値のペアで表されるタグオプション。これらは、結果のタグの属性としてレンダリングされます。値はencode() を使用してHTMLエンコードされます。値がnullの場合、対応する属性はレンダリングされません。属性のレンダリング方法の詳細については、renderTagAttributes() を参照してください。 |
戻り値 | 文字列 |
生成されたファイル入力タグ |
---|
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 | 文字列 |
属性名または属性式 |
戻り値 | 文字列 |
接頭辞と接尾辞のない属性名。 |
---|---|---|
例外発生 | 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 | 文字列 |
属性名または属性式 |
戻り値 | 文字列|配列 |
対応する属性値 |
---|---|---|
例外発生 | 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()で属性式の説明を参照してください。 |
戻り値 | 文字列 |
生成された入力ID。 |
---|---|---|
例外発生 | 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 | 文字列 |
入力名 |
戻り値 | 文字列 |
生成された入力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 | 文字列 |
属性名または属性式 |
戻り値 | 文字列 |
生成された入力名 |
---|---|---|
例外発生 | 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 | 配列|文字列 |
画像URL。このパラメータはyii\helpers\Url::to()によって処理されます。 |
$options | 配列 |
名前と値のペアで表されるタグオプション。これらは、結果のタグの属性としてレンダリングされます。値はencode() を使用してHTMLエンコードされます。値がnullの場合、対応する属性はレンダリングされません。属性のレンダリング方法の詳細については、renderTagAttributes() を参照してください。 バージョン2.0.12以降、 |
戻り値 | 文字列 |
生成された画像タグ。 |
---|
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 | 文字列|null |
name属性。nullの場合、name属性は生成されません。 |
$value | 文字列|null |
value属性。nullの場合、value属性は生成されません。 |
$options | 配列 |
名前と値のペアで表されるタグオプション。これらは、結果のタグの属性としてレンダリングされます。値はencode() を使用してHTMLエンコードされます。値がnullの場合、対応する属性はレンダリングされません。属性のレンダリング方法の詳細については、renderTagAttributes() を参照してください。 |
戻り値 | 文字列 |
生成される入力タグ |
---|
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ファイルを参照するスクリプトタグを生成します。
public static string jsFile ( $url, $options = [] ) | ||
$url | 文字列 |
外部JavaScriptファイルのURL。このパラメータはyii\helpers\Url::to()によって処理されます。 |
$options | 配列 |
名前と値のペアで指定されたタグオプション。以下のオプションは特別に処理されます。
残りのオプションは、結果のscriptタグの属性としてレンダリングされます。値はencode()を使用してHTMLエンコードされます。値がnullの場合、対応する属性はレンダリングされません。renderTagAttributes()で、属性のレンダリング方法の詳細を参照してください。 |
戻り値 | 文字列 |
生成された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コードを渡すことができます。エンドユーザーからのものである場合は、XSS攻撃を防ぐためにencode()する必要があります。 |
$for | 文字列|null |
このラベルが関連付けられているHTML要素のID。nullの場合、「for」属性は生成されません。 |
$options | 配列 |
名前と値のペアで表されるタグオプション。これらは、結果のタグの属性としてレンダリングされます。値はencode() を使用してHTMLエンコードされます。値がnullの場合、対応する属性はレンダリングされません。属性のレンダリング方法の詳細については、renderTagAttributes() を参照してください。 |
戻り値 | 文字列 |
生成されたラベルタグ |
---|
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()で、属性のレンダリング方法の詳細を参照してください。 |
戻り値 | 文字列 |
生成されたリストボックスタグ |
---|
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コードを渡すことができます。エンドユーザーからの入力を受け取る場合は、XSS攻撃を防ぐためにencode() を検討する必要があります。 |
文字列|null |
メールアドレス。nullの場合、最初の引数(リンク本文)がメールアドレスとして扱われ、使用されます。 |
|
$options | 配列 |
名前と値のペアで表されるタグオプション。これらは、結果のタグの属性としてレンダリングされます。値はencode() を使用してHTMLエンコードされます。値がnullの場合、対応する属性はレンダリングされません。属性のレンダリング方法の詳細については、renderTagAttributes() を参照してください。 |
戻り値 | 文字列 |
生成された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 |
リストを生成するためのアイテム。各アイテムは、1つのリストアイテムを生成します。`$options['encode']`が設定されていないか、trueの場合、アイテムは自動的にHTMLエンコードされます。 |
$options | 配列 |
ラジオボタンリストのオプション(名前 => 設定)。以下のオプションがサポートされています。
属性のレンダリング方法の詳細については、renderTagAttributes()を参照してください。 |
戻り値 | 文字列 |
生成された順序付きリスト。`$items`が空の場合、空文字列が返されます。 |
---|
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 | 文字列|null |
value属性。nullの場合、value属性は生成されません。 |
$options | 配列 |
名前と値のペアで表されるタグオプション。これらは、結果のタグの属性としてレンダリングされます。値はencode() を使用してHTMLエンコードされます。値がnullの場合、対応する属性はレンダリングされません。属性のレンダリング方法の詳細については、renderTagAttributes() を参照してください。 |
戻り値 | 文字列 |
生成されたパスワード入力タグ。 |
---|
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() で、受け入れられる属性の詳細を確認してください。 |
戻り値 | 文字列 |
生成されたラジオボタンタグ |
---|
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()を参照してください。 |
戻り値 | 文字列 |
生成されたラジオボタンリスト |
---|
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 | 文字列|配列 |
削除する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);
}
}
}
}
指定されたCSSスタイルをHTMLオプションから削除します。
例:
Html::removeCssStyle($options, ['width', 'height']);
参照: addCssStyle().
public static void removeCssStyle ( &$options, $properties ) | ||
$options | 配列 |
変更するHTMLオプション。 |
$properties | 文字列|配列 |
削除する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() 呼び出しに渡される $options パラメータ。このメソッドは、存在する場合、「prompt」、「options」、および「groups」の要素を除外します。これらの要素の詳細については、dropDownList()を参照してください。 |
戻り値 | 文字列 |
生成されたリストオプション |
---|
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エンコードされます。 |
戻り値 | 文字列 |
レンダリング結果。属性が空でない場合、先頭に空白文字が付いた文字列としてレンダリングされます(タグ名に直接追加できるようにするため)。属性がない場合は、空文字列が返されます。 |
---|
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コードを渡すことができます。これがエンドユーザーからのものである場合、XSS攻撃を防ぐために encode() することを検討する必要があります。 |
$options | 配列 |
名前と値のペアで表されるタグオプション。これらは、結果のタグの属性としてレンダリングされます。値はencode() を使用してHTMLエンコードされます。値がnullの場合、対応する属性はレンダリングされません。属性のレンダリング方法の詳細については、renderTagAttributes() を参照してください。 |
戻り値 | 文字列 |
生成されたリセットボタンタグ |
---|
public static function resetButton($content = 'Reset', $options = [])
{
$options['type'] = 'reset';
return static::button($content, $options);
}
リセット入力ボタンを生成します。
public static string resetInput ( $label = 'Reset', $options = [] ) | ||
$label | 文字列|null |
value属性。nullの場合、value属性は生成されません。 |
$options | 配列 |
ボタンタグの属性。encode()を使用してHTMLエンコードされます。値がnullの属性は無視され、返されるタグには含まれません。renderTagAttributes()で属性のレンダリング方法の詳細を参照してください。 |
戻り値 | 文字列 |
生成されたボタンタグ |
---|
public static function resetInput($label = 'Reset', $options = [])
{
$options['type'] = 'reset';
$options['value'] = $label;
return static::tag('input', '', $options);
}
スクリプトタグを生成します。
public static string script ( $content, $options = [] ) | ||
$content | 文字列 |
スクリプトの内容 |
$options | 配列 |
名前と値のペアで表されるタグオプション。これらは、結果のタグの属性としてレンダリングされます。値はencode() を使用してHTMLエンコードされます。値がnullの場合、対応する属性はレンダリングされません。属性のレンダリング方法の詳細については、renderTagAttributes() を参照してください。 |
戻り値 | 文字列 |
生成された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);
}
}
スタイルタグを生成します。
public static string style ( $content, $options = [] ) | ||
$content | 文字列 |
スタイルの内容 |
$options | 配列 |
名前と値のペアで表されるタグオプション。これらは、結果のタグの属性としてレンダリングされます。値はencode() を使用してHTMLエンコードされます。値がnullの場合、対応する属性はレンダリングされません。属性のレンダリング方法の詳細については、renderTagAttributes() を参照してください。 |
戻り値 | 文字列 |
生成されたスタイルタグ |
---|
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コードを渡すことができます。これがエンドユーザーからのものである場合、XSS攻撃を防ぐために encode() することを検討する必要があります。 |
$options | 配列 |
名前と値のペアで表されるタグオプション。これらは、結果のタグの属性としてレンダリングされます。値はencode() を使用してHTMLエンコードされます。値がnullの場合、対応する属性はレンダリングされません。属性のレンダリング方法の詳細については、renderTagAttributes() を参照してください。 |
戻り値 | 文字列 |
生成された送信ボタンタグ |
---|
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 | 文字列|null |
value属性。nullの場合、value属性は生成されません。 |
$options | 配列 |
名前と値のペアで表されるタグオプション。これらは、結果のタグの属性としてレンダリングされます。値はencode() を使用してHTMLエンコードされます。値がnullの場合、対応する属性はレンダリングされません。属性のレンダリング方法の詳細については、renderTagAttributes() を参照してください。 |
戻り値 | 文字列 |
生成されたボタンタグ |
---|
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 | 文字列|ブール値|null |
タグ名。$nameが |
$content | 文字列 |
開始タグと終了タグの間に囲まれるコンテンツ。HTMLエンコードされません。エンドユーザーからの入力である場合は、XSS攻撃を防ぐためにencode() を検討してください。 |
$options | 配列 |
名前と値のペアで表されるHTMLタグ属性(HTMLオプション)。これらは、結果のタグの属性としてレンダリングされます。encode() を使用して値はHTMLエンコードされます。値がnullの場合、対応する属性はレンダリングされません。 たとえば、 属性のレンダリング方法の詳細については、renderTagAttributes()を参照してください。 |
戻り値 | 文字列 |
生成された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 | 文字列|null |
value属性。nullの場合、value属性は生成されません。 |
$options | 配列 |
名前と値のペアで表されるタグオプション。これらは、結果のタグの属性としてレンダリングされます。値はencode() を使用してHTMLエンコードされます。値がnullの場合、対応する属性はレンダリングされません。属性のレンダリング方法の詳細については、renderTagAttributes() を参照してください。 |
戻り値 | 文字列 |
生成されたテキスト入力タグ |
---|
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() を参照して、属性のレンダリング方法の詳細を確認してください。以下の特別なオプションが認識されます。
|
戻り値 | 文字列 |
生成されたテキストエリアタグ |
---|
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 |
リストを生成するためのアイテム。各アイテムは、1つのリストアイテムを生成します。`$options['encode']`が設定されていないか、trueの場合、アイテムは自動的にHTMLエンコードされます。 |
$options | 配列 |
ラジオボタンリストのオプション(名前 => 設定)。以下のオプションがサポートされています。
属性のレンダリング方法の詳細については、renderTagAttributes()を参照してください。 |
戻り値 | 文字列 |
生成された順序なしリスト。 |
---|
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
);
}
サインアップ または ログイン してコメントしてください。