mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-03 13:58:55 +08:00
@ -162,10 +162,10 @@ especifica directamente.
|
||||
Los métodos más genéricos son:
|
||||
|
||||
```php
|
||||
// type, input name, input value, options
|
||||
type, input name, input value, options
|
||||
<?= Html::input('text', 'username', $user->name, ['class' => $username]) ?>
|
||||
|
||||
// type, model, model attribute name, options
|
||||
type, model, model attribute name, options
|
||||
<?= Html::activeInput('text', $user, 'name', ['class' => $username]) ?>
|
||||
```
|
||||
|
||||
@ -282,13 +282,13 @@ Existen dos métodos para generar etiquetas que envuelvan estilos y scripts incr
|
||||
```php
|
||||
<?= Html::style('.danger { color: #f00; }') ?>
|
||||
|
||||
// Genera
|
||||
Genera
|
||||
|
||||
<style>.danger { color: #f00; }</style>
|
||||
|
||||
<?= Html::script('alert("Hello!");', ['defer' => true]);
|
||||
|
||||
// Genera
|
||||
Genera
|
||||
|
||||
<script defer>alert("Hello!");</script>
|
||||
```
|
||||
@ -298,7 +298,7 @@ Si se quiere enlazar un estilo externo desde un archivo CSS:
|
||||
```php
|
||||
<?= Html::cssFile('@web/css/ie5.css', ['condition' => 'IE 5']) ?>
|
||||
|
||||
// genera
|
||||
genera
|
||||
|
||||
<!--[if IE 5]>
|
||||
<link href="http://example.com/css/ie5.css" />
|
||||
@ -351,7 +351,7 @@ Para generar una etiqueta de tipo imagen se puede usar el siguiente ejemplo:
|
||||
```php
|
||||
<?= Html::img('@web/images/logo.png', ['alt' => 'My logo']) ?>
|
||||
|
||||
// genera
|
||||
genera
|
||||
|
||||
<img src="http://example.com/images/logo.png" alt="My logo" />
|
||||
```
|
||||
|
||||
@ -9,6 +9,7 @@ of Html helper which provides a set of static methods for handling commonly used
|
||||
> Note: If your markup is nearly static it's better to use HTML directly. There's no need to wrap absolutely everything
|
||||
with Html helper calls.
|
||||
|
||||
|
||||
Basics <a name="basics"></a>
|
||||
----------------------------
|
||||
|
||||
@ -39,7 +40,7 @@ In case you need just start tag or just closing tag you can use `Html::beginTag(
|
||||
|
||||
Options are used in many methods of Html helper and various widgets. In all these cases there is some extra handling to
|
||||
know about:
|
||||
|
||||
|
||||
- If a value is null, the corresponding attribute will not be rendered.
|
||||
- Attributes whose values are of boolean type will be treated as
|
||||
[boolean attributes](http://www.w3.org/TR/html5/infrastructure.html#boolean-attributes).
|
||||
@ -49,13 +50,12 @@ know about:
|
||||
- The "data" attribute could receive JSON. It is handled the same way as array i.e.
|
||||
`'data' => ['params' => ['id' => 1, 'name' => 'yii'], 'status' => 'ok']` becomes
|
||||
`data-params='{"id":1,"name":"yii"}' data-status="ok"`.
|
||||
|
||||
|
||||
### Forming class and style dynamically
|
||||
|
||||
When building options for HTML tag we're often starting with defaults which we need to modify. In order to add or
|
||||
remove CSS class you can use the following:
|
||||
|
||||
|
||||
```php
|
||||
$options = ['class' => 'btn btn-default'];
|
||||
|
||||
@ -88,7 +88,7 @@ could be converted there and forth by using [[yii\helpers\Html::cssStyleFromArra
|
||||
[[yii\helpers\Html::cssStyleToArray()|cssStyleToArray()]]. The [[yii\helpers\Html::removeCssStyle()|removeCssStyle()]]
|
||||
method accepts an array of properties to remove. If it's going to be a single property it could be specified as string.
|
||||
|
||||
|
||||
|
||||
Encoding and decoding content <a name="encoding-and-decoding-content"></a>
|
||||
--------------------------------------------------------------------------
|
||||
|
||||
@ -104,7 +104,7 @@ $userName = Html::encode($user->name);
|
||||
echo $userName;
|
||||
|
||||
$decodedUserName = Html::decode($userName);
|
||||
```
|
||||
```
|
||||
|
||||
|
||||
Forms
|
||||
@ -115,6 +115,7 @@ dealing with them.
|
||||
|
||||
> Note: consider using [[yii\widgets\ActiveForm|ActiveForm]] in case you deal with models and need validation.
|
||||
|
||||
|
||||
### Open and close a form
|
||||
|
||||
Form could be opened with [[yii\helpers\Html::beginForm()|beginForm()]] method like the following:
|
||||
@ -134,6 +135,7 @@ Closing form tag is simple:
|
||||
<?= Html::endForm() ?>
|
||||
```
|
||||
|
||||
|
||||
### Buttons
|
||||
|
||||
In order to generate buttons you can use the following code:
|
||||
@ -154,7 +156,6 @@ There are two groups on input methods. The ones starting with `active` and calle
|
||||
with it. Active inputs are taking data from model and attribute specified while in case of regular input data is specified
|
||||
directly.
|
||||
|
||||
|
||||
The most generic methods are:
|
||||
|
||||
```php
|
||||
@ -198,7 +199,7 @@ Dropdown list and list box could be rendered like the following:
|
||||
|
||||
First argument is the name of the input, second is the value that's currently selected and third is key-value pairs where
|
||||
array key is list value and array value is list label.
|
||||
|
||||
|
||||
If you want multiple choices to be selectable, checkbox list is a good match:
|
||||
|
||||
```php
|
||||
@ -213,9 +214,8 @@ If not, use radio list:
|
||||
<?= Html::activeRadioList($user, 'role', ArrayHelper::map($roleModels, 'id', 'name')) ?>
|
||||
```
|
||||
|
||||
|
||||
### Labels and errors
|
||||
|
||||
### Labels and errors
|
||||
|
||||
Same as inputs there are two methods for generating form labels. Active that's taking data from the model and non-active
|
||||
that accepts data directly:
|
||||
@ -224,7 +224,7 @@ that accepts data directly:
|
||||
<?= Html::label('User name', 'username', ['class' => 'label username']) ?>
|
||||
<?= Html::activeLabel($user, 'username', ['class' => 'label username'])
|
||||
```
|
||||
|
||||
|
||||
In order to display form errors from a model or models as a summary you could use:
|
||||
|
||||
```php
|
||||
@ -236,10 +236,10 @@ To display individual error:
|
||||
```php
|
||||
<?= Html::error($post, 'title', ['class' => 'error']) ?>
|
||||
```
|
||||
|
||||
|
||||
|
||||
### Names and values
|
||||
|
||||
|
||||
There are methods to get names, ids and values for input fields based on the model. These are mainly used internally
|
||||
but could be handy sometimes:
|
||||
|
||||
@ -271,14 +271,13 @@ In order to get attribute name without suffixes or prefixes one can use the foll
|
||||
// dates
|
||||
echo Html::getAttributeName('dates[0]');
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
Styles and scripts
|
||||
------------------
|
||||
|
||||
There two methods to generate tags wrapping embedded styles and scripts:
|
||||
|
||||
|
||||
```php
|
||||
<?= Html::style('.danger { color: #f00; }') ?>
|
||||
|
||||
@ -321,12 +320,13 @@ To link JavaScript file:
|
||||
|
||||
Same as with CSS first argument specifies link to the file to be included. Options could be passed as the second argument.
|
||||
In options you can specify `condition` in the same way as in options for `cssFile`.
|
||||
|
||||
|
||||
|
||||
Links
|
||||
-----
|
||||
|
||||
There's a method to generate hyperlink conveniently:
|
||||
|
||||
|
||||
```php
|
||||
<?= Html::a('Profile', ['user/view', 'id' => $id], ['class' => 'profile-link']) ?>
|
||||
```
|
||||
@ -336,17 +336,17 @@ The first argument is the title. It's not encoded so if you're using data got fr
|
||||
what values it accepts. Third argument is array of tag properties.
|
||||
|
||||
In you need to generate `mailto` link you can use the following code:
|
||||
|
||||
|
||||
```php
|
||||
<?= Html::mailto('Contact us', 'admin@example.com') ?>
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
Images
|
||||
------
|
||||
|
||||
In order to generate image tag use the following:
|
||||
|
||||
|
||||
```php
|
||||
<?= Html::img('@web/images/logo.png', ['alt' => 'My logo']) ?>
|
||||
|
||||
@ -357,10 +357,11 @@ generates
|
||||
|
||||
Aside [aliases](concept-aliases.md) the first argument can accept routes, parameters and URLs. Same way as
|
||||
[Url::to()](helper-url.md) does.
|
||||
|
||||
|
||||
|
||||
Lists
|
||||
-----
|
||||
|
||||
|
||||
Unordered list could be generated like the following:
|
||||
|
||||
```php
|
||||
|
||||
Reference in New Issue
Block a user