Skip to content

Commit

Permalink
remove deprecated spaceless filter, more whitespace control for clean…
Browse files Browse the repository at this point in the history
…er HTML
  • Loading branch information
kevinpapst committed Sep 9, 2024
1 parent 35a8aa0 commit 58672c4
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 130 deletions.
130 changes: 64 additions & 66 deletions templates/components/actions.html.twig
Original file line number Diff line number Diff line change
@@ -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 -%}
<div class="dropdown">
<a href="#" class="link-secondary {% if tabler_bundle.isDarkMode() %}btn-dark{% endif%} {{ class }}" data-bs-toggle="dropdown" data-bs-auto-close="outside" aria-expanded="false" aria-label="{{ 'Actions'|trans({}, 'TablerBundle') }}">
<i class="{{ options.icon|default('fas fa-ellipsis-h') }}"></i>
</a>
<div class="dropdown-menu dropdown-menu-end" data-popper-placement="bottom-end">
{%- apply spaceless -%}
{%- for icon,values in actions %}
{%- if 'divider' in icon and values is null %}
{% if not loop.last and divider is same as (false) %}
<hr class="dropdown-divider" />
{% endif %}
{% set divider = true %}
{% else %}
{% if values is iterable %}
{% if values['title'] is not defined %}
{% set values = values|merge({'title': icon|trans({}, translationDomain)}) %}
{% endif %}
{% else %}
{% set values = {'url': values, 'title': icon|trans({}, translationDomain)} %}
{% endif %}
{% if icon == 'trash' %}
{% set trash = values %}
{% else %}
{% if values.children is defined %}
{% if values.title is defined and values.title is not empty %}
<div class="dropend">
<a class="dropdown-item dropdown-toggle" href="#" data-bs-toggle="dropdown" data-bs-auto-close="outside" role="button" aria-expanded="false">
{{ values.title|trans }}
</a>
<div class="dropdown-menu">
{%- for childIcon, childValues in values.children %}
{{ macro.button(false, childValues|merge({icon: false, class: childValues.class is defined ? childValues.class ~ ' dropdown-item' : 'dropdown-item'}), false) }}
{% endfor %}
</div>
</div>
{% else %}
{% if divider is same as (false) %}<hr class="dropdown-divider" />{% endif %}
{%- for childIcon, childValues in values.children %}
{{ macro.button(false, childValues|merge({icon: false, class: childValues.class is defined ? childValues.class ~ ' dropdown-item' : 'dropdown-item'}), false) }}
{% endfor %}
<hr class="dropdown-divider" />
{% endif %}
{% set divider = true %}
{% else %}
{% set divider = false %}
{{ macro.button(false, values|merge({icon: false, class: values.class is defined ? values.class ~ ' dropdown-item' : 'dropdown-item'}), false) }}
{% endif %}
{% endif %}
{% endif -%}
{% endfor -%}
{%- if trash is not null %}
{%- if actions|length > 1 and divider is same as (false) -%}
{%- for icon,values in actions -%}
{%- if 'divider' in icon and values is null -%}
{%- if not loop.last and divider is same as (false) -%}
<hr class="dropdown-divider" />
{% endif %}
{{ macro.button(false, trash|merge({class: trash.class is defined ? trash.class ~ ' text-danger dropdown-item' : 'dropdown-item'}), false) }}
{% endif -%}
{% endapply %}
{%- endif -%}
{%- set divider = true -%}
{%- else -%}
{%- if values is iterable -%}
{%- if values['title'] is not defined -%}
{%- set values = values|merge({'title': icon|trans({}, translationDomain)}) -%}
{%- endif -%}
{%- else -%}
{%- set values = {'url': values, 'title': icon|trans({}, translationDomain)} -%}
{%- endif -%}
{%- if icon == 'trash' -%}
{%- set trash = values -%}
{%- else -%}
{%- if values.children is defined -%}
{%- if values.title is defined and values.title is not empty -%}
<div class="dropend">
<a class="dropdown-item dropdown-toggle" href="#" data-bs-toggle="dropdown" data-bs-auto-close="outside" role="button" aria-expanded="false">
{{ values.title|trans }}
</a>
<div class="dropdown-menu">
{%- for childIcon, childValues in values.children -%}
{{-- macro.button(false, childValues|merge({icon: false, class: childValues.class is defined ? childValues.class ~ ' dropdown-item' : 'dropdown-item'}), false) -}}
{%- endfor -%}
</div>
</div>
{%- else -%}
{%- if divider is same as (false) %}<hr class="dropdown-divider" />{% endif -%}
{%- for childIcon, childValues in values.children -%}
{{- macro.button(false, childValues|merge({icon: false, class: childValues.class is defined ? childValues.class ~ ' dropdown-item' : 'dropdown-item'}), false) -}}
{%- endfor -%}
<hr class="dropdown-divider" />
{%- endif -%}
{%- set divider = true -%}
{%- else -%}
{%- set divider = false -%}
{{- macro.button(false, values|merge({icon: false, class: values.class is defined ? values.class ~ ' dropdown-item' : 'dropdown-item'}), false) -}}
{%- endif -%}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{%- if trash is not null -%}
{%- if actions|length > 1 and divider is same as (false) -%}
<hr class="dropdown-divider" />
{%- endif -%}
{{- macro.button(false, trash|merge({class: trash.class is defined ? trash.class ~ ' text-danger dropdown-item' : 'dropdown-item'}), false) -}}
{%- endif -%}
</div>
</div>
{% endif %}
{% endmacro %}
{%- endif -%}
{%- endmacro %}
117 changes: 58 additions & 59 deletions templates/components/button.html.twig
Original file line number Diff line number Diff line change
@@ -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 -%}
<span class="badge bg-{{ badge_color }} {% if forceTitle and title is not null %}ms-1{% endif %}">{{ badge }}</span>
{%- endif -%}
</{{ buttonType|raw }}>
{% 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 -%}
<span class="badge bg-{{ badge_color }} {% if forceTitle and title is not null %}ms-1{% endif %}">{{ badge }}</span>
{%- endif -%}
</{{ buttonType|raw }}>
{% endmacro %}
8 changes: 3 additions & 5 deletions templates/includes/utils.html.twig
Original file line number Diff line number Diff line change
@@ -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 %}

0 comments on commit 58672c4

Please sign in to comment.