# DynamicContentAwareTrait

> DynamicContentAwareTrait implements common methods for classes which support a yii\base\View dynamic content feature.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [base](/docs/api/yii/base/index.md) › DynamicContentAwareTrait

| trait | `yii\base\DynamicContentAwareTrait` |
| --- | --- |
| Since | 2.0.14 |
| Source | [`framework/base/DynamicContentAwareTrait.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/base/DynamicContentAwareTrait.php#L18) |

Used by 2 types:

- [`yii\filters\PageCache`](/docs/api/yii/filters/pagecache/index.md)
- [`yii\widgets\FragmentCache`](/docs/api/yii/widgets/fragmentcache/index.md)

DynamicContentAwareTrait implements common methods for classes
which support a [`View`](/docs/api/yii/base/view/index.md) dynamic content feature.

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`addDynamicPlaceholder()`](#method-addDynamicPlaceholder) | Adds a placeholder for dynamic content. | `DynamicContentAwareTrait` |
| [`getDynamicPlaceholders()`](#method-getDynamicPlaceholders) | Returns a list of placeholders for dynamic content. This method is used internally to implement the content caching feature. | `DynamicContentAwareTrait` |
| [`setDynamicPlaceholders()`](#method-setDynamicPlaceholders) | Sets a list of placeholders for dynamic content. This method is used internally to implement the content caching feature. | `DynamicContentAwareTrait` |
| [`getView()`](#method-getView) protected abstract | Returns the view object that can be used to render views or view files using dynamic contents. | `DynamicContentAwareTrait` |
| [`updateDynamicContent()`](#method-updateDynamicContent) protected | Replaces placeholders in $content with results of evaluated dynamic statements. | `DynamicContentAwareTrait` |

## Method details

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

```php
public function addDynamicPlaceholder(mixed $name, mixed $statements): mixed
```

Adds a placeholder for dynamic content.

This method is used internally to implement the content caching feature.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `mixed` | The placeholder name. |
| `$statements` | `mixed` | The PHP statements for generating the dynamic content. |

**Returns**

`mixed`

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

```php
public function getDynamicPlaceholders(): array
```

Returns a list of placeholders for dynamic content. This method
is used internally to implement the content caching feature.

**Returns**

`array`: A list of placeholders.

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

```php
public function setDynamicPlaceholders(mixed $placeholders): mixed
```

Sets a list of placeholders for dynamic content. This method
is used internally to implement the content caching feature.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$placeholders` | `mixed` | A list of placeholders. |

**Returns**

`mixed`

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

```php
abstract protected function getView(): yii\base\View
```

Returns the view object that can be used to render views or view files using dynamic contents.

**Returns**

[`View`](/docs/api/yii/base/view/index.md): The view object that can be used to render views or view files.

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

```php
protected function updateDynamicContent(string $content, string[] $placeholders, bool $isRestoredFromCache = false): string
```

Replaces placeholders in $content with results of evaluated dynamic statements.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$content` | `string` | Content to be parsed. |
| `$placeholders` | `string[]` | Placeholders and their values. |
| `$isRestoredFromCache` | `bool` | Whether content is going to be restored from cache. |

**Returns**

`string`: Final content.