lmc-eu / spirit-web-twig-bundle

[READ-ONLY] Spirit Design System Web Twig components
0 stars 0 forks source link

Twig filter classes/classnames #1

Open literat opened 2 months ago

literat commented 2 months ago

I want to have the same functionality as classes/classnames like the npm library for React so I can easily send an array to the class prop.

new TwigFilter('classes', [$this, 'stringifyHtmlClasses']),

/**
 * @param string[] $classes
 */
public function stringifyHtmlClasses(array $classes): string
{
   return implode(' ', $classes);
}
<Text
  size="large"
  class="{{
     [
         'text-secondary',
         _person.description ? 'mb-1' : 'mb-0'
     ]|classes
   }}"
>
  {{- _person.role -}}
</Text>
adamkudrna commented 2 months ago

The join filter wouldn't work for you?

    [
         'text-secondary',
         _person.description ? 'mb-1' : 'mb-0'
     ] | join(' ')

The join filter is basically an implementation of PHP's implode().