Skip to content

Commit

Permalink
BC: change argument order for icon() twig function (#69)
Browse files Browse the repository at this point in the history
* change argument order to simplify icon usage outside buttons
* do not use icon class for buttons by default and re-use own macros
  • Loading branch information
kevinpapst committed Mar 2, 2022
1 parent 8a70eaa commit 22b4660
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 26 deletions.
8 changes: 4 additions & 4 deletions src/Twig/RuntimeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,13 @@ public function getUserDetails(): ?UserDetailsEvent
return $userEvent;
}

public function createIcon(string $name, string $default = null, bool $isIcon = true): string
public function createIcon(string $name, bool $withIconClass = false, string $default = null): string
{
return '<i class="' . $this->icon($name, $default, $isIcon) . '"></i>';
return '<i class="' . $this->icon($name, $withIconClass, $default) . '"></i>';
}

public function icon(string $name, string $default = null, bool $isIcon = true): string
public function icon(string $name, bool $withIconClass = false, string $default = null): string
{
return ($isIcon ? 'icon ' : '') . ($this->icons[str_replace('-', '_', $name)] ?? ($default ?? $name));
return ($withIconClass ? 'icon ' : '') . ($this->icons[str_replace('-', '_', $name)] ?? ($default ?? $name));
}
}
2 changes: 1 addition & 1 deletion templates/components/alert.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<div class="d-flex">
{% if icon %}
<div>
<i class="alert-icon {{ icon|icon(icon, false) }}"></i>
<i class="alert-icon {{ icon|icon(icon) }}"></i>
</div>
{% endif %}
<div>
Expand Down
2 changes: 1 addition & 1 deletion templates/components/button.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
{% endfor %}
{% endif %}>
{%- if icon is not same as (false) -%}
{{ tabler_icon(icon) }}
{{ tabler_icon(icon, true, icon) }}
{% endif %}
{%- if (forceTitle or icon is same as (false)) and title is not null -%}
{{ title|trans({}, translation_domain) }}
Expand Down
12 changes: 2 additions & 10 deletions templates/components/buttons.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,12 @@
{{ _self.action_cardtoolbutton('collapse', {collapse: target, title: label|default('')}) }}
{% endmacro %}

{# TODO test me #}
{% macro link_toolbutton(icon, href, label, attrs) %}
{% set attrs = attrs|default({})|merge({class: 'me-2 ' ~ (attrs.class|default(''))}) %}
<a href="{{ href }}" {% for name, value in attrs|default({}) %} {{ name }}="{{ value }}"{% endfor %}>
{{ tabler_icon(icon) }}{% if label is not null %} {{ label }}{% endif %}
</a>
{% endmacro %}

{% macro link_button(label, href, icon, type, size) %}
{% set _size = size|default(null) %}
{% set _type = type|default('primary') %}
<a href="{{ href|default('#') }}" class="btn btn-{{ _type }}{% if _size %} btn-{{ _size }}{% endif %}">
{% if icon %}
{{ tabler_icon(icon) }}
{{ tabler_icon(icon, true) }}
{% endif %}
{% if label %}
<span>{{ label }}</span>
Expand All @@ -33,7 +25,7 @@
{% set _type = type|default('primary') %}
<button data-action="{{ action }}" class="btn btn-{{ _type }}{% if _size %} btn-{{ _size }}{% endif %}">
{% if icon %}
{{ tabler_icon(icon) }}
{{ tabler_icon(icon, true) }}
{% endif %}
{% if label %}
<span>{{ label }}</span>
Expand Down
6 changes: 2 additions & 4 deletions templates/error.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@
</p>
<div class="empty-action">
{% block error_actions %}
<a href="{{ path('tabler_welcome'|tabler_route) }}" class="btn btn-primary">
{{ tabler_icon('back') }}
{{ 'Take me home'|trans({}, 'TablerBundle') }}
</a>
{% from '@Tabler/components/buttons.html.twig' import link_button %}
{{ link_button(('Take me home'|trans({}, 'TablerBundle')), (path('tabler_welcome'|tabler_route)), 'back') }}
{% endblock %}
</div>
</div>
Expand Down
4 changes: 2 additions & 2 deletions templates/includes/menu.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
{%- endif -%}>
{% if item.icon %}
<span class="nav-link-icon d-md-none d-lg-inline-block text-center">
{{ tabler_icon(item.icon, null, false) }}
{{ tabler_icon(item.icon, false, item.icon) }}
</span>
{% endif %}
<span class="nav-link-title">{{ item.label|trans }}</span>
Expand All @@ -32,7 +32,7 @@
<a class="dropdown-item {{ child.isActive ? 'active':'' }}" href="{{ '/' in child.route ? child.route : path(child.route|tabler_route, child.routeArgs) }}">
{% if child.icon %}
<span class="nav-link-icon d-md-none d-lg-inline-block text-center">
{{ tabler_icon(child.icon, null, false) }}
{{ tabler_icon(child.icon, false, child.icon) }}
</span>
{% endif %}
{{ child.label|trans }}
Expand Down
6 changes: 2 additions & 4 deletions templates/security/password-reset.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@
<input type="text" id="username" name="username" required="required" class="form-control" placeholder="{{ 'Username or email address'|trans({}, 'TablerBundle') }}">
</div>
<div class="form-footer">
<button type="submit" class="btn btn-primary w-100">
{{ tabler_icon('mail') }}
{{ 'Reset your password'|trans({}, 'TablerBundle') }}
</button>
{% from '@Tabler/components/buttons.html.twig' import submit_button %}
{{ submit_button('mail', {class: 'w-100', title: 'Reset your password', translation_domain: 'TablerBundle'}, 'primary') }}
</div>
</form>
{% endblock %}
Expand Down

0 comments on commit 22b4660

Please sign in to comment.