Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added method ComponentAttributeBag::has() for view-engine. #3123

Merged
merged 5 commits into from
Jan 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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