Skip to content

Commit

Permalink
Added method ComponentAttributeBag::has() for view-engine. (#3123)
Browse files Browse the repository at this point in the history
* feat: Add has() method to Hyperf\ViewEngine\Component\ComponentAttributeBag

* docs: update view-engine.md

* Update CHANGELOG-2.1.md

Co-authored-by: 李铭昕 <715557344@qq.com>
  • Loading branch information
nfangxu and limingxinleo committed Jan 14, 2021
1 parent eab49ac commit c9a6f5a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG-2.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
## Added

- [#3059](https://github.com/hyperf/hyperf/pull/3059) The merged attributes in the view component support attributes other than 'class'.
- [#3123](https://github.com/hyperf/hyperf/pull/3123) Added method `ComponentAttributeBag::has()` for `view-engine`.

# v2.1.2 - 2021-01-11

Expand Down
24 changes: 23 additions & 1 deletion docs/zh-cn/view-engine.md
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,29 @@ class ConfigProvider
</div>
```

#### 默认 / 合并属性
#### 获取属性

您可以使用 `get()` 方法获取特定的属性值。此方法接受属性名称作为第一个参数(第二个参数为默认值),并将返回其值。

```html
<div class="{{ $attributes->get("class", "default") }}">
<!-- 组件内容 -->
</div>
```

#### 检测属性

您可以使用 `has()` 方法获取特定的属性值。此方法接受属性名称作为参数,并将返回布尔值。

```html
@if($attributes->has("class"))
<div class="{{ $attributes->get("class") }}">
<!-- 组件内容 -->
</div>
@endif
```

#### 合并属性

某些时候,你可能需要指定属性的默认值,或将其他值合并到组件的某些属性中。为此,你可以使用属性包的 `merge` 方法:

Expand Down
8 changes: 8 additions & 0 deletions src/view-engine/src/Component/ComponentAttributeBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,14 @@ public function setAttributes(array $attributes)
$this->attributes = $attributes;
}

/**
* Determine if a given attribute exists in the attribute array.
*/
public function has(string $key): bool
{
return array_key_exists($key, $this->attributes);
}

/**
* Get content as a string of HTML.
*
Expand Down

0 comments on commit c9a6f5a

Please sign in to comment.