From 6c9068198558c3185739296aa7eddf05c95f552a Mon Sep 17 00:00:00 2001 From: Florent Torregrosa <florent.torregrosa@gmail.com> Date: Mon, 6 Jan 2025 10:35:18 +0100 Subject: [PATCH] Issue #3497493 by pdureau, grimreaper: Clean variant logic in templates --- components/accordion/accordion.twig | 4 ++-- components/alert/alert.twig | 4 ++-- components/button/button.twig | 4 ++-- components/button_group/button_group.twig | 6 +++--- components/card/card.twig | 2 +- components/carousel/carousel.twig | 6 +++--- components/close_button/close_button.twig | 2 +- components/list/list.twig | 4 ++-- components/list_group/list_group.twig | 4 ++-- components/list_group_item/list_group_item.twig | 4 ++-- components/modal/modal.twig | 5 ++--- components/nav/nav.twig | 4 ++-- components/navbar/navbar.twig | 6 +++--- components/navbar_nav/navbar_nav.twig | 4 ++-- components/offcanvas/offcanvas.twig | 2 +- components/pagination/pagination.twig | 4 ++-- components/progress/progress.twig | 4 ++-- components/spinner/spinner.twig | 2 +- components/table/table.twig | 14 +++++++------- components/table_cell/table_cell.twig | 2 +- components/table_row/table_row.twig | 2 +- 21 files changed, 44 insertions(+), 45 deletions(-) diff --git a/components/accordion/accordion.twig b/components/accordion/accordion.twig index 4e8b4a5d..3456454d 100644 --- a/components/accordion/accordion.twig +++ b/components/accordion/accordion.twig @@ -1,5 +1,5 @@ -{% if variant and variant|lower != 'default' %} - {% set attributes = attributes.addClass('accordion-' ~ variant|lower|replace({'_': '-'})) %} +{% if variant and variant != 'default' %} + {% set attributes = attributes.addClass('accordion-' ~ variant) %} {% endif %} {% set accordion_id = accordion_id|default("accordion-" ~ random()) %} {% set keep_open = keep_open ? 'true' : 'false' %} diff --git a/components/alert/alert.twig b/components/alert/alert.twig index 857a3ae2..4855cc6e 100644 --- a/components/alert/alert.twig +++ b/components/alert/alert.twig @@ -1,5 +1,5 @@ -{% if variant and variant|lower != 'default' %} - {% set attributes = attributes.addClass('alert-' ~ variant|lower|replace({'_': '-'})) %} +{% if variant and variant != 'default' %} + {% set attributes = attributes.addClass('alert-' ~ variant) %} {% endif %} {% if dismissible %} diff --git a/components/button/button.twig b/components/button/button.twig index 15ec8e44..55e7488e 100644 --- a/components/button/button.twig +++ b/components/button/button.twig @@ -1,5 +1,5 @@ -{% if variant and variant|lower != 'default' %} - {% set variants = variant|split('__')|map(v => v|lower|replace({(v): 'btn-' ~ v})|replace({'_': '-'})) %} +{% if variant and variant != 'default' %} + {% set variants = variant|split('__')|map(v => v|replace({(v): 'btn-' ~ v})|replace({'_': '-'})) %} {% set attributes = attributes.addClass(variants) %} {% set attributes = attributes.addClass('btn') %} {% endif %} diff --git a/components/button_group/button_group.twig b/components/button_group/button_group.twig index d88c731f..d0113207 100644 --- a/components/button_group/button_group.twig +++ b/components/button_group/button_group.twig @@ -1,8 +1,8 @@ -{% if variant and variant|lower != 'default' %} - {% set attributes = attributes.addClass('btn-group-' ~ variant|lower|replace({'_': '-'})) %} +{% if variant and variant != 'default' %} + {% set attributes = attributes.addClass('btn-group-' ~ variant) %} {% endif %} -{% if variant and variant|lower != 'vertical' %} +{% if variant and variant != 'vertical' %} {% set attributes = attributes.addClass('btn-group') %} {% endif %} diff --git a/components/card/card.twig b/components/card/card.twig index b3745b12..ba8af5d9 100644 --- a/components/card/card.twig +++ b/components/card/card.twig @@ -1,4 +1,4 @@ -{% if variant and variant|lower == 'horizontal' %} +{% if variant and variant == 'horizontal' %} {% set row_attributes = create_attribute(row_attributes|default({})) %} {% set image_col_attributes = create_attribute(image_col_attributes|default({})) %} {% set content_col_attributes = create_attribute(content_col_attributes|default({})) %} diff --git a/components/carousel/carousel.twig b/components/carousel/carousel.twig index cab173c6..c1dae6d2 100644 --- a/components/carousel/carousel.twig +++ b/components/carousel/carousel.twig @@ -1,8 +1,8 @@ -{% if variant and variant|lower != 'default' %} - {% set variants = variant|split('__')|map(v => v|lower|replace({(v): 'carousel-' ~ v})|replace({'_': '-'})) %} +{% if variant and variant != 'default' %} + {% set variants = variant|split('__')|map(v => v|replace({(v): 'carousel-' ~ v})) %} {% set attributes = attributes.addClass(variants) %} {% endif %} -{% set attributes = 'dark' in variant|lower ? attributes.setAttribute('data-bs-theme', 'dark') : attributes %} +{% set attributes = 'dark' in variant ? attributes.setAttribute('data-bs-theme', 'dark') : attributes %} {% if not with_touch %} {% set attributes = attributes.setAttribute('data-bs-touch', 'false') %} diff --git a/components/close_button/close_button.twig b/components/close_button/close_button.twig index d1350276..65c4f599 100644 --- a/components/close_button/close_button.twig +++ b/components/close_button/close_button.twig @@ -1,4 +1,4 @@ -{% if variant and variant|lower != 'default' %} +{% if variant and variant != 'default' %} {% set attributes = attributes.addClass('btn-close-' ~ variant) %} {% endif %} {% set attributes = attributes.addClass('btn-close') %} diff --git a/components/list/list.twig b/components/list/list.twig index 303d2a30..a3c62e15 100644 --- a/components/list/list.twig +++ b/components/list/list.twig @@ -1,5 +1,5 @@ -{% if variant and variant|lower != 'default' %} - {% set attributes = attributes.addClass('list-' ~ variant|lower|replace({'_': '-'})) %} +{% if variant and variant != 'default' %} + {% set attributes = attributes.addClass('list-' ~ variant) %} {% endif %} {% set list_type = list_type|default('ul') %} diff --git a/components/list_group/list_group.twig b/components/list_group/list_group.twig index feaa53e9..d37459cb 100644 --- a/components/list_group/list_group.twig +++ b/components/list_group/list_group.twig @@ -1,5 +1,5 @@ -{% if variant and variant|lower != 'default' %} - {% set variants = variant|split('__')|map(v => v|lower|replace({(v): 'list-group-' ~ v})|replace({'_': '-'})) %} +{% if variant and variant != 'default' %} + {% set variants = variant|split('__')|map(v => v|replace({(v): 'list-group-' ~ v})|replace({'_': '-'})) %} {% set attributes = attributes.addClass(variants) %} {% endif %} diff --git a/components/list_group_item/list_group_item.twig b/components/list_group_item/list_group_item.twig index 082d026b..fa5c2257 100644 --- a/components/list_group_item/list_group_item.twig +++ b/components/list_group_item/list_group_item.twig @@ -1,5 +1,5 @@ -{% if variant and variant|lower != 'default' %} - {% set attributes = attributes.addClass('list-group-item-' ~ variant|lower|replace({'_': '-'})) %} +{% if variant and variant != 'default' %} + {% set attributes = attributes.addClass('list-group-item-' ~ variant) %} {% endif %} {% set attributes = attributes.addClass('list-group-item') %} diff --git a/components/modal/modal.twig b/components/modal/modal.twig index 1f44db42..dd68d280 100644 --- a/components/modal/modal.twig +++ b/components/modal/modal.twig @@ -1,7 +1,6 @@ {% set dialog_attributes = create_attribute({'class': 'modal-dialog'}) %} -{% if variant and variant|lower != 'default' %} - {% set variants = variant|split('__')|map(v => v|lower|replace({(v): 'modal-' ~ v})|replace({'_': '-'})) %} - {% set dialog_attributes = dialog_attributes.addClass(variants) %} +{% if variant and variant != 'default' %} + {% set dialog_attributes = dialog_attributes.addClass('modal-' ~ variant) %} {% endif %} {% set modal_id = modal_id|default("modal-" ~ random()) %} diff --git a/components/nav/nav.twig b/components/nav/nav.twig index aa9500d1..5333f0ca 100644 --- a/components/nav/nav.twig +++ b/components/nav/nav.twig @@ -1,5 +1,5 @@ -{% if variant and variant|lower != 'default' %} - {% set variants = variant|split('__')|map(v => v|lower|replace({(v): 'nav-' ~ v})|replace({'_': '-'})) %} +{% if variant and variant != 'default' %} + {% set variants = variant|split('__')|map(v => v|replace({(v): 'nav-' ~ v})) %} {% set attributes = attributes.addClass(variants) %} {% set attributes = card_header and 'tabs' in variant ? attributes.addClass('card-header-tabs') : attributes %} {% set attributes = card_header and 'pills' in variant ? attributes.addClass('card-header-pills') : attributes %} diff --git a/components/navbar/navbar.twig b/components/navbar/navbar.twig index 81012fcf..d16f711e 100644 --- a/components/navbar/navbar.twig +++ b/components/navbar/navbar.twig @@ -1,10 +1,10 @@ -{% if variant and variant|lower != 'default' %} - {% set variants = variant|split('__')|map(v => v|lower|replace({(v): 'navbar-' ~ v})|replace({'_': '-'})) %} +{% if variant and variant != 'default' %} + {% set variants = variant|split('__')|map(v => v|replace({(v): 'navbar-' ~ v})|replace({'_': '-'})) %} {% set attributes = attributes.addClass(variants) %} {% endif %} {% set attributes = attributes.addClass('navbar') %} -{% set attributes = 'dark' in variant|lower ? attributes.setAttribute('data-bs-theme', 'dark') : attributes %} +{% set attributes = 'dark' in variant ? attributes.setAttribute('data-bs-theme', 'dark') : attributes %} {% set navbar_id = navbar_id|default("navbar-" ~ random()) %} {% set placement = placement|default('default') %} diff --git a/components/navbar_nav/navbar_nav.twig b/components/navbar_nav/navbar_nav.twig index cd73e2dc..64b9ce00 100644 --- a/components/navbar_nav/navbar_nav.twig +++ b/components/navbar_nav/navbar_nav.twig @@ -1,5 +1,5 @@ -{% if variant and variant|lower != 'default' %} - {% set attributes = attributes.addClass('navbar-nav-' ~ variant|lower|replace({'_': '-'})) %} +{% if variant and variant != 'default' %} + {% set attributes = attributes.addClass('navbar-nav-' ~ variant) %} {% endif %} {% set attributes = attributes.addClass('navbar-nav') %} diff --git a/components/offcanvas/offcanvas.twig b/components/offcanvas/offcanvas.twig index 654d8ebc..dcacac29 100644 --- a/components/offcanvas/offcanvas.twig +++ b/components/offcanvas/offcanvas.twig @@ -1,4 +1,4 @@ -{% set attributes = attributes.addClass('offcanvas-' ~ variant|default('start')|lower|replace({'_': '-'})) %} +{% set attributes = attributes.addClass('offcanvas-' ~ variant|default('start')) %} {% set offcanvas_id = offcanvas_id|default("offcanvas-" ~ random()) %} {% set heading_level = heading_level|default(5) %} diff --git a/components/pagination/pagination.twig b/components/pagination/pagination.twig index 76ed3162..896acdee 100644 --- a/components/pagination/pagination.twig +++ b/components/pagination/pagination.twig @@ -1,5 +1,5 @@ -{% if variant and variant|lower != 'default' %} - {% set attributes = attributes.addClass('pagination-' ~ variant|lower|replace({'_': '-'})) %} +{% if variant and variant != 'default' %} + {% set attributes = attributes.addClass('pagination-' ~ variant) %} {% endif %} {% if items %} diff --git a/components/progress/progress.twig b/components/progress/progress.twig index 80c59bf5..1f3495fa 100644 --- a/components/progress/progress.twig +++ b/components/progress/progress.twig @@ -1,5 +1,5 @@ -{% if variant and variant|lower != 'default' %} - {% set variants = variant|split('__')|map(v => v|lower|replace({(v): 'progress-bar-' ~ v})|replace({'_': '-'})) %} +{% if variant and variant != 'default' %} + {% set variants = variant|split('__')|map(v => v|replace({(v): 'progress-bar-' ~ v})) %} {% set attributes = attributes.addClass(variants) %} {% endif %} diff --git a/components/spinner/spinner.twig b/components/spinner/spinner.twig index 84da637c..faadc4aa 100644 --- a/components/spinner/spinner.twig +++ b/components/spinner/spinner.twig @@ -1,5 +1,5 @@ {% if variant %} - {% set variants = variant|split('__')|map(v => v|lower|replace({(v): 'spinner-' ~ v})|replace({'_': '-'})) %} + {% set variants = variant|split('__')|map(v => v|replace({(v): 'spinner-' ~ v})|replace({'_': '-'})) %} {% set attributes = attributes.addClass(variants) %} {% endif %} diff --git a/components/table/table.twig b/components/table/table.twig index 234349ad..0e166237 100644 --- a/components/table/table.twig +++ b/components/table/table.twig @@ -1,22 +1,22 @@ {% set attributes = attributes.addClass('table') %} -{% if variant and variant|lower != 'default' %} - {% set variants = variant|split('__')|map(v => v|lower|replace({(v): 'table-' ~ v})|replace({'_': '-'})) %} +{% if variant and variant != 'default' %} + {% set variants = variant|split('__')|map(v => v|replace({(v): 'table-' ~ v})) %} {% set attributes = attributes.addClass(variants) %} {% endif %} {% if stripes %} {% for stripe in stripes %} - {% set attributes = attributes.addClass('table-' ~ stripe|lower|replace({'_': '-'})) %} + {% set attributes = attributes.addClass('table-' ~ stripe) %} {% endfor %} {% endif %} -{% set attributes = borders ? attributes.addClass('table-' ~ borders|lower|replace({'_': '-'})) : attributes %} +{% set attributes = borders ? attributes.addClass('table-' ~ borders) : attributes %} {% set attributes = hover ? attributes.addClass('table-hover') : attributes %} {% set attributes = caption_top ? attributes.addClass('caption-top') : attributes %} {% set colgroups = colgroups and colgroups is not sequence ? [colgroups] : colgroups %} {% if responsive %} -<div class="{{ 'table-' ~ responsive|lower|replace({'_': '-'}) }}"> +<div class="{{ 'table-' ~ responsive }}"> {% endif %} <table{{ attributes }}> @@ -40,7 +40,7 @@ {% if header %} {% set thead_attributes = create_attribute({ 'class': [ - header_color ? 'table-' ~ header_color|lower|replace({'_': '-'}), + header_color ? 'table-' ~ header_color, divider and 'thead' in divider ? 'table-group-divider', ], }) %} @@ -83,7 +83,7 @@ {% if footer %} {% set tfoot_attributes = create_attribute({ 'class': [ - footer_color ? 'table-' ~ footer_color|lower|replace({'_': '-'}), + footer_color ? 'table-' ~ footer_color, divider and 'tfoot' in divider ? 'table-group-divider', ], }) %} diff --git a/components/table_cell/table_cell.twig b/components/table_cell/table_cell.twig index 71e4c876..3416a060 100644 --- a/components/table_cell/table_cell.twig +++ b/components/table_cell/table_cell.twig @@ -1,6 +1,6 @@ {% set tag = tag|default('td') %} {% set attributes = active ? attributes.addClass('table-active') : attributes %} -{% set attributes = color ? attributes.addClass('table-' ~ color|lower|replace({'_': '-'})) : attributes %} +{% set attributes = color ? attributes.addClass('table-' ~ color) : attributes %} <{{ tag }}{{ attributes }}> {{- content -}} diff --git a/components/table_row/table_row.twig b/components/table_row/table_row.twig index 63c19b56..272d2fee 100644 --- a/components/table_row/table_row.twig +++ b/components/table_row/table_row.twig @@ -1,5 +1,5 @@ {% set attributes = active ? attributes.addClass('table-active') : attributes %} -{% set attributes = color ? attributes.addClass('table-' ~ color|lower|replace({'_': '-'})) : attributes %} +{% set attributes = color ? attributes.addClass('table-' ~ color) : attributes %} <tr{{ attributes }}> {{- cells -}} -- GitLab