# DetailView

> DetailView displays the detail of a single data $model.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [widgets](/docs/api/yii/widgets/index.md) › DetailView

| class | `yii\widgets\DetailView` |
| --- | --- |
| Extends | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) » [`yii\base\Component`](/docs/api/yii/base/component/index.md) » [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`yii\base\ViewContextInterface`](/docs/api/yii/base/viewcontextinterface/index.md), [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Since | 2.0 |
| Source | [`framework/widgets/DetailView.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/widgets/DetailView.php#L53) |

DetailView displays the detail of a single data [`$model`](/docs/api/yii/widgets/detailview/index.md#property-model).

DetailView is best used for displaying a model in a regular format (e.g. each model attribute
is displayed as a row in a table.) The model can be either an instance of [`Model`](/docs/api/yii/base/model/index.md)
or an associative array.

DetailView uses the [`$attributes`](/docs/api/yii/widgets/detailview/index.md#property-attributes) property to determines which model attributes
should be displayed and how they should be formatted.

A typical usage of DetailView is as follows:

```
echo DetailView::widget([
    'model' => $model,
    'attributes' => [
        'title',               // title attribute (in plain text)
        'description:html',    // description attribute in HTML
        [                      // the owner name of the model
            'label' => 'Owner',
            'value' => $model->owner->name,
        ],
        'created_at:datetime', // creation date formatted as datetime
    ],
]);
```

For more details and usage information on DetailView, see the [guide article on data widgets](/docs/guide/output-data-widgets/index.md).

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| [`$attributes`](#property-attributes) | `array` | A list of attributes to be displayed in the detail view. Default: `null`. | `DetailView` |
| <span id="property-autoIdPrefix"></span>[`$autoIdPrefix`](/docs/api/yii/base/widget/index.md#property-autoIdPrefix) static | `string` | The prefix to the automatically generated widget IDs. Default: `'w'`. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="property-behaviors"></span>[`$behaviors`](/docs/api/yii/base/component/index.md#property-behaviors) read-only | `array<`[`yii\base\Behavior`](/docs/api/yii/base/behavior/index.md)`<static>>` | List of behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`$formatter`](#property-formatter) | `array\|`[`yii\i18n\Formatter`](/docs/api/yii/i18n/formatter/index.md)`\|null` | The formatter used to format model attribute values into displayable texts. Default: `null`. | `DetailView` |
| <span id="property-id"></span>[`$id`](/docs/api/yii/base/widget/index.md#property-id) | `string` | Id of the widget. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| [`$model`](#property-model) | `array\|object` | The data model whose details are to be displayed. Default: `null`. | `DetailView` |
| [`$options`](#property-options) | `array\|null` | The HTML attributes for the container tag of this widget. Default: `['class' => 'detail-view']`. | `DetailView` |
| [`$template`](#property-template) | `string\|callable` | The template used to render a single attribute. Default: `'<tr><th{captionOptions}>{label}</th><td{contentOptions}>{value}</td></tr>'`. | `DetailView` |
| <span id="property-view"></span>[`$view`](/docs/api/yii/base/widget/index.md#property-view) | [`yii\web\View`](/docs/api/yii/web/view/index.md) | The view object that can be used to render views or view files. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="property-viewPath"></span>[`$viewPath`](/docs/api/yii/base/widget/index.md#property-viewPath) read-only | `string` | The directory containing the view files for this widget. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| <span id="method-__construct"></span>[`__construct()`](/docs/api/yii/base/baseobject/index.md#method-__construct) | Constructor. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/component/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__clone"></span>[`__clone()`](/docs/api/yii/base/component/index.md#method-__clone) | This method is called after the object is created by cloning an existing one. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/component/index.md#method-__get) | Returns the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/component/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/component/index.md#method-__set) | Sets the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/component/index.md#method-__unset) | Sets a component property to be null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-afterRun"></span>[`afterRun()`](/docs/api/yii/base/widget/index.md#method-afterRun) | This method is invoked right after a widget is executed. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="method-attachBehavior"></span>[`attachBehavior()`](/docs/api/yii/base/component/index.md#method-attachBehavior) | Attaches a behavior to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-attachBehaviors"></span>[`attachBehaviors()`](/docs/api/yii/base/component/index.md#method-attachBehaviors) | Attaches a list of behaviors to the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-beforeRun"></span>[`beforeRun()`](/docs/api/yii/base/widget/index.md#method-beforeRun) | This method is invoked right before the widget is executed. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="method-begin"></span>[`begin()`](/docs/api/yii/base/widget/index.md#method-begin) static | Begins a widget. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="method-behaviors"></span>[`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) | Returns a list of behaviors that this component should behave as. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/component/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/component/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-detachBehavior"></span>[`detachBehavior()`](/docs/api/yii/base/component/index.md#method-detachBehavior) | Detaches a behavior from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-detachBehaviors"></span>[`detachBehaviors()`](/docs/api/yii/base/component/index.md#method-detachBehaviors) | Detaches all behaviors from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-end"></span>[`end()`](/docs/api/yii/base/widget/index.md#method-end) static | Ends a widget. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="method-ensureBehaviors"></span>[`ensureBehaviors()`](/docs/api/yii/base/component/index.md#method-ensureBehaviors) | Makes sure that the behaviors declared in [`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) are attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getBehavior"></span>[`getBehavior()`](/docs/api/yii/base/component/index.md#method-getBehavior) | Returns the named behavior object. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getBehaviors"></span>[`getBehaviors()`](/docs/api/yii/base/component/index.md#method-getBehaviors) | Returns all behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getId"></span>[`getId()`](/docs/api/yii/base/widget/index.md#method-getId) | Returns the ID of the widget. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="method-getView"></span>[`getView()`](/docs/api/yii/base/widget/index.md#method-getView) | Returns the view object that can be used to render views or view files. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="method-getViewPath"></span>[`getViewPath()`](/docs/api/yii/base/widget/index.md#method-getViewPath) | Returns the directory containing the view files for this widget. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="method-hasEventHandlers"></span>[`hasEventHandlers()`](/docs/api/yii/base/component/index.md#method-hasEventHandlers) | Returns a value indicating whether there is any handler attached to the named event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/component/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/component/index.md#method-hasProperty) | Returns a value indicating whether a property is defined for this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`init()`](#method-init) | Initializes the detail view. | `DetailView` |
| <span id="method-off"></span>[`off()`](/docs/api/yii/base/component/index.md#method-off) | Detaches an existing event handler from this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-on"></span>[`on()`](/docs/api/yii/base/component/index.md#method-on) | Attaches an event handler to an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-render"></span>[`render()`](/docs/api/yii/base/widget/index.md#method-render) | Renders a view. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="method-renderFile"></span>[`renderFile()`](/docs/api/yii/base/widget/index.md#method-renderFile) | Renders a view file. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| [`run()`](#method-run) | Renders the detail view. | `DetailView` |
| <span id="method-setId"></span>[`setId()`](/docs/api/yii/base/widget/index.md#method-setId) | Sets the ID of the widget. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="method-setView"></span>[`setView()`](/docs/api/yii/base/widget/index.md#method-setView) | Sets the view object to be used by this widget. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="method-trigger"></span>[`trigger()`](/docs/api/yii/base/component/index.md#method-trigger) | Triggers an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-widget"></span>[`widget()`](/docs/api/yii/base/widget/index.md#method-widget) static | Creates a widget instance and runs it. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| [`normalizeAttributes()`](#method-normalizeAttributes) protected | Normalizes the attribute specifications. | `DetailView` |
| [`renderAttribute()`](#method-renderAttribute) protected | Renders a single attribute. | `DetailView` |

## Events

| Event | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="event-EVENT_INIT"></span>[`EVENT_INIT`](/docs/api/yii/base/widget/index.md#event-EVENT_INIT) | [`yii\base\Event`](/docs/api/yii/base/event/index.md) | An event that is triggered when the widget is initialized via [`init()`](/docs/api/yii/base/widget/index.md#method-init). Event name: `'init'`. Available since 2.0.11. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="event-EVENT_BEFORE_RUN"></span>[`EVENT_BEFORE_RUN`](/docs/api/yii/base/widget/index.md#event-EVENT_BEFORE_RUN) | [`yii\base\WidgetEvent`](/docs/api/yii/base/widgetevent/index.md) | An event raised right before executing a widget. Event name: `'beforeRun'`. Available since 2.0.11. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |
| <span id="event-EVENT_AFTER_RUN"></span>[`EVENT_AFTER_RUN`](/docs/api/yii/base/widget/index.md#event-EVENT_AFTER_RUN) | [`yii\base\WidgetEvent`](/docs/api/yii/base/widgetevent/index.md) | An event raised right after executing a widget. Event name: `'afterRun'`. Available since 2.0.11. | [`yii\base\Widget`](/docs/api/yii/base/widget/index.md) |

## Property details

### $attributes <span id="property-attributes"></span>

```php
public array $attributes = null
```

A list of attributes to be displayed in the detail view.

Each array element
represents the specification for displaying one particular attribute.

An attribute can be specified as a string in the format of `attribute`, `attribute:format` or `attribute:format:label`,
where `attribute` refers to the attribute name, and `format` represents the format of the attribute. The `format`
is passed to the [`Formatter::format()`](/docs/api/yii/i18n/formatter/index.md#method-format) method to format an attribute value into a displayable text.
Please refer to [`Formatter`](/docs/api/yii/i18n/formatter/index.md) for the supported types. Both `format` and `label` are optional.
They will take default values if absent.

An attribute can also be specified in terms of an array with the following elements:

- `attribute`: the attribute name. This is required if either `label` or `value` is not specified.
- `label`: the label associated with the attribute. If this is not specified, it will be generated from the attribute name.
- `value`: the value to be displayed. If this is not specified, it will be retrieved from [`$model`](/docs/api/yii/widgets/detailview/index.md#property-model) using the attribute name
  by calling [`ArrayHelper::getValue()`](/docs/api/yii/helpers/basearrayhelper/index.md#method-getValue). Note that this value will be formatted into a displayable text
  according to the `format` option. Since version 2.0.11 it can be defined as closure with the following
  parameters:

  ```
  function ($model, $widget)
  ```

  `$model` refers to displayed model and `$widget` is an instance of `DetailView` widget.

- `format`: the type of the value that determines how the value would be formatted into a displayable text.
  Please refer to [`Formatter`](/docs/api/yii/i18n/formatter/index.md) for supported types and [`Formatter::format()`](/docs/api/yii/i18n/formatter/index.md#method-format) on how to specify this value.
- `visible`: whether the attribute is visible. If set to `false`, the attribute will NOT be displayed.
- `contentOptions`: the HTML attributes to customize value tag. For example: `['class' => 'bg-red']`.
  Please refer to [`BaseHtml::renderTagAttributes()`](/docs/api/yii/helpers/basehtml/index.md#method-renderTagAttributes) for the supported syntax.
- `captionOptions`: the HTML attributes to customize label tag. For example: `['class' => 'bg-red']`.
  Please refer to [`BaseHtml::renderTagAttributes()`](/docs/api/yii/helpers/basehtml/index.md#method-renderTagAttributes) for the supported syntax.

### $formatter <span id="property-formatter"></span>

```php
public array|yii\i18n\Formatter|null $formatter = null
```

The formatter used to format model attribute values into displayable texts.

This can be either an instance of [`Formatter`](/docs/api/yii/i18n/formatter/index.md) or an configuration array for creating the [`Formatter`](/docs/api/yii/i18n/formatter/index.md)
instance. If this property is not set, the `formatter` application component will be used.

### $model <span id="property-model"></span>

```php
public array|object $model = null
```

The data model whose details are to be displayed.

This can be a [`Model`](/docs/api/yii/base/model/index.md) instance,
an associative array, an object that implements [`Arrayable`](/docs/api/yii/base/arrayable/index.md) interface or simply an object with defined
public accessible non-static properties.

### $options <span id="property-options"></span>

```php
public array|null $options = ['class' => 'detail-view']
```

The HTML attributes for the container tag of this widget.

The `tag` option specifies
what container tag should be used. It defaults to `table` if not set.

### $template <span id="property-template"></span>

```php
public string|callable $template = '<tr><th{captionOptions}>{label}</th><td{contentOptions}>{value}</td></tr>'
```

The template used to render a single attribute.

If a string, the token `{label}`
and `{value}` will be replaced with the label and the value of the corresponding attribute.
If a callback (e.g. an anonymous function), the signature must be as follows:

```
function ($attribute, $index, $widget)
```

where `$attribute` refer to the specification of the attribute being rendered, `$index` is the zero-based
index of the attribute in the [`$attributes`](/docs/api/yii/widgets/detailview/index.md#property-attributes) array, and `$widget` refers to this widget instance.

Since Version 2.0.10, the tokens `{captionOptions}` and `{contentOptions}` are available, which will represent
HTML attributes of HTML container elements for the label and value.

## Method details

### init() <span id="method-init"></span>

```php
public function init(): mixed
```

Initializes the detail view.

This method will initialize required property values.

**Returns**

`mixed`

### run() <span id="method-run"></span>

```php
public function run(): mixed
```

Renders the detail view.

This is the main entry of the whole detail view rendering.

**Returns**

`mixed`

### normalizeAttributes() <span id="method-normalizeAttributes"></span>

```php
protected function normalizeAttributes(): mixed
```

Normalizes the attribute specifications.

**Returns**

`mixed`

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md)

### renderAttribute() <span id="method-renderAttribute"></span>

```php
protected function renderAttribute(array $attribute, int $index): string
```

Renders a single attribute.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$attribute` | `array` | The specification of the attribute to be rendered. |
| `$index` | `int` | The zero-based index of the attribute in the [`$attributes`](/docs/api/yii/widgets/detailview/index.md#property-attributes) array |

**Returns**

`string`: The rendering result