stillat/statamic-attribute-renderer

此包的最新版本(v1.1.0)没有可用的许可信息。

v1.1.0 2024-05-11 20:02 UTC

This package is auto-updated.

Last update: 2024-09-11 20:42:10 UTC


README

属性渲染器是一个实用工具插件,可以帮助您从数组创建HTML属性字符串。

从高层次来看,它允许您将如下内容

<?php

use function Stillat\StatamicAttributeRenderer\attributes;

attributes([
    'name' => 'author',
    'content' => 'John Doe'
]);

转换为以下HTML属性字符串

name="author" content="John Doe"

属性渲染器也具有上下文感知能力,可以执行如下操作

<?php

use function Stillat\StatamicAttributeRenderer\attributes;

attributes([
    'name' => 'author',
    'content' => '$author'
], [
    'author' => 'John Doe'
]);

这些示例很简单,属性渲染器还支持更多复杂的情况。

如何安装

您可以从项目的根目录运行以下命令来安装属性渲染器

composer require stillat/statamic-attribute-renderer

将数组转换为属性字符串

将属性详情的键/值数组转换为字符串的最简单方法是使用 attributes 工具函数

<?php

use function Stillat\StatamicAttributeRenderer\attributes;

attributes([
    'name' => 'author',
    'content' => 'John Doe'
]);

它产生以下结果

name="author" content="John Doe"

解析变量值

我们可以从上下文数据中解析变量,该数据作为 attributes 函数的第二个参数提供。当指定变量名称时,我们只需在它们前面加上 $ 符号

<?php

use function Stillat\StatamicAttributeRenderer\attributes;

attributes([
    'name' => 'author',
    'content' => '$author'
], [
    'author' => 'John Doe'
]);

我们可以使用 $$ 来转义变量字符串的开始,以便输出以单个 $ 开头的字符串

<?php

use function Stillat\StatamicAttributeRenderer\attributes;

attributes([
    'name' => 'author',
    'content' => '$author',
    'content_two' => '$$author',
    'content_three' => '$$$author',
], [
    'author' => 'John Doe'
]);

它产生以下输出

name="author" content="John Doe" content_two="$author" content_three="$$author"

属性渲染器不支持更复杂的变量路径,例如嵌套属性或数组索引。如果您需要更复杂的功能,请考虑使用基于闭包的变量解析器。

基于闭包的变量解析器

我们可以将 Closure 作为属性值提供,以便解析更复杂的值。我们将接收到上下文数组作为第一个参数

<?php

use function Stillat\StatamicAttributeRenderer\attributes;

attributes([
    'name' => 'author',
    'content' => function (array $context) {
        return 'Hello, '.$context['author'];
    },
], [
    'author' => 'John Doe'
]);

它产生

name="author" content="Hello, John Doe"

可跳过的/可忽略的属性

默认情况下,如果值返回 null,属性渲染器将发出空字符串

<?php

use function Stillat\StatamicAttributeRenderer\attributes;

attributes([
    'name' => 'author',
    'content' => '$name'
]);

它产生

name="author" content=""

我们可以让属性渲染器知道,在生成最终结果时可以忽略一个属性

<?php

use function Stillat\StatamicAttributeRenderer\attributes;
use function Stillat\StatamicAttributeRenderer\isIgnorable;

attributes([
    'name' => 'author',
    'content' => isIgnorable('$name')
]);

现在将产生

name="author"

然而,如果值确实存在于上下文中

<?php

use function Stillat\StatamicAttributeRenderer\attributes;
use function Stillat\StatamicAttributeRenderer\isIgnorable;

attributes([
    'name' => 'author',
    'content' => isIgnorable('$name')
], [
    'name' => 'John Doe'
]);

可忽略的属性将添加到输出中

name="author" content="John Doe"

可拒绝的属性

可拒绝的属性与可忽略的属性类似。但是,如果为这些值之一解析出 null 或空字符串值,则返回空属性字符串,无论是否匹配其他属性值

<?php

use function Stillat\StatamicAttributeRenderer\attributes;
use function Stillat\StatamicAttributeRenderer\rejectsOnEmpty;

attributes([
    'name' => 'author',
    'content' => rejectsOnEmpty('$name'),
    'first_name' => '$first_name'
], [
    'first_name' => 'John Doe'
]);

许可

属性渲染器是免费软件,在MIT许可下发布。