- Issue created by @pdureau
- Merge request !9Issue #3497483 by pdureau: Clean variant logic in templates → (Merged) created by pdureau
Many components have variants:
Maybe there was a time in the early UI Patterns 1.x when lowering the variant name was useful, but it is not anymore:
$ grep -r variant.*lower components/*/*.twig
components/banner/banner.twig:{% if variant and variant|lower != 'default' %}
components/banner/banner.twig: {% set attributes = attributes.addClass('mdc-banner--' ~ variant|lower|replace({_: '-'})) %}
components/button/button.twig:{% if variant and variant|lower != 'default' %}
components/button/button.twig: {% set attributes = attributes.addClass('mdc-button--' ~ variant|lower|replace({_: '-'})) %}
components/card/card.twig:{% if variant and variant|lower != 'default' %}
components/card/card.twig: {% set attributes = attributes.addClass('mdc-card--' ~ variant|lower|replace({_: '-'})) %}
components/chip/chip.twig:{% if variant and variant|lower != 'default' %}
components/chip/chip.twig: {% set attributes = attributes.addClass('mdc-chip--' ~ variant|lower|replace({_: '-'})) %}
components/chip/chip.twig: {% if variant|lower == 'touch' %}
components/chip/chip.twig: {% if variant|lower == 'selected' %}
components/chip/chip.twig: {% if icon and variant|lower != 'selected' %}
components/chip_set/chip_set.twig:{% if variant and variant|lower != 'default' %}
components/chip_set/chip_set.twig: {% set attributes = attributes.addClass('mdc-chip-set--' ~ variant|lower|replace({_: '-'})) %}
components/drawer/drawer.twig:{% if variant and variant|lower != 'default' %}
components/drawer/drawer.twig: {% set attributes = attributes.addClass('mdc-drawer--' ~ variant|lower|replace({_: '-'})) %}
components/fab/fab.twig:{% if variant and variant|lower != 'default' %}
components/fab/fab.twig: {% set variants = variant|split('__')|map(v => v|lower|replace({(v): 'mdc-fab--' ~ v})|replace({_: '-'})) %}
components/image_list/image_list.twig:{% if variant and variant|lower != 'default' %}
components/image_list/image_list.twig: {% set attributes = attributes.addClass('mdc-image-list--' ~ variant|lower|replace({_: '-'})) %}
components/list_item/list_item.twig:{% if variant and variant|lower != 'default' %}
components/list_item/list_item.twig: {% set attributes = attributes.addClass('mdc-deprecated-list-item--' ~ variant|lower|replace({_: '-'})) %}
components/list/list.twig:{% if variant and variant|lower != 'default' %}
components/list/list.twig: {% set attributes = attributes.addClass('mdc-deprecated-list--' ~ variant|lower|replace({_: '-'})) %}
components/slider/slider.twig:{% if variant and variant|lower != 'default' %}
components/slider/slider.twig: {% set variants = variants|map(v => v|lower|replace({(v): 'mdc-slider--' ~ v})|replace({_: '-'})) %}
components/snackbar/snackbar.twig:{% if variant and variant|lower != 'default' %}
components/snackbar/snackbar.twig: {% set attributes = attributes.addClass('mdc-snackbar--' ~ variant|lower|replace({_: '-'})) %}
components/tab/tab.twig:{% if variant and variant|lower != 'default' %}
components/tab/tab.twig: {% set attributes = attributes.addClass('mdc-tab--' ~ variant|lower|replace({_: '-'})) %}
components/top_app_bar/top_app_bar.twig:{% if variant and variant|lower != 'default' %}
components/top_app_bar/top_app_bar.twig: {% set attributes = attributes.addClass('mdc-top-app-bar--' ~ variant|lower|replace({_: '-'})) %}
Remove the filter.
Only 2 components have variants with "__"
$ grep -r __ components/*/*.yml
components/fab/fab.component.yml: mini__touch:
components/slider/slider.component.yml: discrete__tick_marks:
components/slider/slider.component.yml: range__discrete:
components/slider/slider.component.yml: range__discrete__tick_marks:
But 3 are manipulating it:
$ grep -r variant.*__ components/*/*.twig
components/chip_set/chip_set.twig: {% set variants = variant|split('__') %}
components/fab/fab.twig: {% set variants = variant|split('__')|map(v => v|lower|replace({(v): 'mdc-fab--' ~ v})|replace({_: '-'})) %}
components/slider/slider.twig: {% set variants = variant|split('__') %}
Remove the logic from chip_set
$ grep -r variant.*replace components/*/*.twig
components/banner/banner.twig: {% set attributes = attributes.addClass('mdc-banner--' ~ variant|lower|replace({_: '-'})) %}
components/button/button.twig: {% set attributes = attributes.addClass('mdc-button--' ~ variant|lower|replace({_: '-'})) %}
components/card/card.twig: {% set attributes = attributes.addClass('mdc-card--' ~ variant|lower|replace({_: '-'})) %}
components/chip/chip.twig: {% set attributes = attributes.addClass('mdc-chip--' ~ variant|lower|replace({_: '-'})) %}
components/chip_set/chip_set.twig: {% set attributes = attributes.addClass('mdc-chip-set--' ~ variant|lower|replace({_: '-'})) %}
components/drawer/drawer.twig: {% set attributes = attributes.addClass('mdc-drawer--' ~ variant|lower|replace({_: '-'})) %}
components/fab/fab.twig: {% set variants = variant|split('__')|map(v => v|lower|replace({(v): 'mdc-fab--' ~ v})|replace({_: '-'})) %}
components/image_list/image_list.twig: {% set attributes = attributes.addClass('mdc-image-list--' ~ variant|lower|replace({_: '-'})) %}
components/list_item/list_item.twig: {% set attributes = attributes.addClass('mdc-deprecated-list-item--' ~ variant|lower|replace({_: '-'})) %}
components/list/list.twig: {% set attributes = attributes.addClass('mdc-deprecated-list--' ~ variant|lower|replace({_: '-'})) %}
components/slider/slider.twig: {% set variants = variants|map(v => v|lower|replace({(v): 'mdc-slider--' ~ v})|replace({_: '-'})) %}
components/snackbar/snackbar.twig: {% set attributes = attributes.addClass('mdc-snackbar--' ~ variant|lower|replace({_: '-'})) %}
components/tab/tab.twig: {% set attributes = attributes.addClass('mdc-tab--' ~ variant|lower|replace({_: '-'})) %}
components/top_app_bar/top_app_bar.twig: {% set attributes = attributes.addClass('mdc-top-app-bar--' ~ variant|lower|replace({_: '-'})) %}
But those components doesn't have "_" (single underscore) in variant ID:
Remove replace filter
Active
2.0
Code