Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnathonKoster committed Nov 11, 2023
0 parents commit dc7d607
Show file tree
Hide file tree
Showing 34 changed files with 13,962 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github: JohnathonKoster
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
vendor
mix-manifest.json
.idea
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) <Johnathon Koster>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
221 changes: 221 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
# Attribute Renderer for Statamic

Attribute Renderer is a utility addon that helps create HTML attribute strings from arrays.

At a high level, it allows you to convert something like this:

```php
<?php

use function Stillat\StatamicAttributeRenderer\attributes;

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

to the following HTML attribute string:

```html
name="author" content="John Doe"
```

The attributes renderer is also context aware, and can do things like this:

```php
<?php

use function Stillat\StatamicAttributeRenderer\attributes;

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

These examples are basic, and Attribute Renderer supports even more complex scenarios.

## How to Install

Attribute Renderer can be installed by running the following command from the root of your project:

``` bash
composer require stillat/statamic-attribute-renderer
```

## Converting Arrays to Attribute Strings

The simplest way to convert a key/value array of attribute details to a string is using the `attributes` utility function:

```php
<?php

use function Stillat\StatamicAttributeRenderer\attributes;

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

which produces the following result:

```html
name="author" content="John Doe"
```

## Resolving Variable Values

We can resolve variables from contextual data, which is supplied as the second argument to the `attributes` function. When specifying variable names, we simply prefix them with the `$` symbol:

```php
<?php

use function Stillat\StatamicAttributeRenderer\attributes;

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

We can use `$$` to escape the beginning of a variable string to emit string beginning with a single `$`:

```php
<?php

use function Stillat\StatamicAttributeRenderer\attributes;

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

which produces the following output:

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

Attribute Renderer does not support more complicated variable paths, such as nested properties, or array indices. If you need something more complicated, consider using a closure based variable resolver.

## Closure Based Variable Resolvers

We can supply a `Closure` as the value of our attribute in order to resolve more complicated values. We will receive the context array as the first argument:

```php
<?php

use function Stillat\StatamicAttributeRenderer\attributes;

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

which produces:

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

## Skippable/Ignorable Properties

By default, Attribute Renderer will emit empty strings if a value returns `null`:

```php
<?php

use function Stillat\StatamicAttributeRenderer\attributes;

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

produces:

```html
name="author" content=""
```

we can let Attribute Renderer know its okay to ignore a property when producing the final result:

```php
<?php

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

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

would now produce:

```html
name="author"
```

However, if the value did exist within the context:

```php
<?php

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

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

the ignorable property is added to the output:

```html
name="author" content="John Doe"
```

## Rejectable Properties

Rejectable properties are similar to ignorable properties. However, if a `null` or empty string value is resolved for one of these values, an empty attribute string is returned, regardless of if other property values were matched:

```php
<?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'
]);
```

## License

Attribute Renderer is free software, released under the MIT license.
40 changes: 40 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "stillat/statamic-attribute-renderer",
"autoload": {
"psr-4": {
"Stillat\\StatamicAttributeRenderer\\": "src"
},
"files": [
"src/helpers.php"
]
},
"autoload-dev": {
"psr-4": { "Tests\\": "tests/" }
},
"require": {
"php": "^8.1",
"statamic/cms": "^4"
},
"require-dev": {
"pestphp/pest": "^2.24",
"orchestra/testbench": "^8.14",
"pestphp/pest-plugin-laravel": "^2.2"
},
"extra": {
"statamic": {
"name": "Attribute Renderer for Statamic",
"description": "A simple utility addon for Statamic that provides a simple way to render HTML attributes, with advanced variable handling features."
},
"laravel": {
"providers": [
"Stillat\\StatamicAttributeRenderer\\ServiceProvider"
]
}
},
"config": {
"allow-plugins": {
"pestphp/pest-plugin": true,
"pixelfear/composer-dist-plugin": true
}
}
}
Loading

0 comments on commit dc7d607

Please sign in to comment.