8000 6.x string template by dereuromark · Pull Request #18846 · cakephp/cakephp · GitHub
[go: up one dir, main page]

Skip to content

Conversation

dereuromark
Copy link
Member

Part of https://github.com/cakephp/cakephp/wiki/6.0-Ideas TODOs

What do you think? Is this a general improvement in the API?

The codebase now uses the cleaner, more type-safe methods:

  • addClassToArray() for adding classes to attribute arrays
  • mergeClasses() for merging class arrays/strings

We could add this to 5.next as well, but since this is more internal, not sure if we need to.

@albertcansado
Copy link
Contributor

Hi,

Regarding the mergeClasses method, in my projects I’ve created an alternative inspired by the classnames library.

I’m sharing the code (excluding tests, but I can share it) and an example because I think it’s easier to understand what it does and it’s much more visual.

// The method name should obviously be changed
public function cx(...$args): string
{
    $output = [];
    $this->cxRecursive($output, $args);

    return trim(implode(' ', array_unique($output)));
}

private function cxRecursive(array &$output, array $args): void
{
    foreach ($args as $key => $value) {
        if (is_array($value)) {
            $this->cxRecursive($output, $value);
        } elseif (is_numeric($key) && is_string($value)) {
            $output[] = $value;
        } elseif ($value === true && is_string($key)) {
            $output[] = $key;
        }
    }
}

# Example

$this->Html->tag('div', 'mycontent', ['class' => $this->Html->cx('foo', ['my-class' => true, 'is-active' => $aBooleanVar]));

// <div class="foo my-class">mycontent</div>

# Usage in the addClassToArray method

public function addClassToArray(
    array $attributes,
    array|string $newClasses,
    string $key = 'class',
): array {
    return Hash::insert($attributes, $key, $this->cx($attributes[$key] ?? null, $newClasses));
}

I just wanted to share it in case you find it interesting or useful. Otherwise, feel free to disregard this, and thank you for your work.

@dereuromark dereuromark added the needs squashing The pull request should be squashed before merging label Aug 23, 2025
@markstory
Copy link
Member

Regarding the mergeClasses method, in my projects I’ve created an alternative inspired by the classnames library.

If we're considering new APIs for manipulating class names, I would prefer an API closer to classnames as well. It is a well used ergonomic solution for handling classnames in client side rendering, and could also be useful for server rendering.

@dereuromark
Copy link
Member Author
8955

Do you want to make an alternative PR? This one is just resolving the arg and return types for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cleanup deprecations needs squashing The pull request should be squashed before merging

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

0