Skip to content

Allow backed enum in HTML attributes extension #4848

Description

@laurentmuller

When converting values, it will be nice to allow backed enumeration.

Enumeration:

namespace App\Enums;

enum TableView: string
{
    case CARD = 'card';
    case TABLE = 'table';
}

Template:

<div {{ html_attr({'data-enum': enum('App\\Enums\\TableView').CARD}) }}>
    Content
</div>

In htmlAttr function:

public static function htmlAttr(Environment $env, iterable|string|false|null ...$args): string
{
    // ...
    foreach ($attr as $name => $value) {
        // ... 
        if ($value instanceof \BackedEnum) {
            $value = $value.value;
        }
        // ...
    }
}

Output:

<div data-enum="card">
    Content
</div>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions