From 58672c4d7ce5bbf32e409868a46e6b8097f23805 Mon Sep 17 00:00:00 2001 From: Kevin Papst Date: Mon, 9 Sep 2024 09:44:21 +0200 Subject: [PATCH] remove deprecated spaceless filter, more whitespace control for cleaner HTML --- templates/components/actions.html.twig | 130 ++++++++++++------------- templates/components/button.html.twig | 117 +++++++++++----------- templates/includes/utils.html.twig | 8 +- 3 files changed, 125 insertions(+), 130 deletions(-) diff --git a/templates/components/actions.html.twig b/templates/components/actions.html.twig index 7a48147..8629307 100644 --- a/templates/components/actions.html.twig +++ b/templates/components/actions.html.twig @@ -1,74 +1,72 @@ -{% macro actions(actions, options) %} - {% import '@Tabler/components/button.html.twig' as macro %} - {% if actions|length >= 1 %} - {% set class = 'btn-sm' %} - {% set translationDomain = 'messages' %} - {% if options.translationDomain is defined %} - {% set translationDomain = options.translationDomain %} - {% endif %} - {% if options.class is defined %} - {% set class = options.class %} - {% endif %} - {% set trash = null %} - {% set divider = false %} +{% macro actions(actions, options) -%} + {%- import '@Tabler/components/button.html.twig' as macro -%} + {%- if actions|length >= 1 -%} + {%- set class = 'btn-sm' -%} + {%- set translationDomain = 'messages' -%} + {%- if options.translationDomain is defined -%} + {%- set translationDomain = options.translationDomain -%} + {%- endif -%} + {%- if options.class is defined -%} + {%- set class = options.class -%} + {%- endif -%} + {%- set trash = null -%} + {%- set divider = false -%} - {% endif %} -{% endmacro %} + {%- endif -%} +{%- endmacro %} diff --git a/templates/components/button.html.twig b/templates/components/button.html.twig index 9601a7f..da9c038 100644 --- a/templates/components/button.html.twig +++ b/templates/components/button.html.twig @@ -1,67 +1,66 @@ {% macro button(icon, values, type) %} {% import '@Tabler/includes/utils.html.twig' as macros %} - {%- apply spaceless -%} - {% if type is same as (false) %} - {# when using in dropdown actions for example, which might not have a "btn" class #} - {% set class = "" %} - {% elseif type is null %} - {% set class = "btn " %} - {% else %} - {% set class = "btn btn-" ~ type ~ " " %} - {% endif %} + {%- if type is same as (false) -%} + {# when using in dropdown actions for example, which might not have a "btn" class #} + {%- set class = "" %} + {%- elseif type is null -%} + {%- set class = "btn " -%} + {%- else -%} + {%- set class = "btn btn-" ~ type ~ " " -%} + {%- endif -%} - {% set icon = values.icon ?? icon %} - {% set disabled = values.disabled ?? false %} - {% set url = values.url ?? '#' %} - {% set onclick = values.onclick ?? null %} - {% set collapse = values.collapse ?? null %} - {% set modal = values.modal ?? null %} - {% set toggle = values.toggle ?? null %} - {% set target = values.target ?? null %} - {% set id = values.id ?? null %} - {% set title = values.title ?? null %} - {% set class = class ~ ( values.class | default("")) %} - {% set attr = values.attr ?? {} %} - {% set forceTitle = icon is same as (false) or (values.combined ?? false) %} - {% set badge = values.badge ?? (values.label ?? null) %} - {% set badge_color = values.badgeColor ?? (values.labelColor ?? 'yellow') %} - {% set translation_domain = values.translation_domain ?? 'messages' %} - {% set translation_params = values.translation_params ?? {} %} - {% set isSubmit = values.buttonType is defined and values.buttonType == 'submit' %} - {% set buttonType = values.buttonType is defined and (values.buttonType == 'button' or values.buttonType == 'submit') ? 'button' : 'a' %} - {% set tooltip = values.tooltip ?? true %} - {% set accesskey = values.accesskey is defined ? values.accesskey : null %} + {%- set icon = values.icon ?? icon -%} + {%- set disabled = values.disabled ?? false -%} + {%- set url = values.url ?? '#' -%} + {%- set onclick = values.onclick ?? null -%} + {%- set collapse = values.collapse ?? null -%} + {%- set modal = values.modal ?? null -%} + {%- set toggle = values.toggle ?? null -%} + {%- set target = values.target ?? null -%} + {%- set id = values.id ?? null -%} + {%- set title = values.title ?? null -%} + {%- set class = class ~ ( values.class | default("")) -%} + {%- set attr = values.attr ?? {} -%} + {%- set forceTitle = icon is same as (false) or (values.combined ?? false) -%} + {%- set badge = values.badge ?? (values.label ?? null) -%} + {%- set badge_color = values.badgeColor ?? (values.labelColor ?? 'yellow') -%} + {%- set translation_domain = values.translation_domain ?? 'messages' -%} + {%- set translation_params = values.translation_params ?? {} -%} + {%- set isSubmit = values.buttonType is defined and values.buttonType == 'submit' -%} + {%- set buttonType = values.buttonType is defined and (values.buttonType == 'button' or values.buttonType == 'submit') ? 'button' : 'a' -%} + {%- set tooltip = values.tooltip ?? true -%} + {%- set accesskey = values.accesskey is defined ? values.accesskey : null -%} - {% if type != 'link' and icon is not same as (false) and (not forceTitle and badge is null) and (type is not same as (false)) %} - {% set class = class ~ ' btn-icon' %} - {% endif %} + {%- if type != 'link' and icon is not same as (false) and (not forceTitle and badge is null) and (type is not same as (false)) %} + {%- set class = class ~ ' btn-icon' -%} + {%- endif -%} - {% if disabled is same as (true) %} - {% set class = class ~ " disabled" %} - {% endif %} + {%- if disabled is same as (true) %} + {%- set class = class ~ " disabled" -%} + {%- endif -%} - <{{ buttonType|raw }} class="{{ class | trim }}" {% if buttonType is not same as ("button") %}href="{{ url }}"{% endif %} - {%- if disabled is same as (true) %} disabled="disabled"{% endif %} - {%- if id is not empty %} id="{{ id }}"{% endif %} - {%- if toggle is not empty %} data-bs-toggle="{{ toggle }}"{% endif %} - {%- if modal is not empty %} data-bs-toggle="modal" data-bs-target="{{ modal }}"{% endif %} - {%- if collapse is not empty %} data-bs-toggle="collapse" data-bs-target="{{ collapse }}"{% endif %} - {%- if onclick is not empty %} onclick="{{ onclick }}"{% endif %} - {%- if target is not empty %} {% if isSubmit %}formtarget{% else %}target{% endif %}="{{ target }}"{% endif %} - {%- if tooltip and not forceTitle and title is not null and type is not same as (false) %} {{ values.tooltip_attr ?? 'data-bs-toggle' }}="tooltip" data-bs-placement="top" title="{{ title|trans(translation_params, translation_domain) }}"{% endif %} - {%- if accesskey is not empty %} accesskey="{{ accesskey }}"{% endif %} - {{ macros.attr_to_html(attr) }} - > - {%- if icon is not same as (false) -%} - {{ tabler_icon(icon, ("btn-sm" not in class and "btn-lg" not in class), icon) }} - {% endif %} - {%- if (forceTitle or icon is same as (false)) and title is not null -%} - {{ title|trans(translation_params, translation_domain) }} - {% endif %} - {%- if badge is not null -%} - {{ badge }} - {%- endif -%} - - {% endapply %} + <{{ buttonType|raw }} class="{{ class | trim }}" + {%- if buttonType is not same as ("button") %} href="{{ url }}"{% endif -%} + {%- if disabled is same as (true) %} disabled="disabled"{% endif -%} + {%- if id is not empty %} id="{{ id }}"{% endif -%} + {%- if toggle is not empty %} data-bs-toggle="{{ toggle }}"{% endif -%} + {%- if modal is not empty %} data-bs-toggle="modal" data-bs-target="{{ modal }}"{% endif -%} + {%- if collapse is not empty %} data-bs-toggle="collapse" data-bs-target="{{ collapse }}"{% endif -%} + {%- if onclick is not empty %} onclick="{{ onclick }}"{% endif -%} + {%- if target is not empty %} {% if isSubmit %}formtarget{% else %}target{% endif -%}="{{ target }}"{% endif -%} + {%- if tooltip and not forceTitle and title is not null and type is not same as (false) %} {{ values.tooltip_attr ?? 'data-bs-toggle' }}="tooltip" data-bs-placement="top" title="{{ title|trans(translation_params, translation_domain) }}"{% endif -%} + {%- if accesskey is not empty %} accesskey="{{ accesskey }}"{% endif -%} + {{- macros.attr_to_html(attr) -}} + > + {%- if icon is not same as (false) -%} + {{ tabler_icon(icon, ("btn-sm" not in class and "btn-lg" not in class), icon) }} + {%- endif -%} + {%- if (forceTitle or icon is same as (false)) and title is not null -%} + {{ title|trans(translation_params, translation_domain) }} + {%- endif -%} + {%- if badge is not null -%} + {{ badge }} + {%- endif -%} + {% endmacro %} diff --git a/templates/includes/utils.html.twig b/templates/includes/utils.html.twig index 0a497d0..ed5f6c7 100644 --- a/templates/includes/utils.html.twig +++ b/templates/includes/utils.html.twig @@ -1,9 +1,7 @@ {# Twig used as macros holder only #} {% macro attr_to_html(attr) %} - {%- if attr is not empty %} - {% for name, value in attr %} - {{ name }}="{{ value|e('html_attr') }}" - {% endfor %} - {% endif %} + {%- if attr is not empty -%} + {% for name, value in attr %} {{ name }}="{{ value|e('html_attr') }}"{% endfor %} + {%- endif -%} {% endmacro %}