Skip to content

Commit 49b5c44

Browse files
committed
Add multiples templates for toast item
1 parent f234732 commit 49b5c44

File tree

12 files changed

+137
-2
lines changed

12 files changed

+137
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Added
66

77
- Progress bar line in toasts
8+
- Customizable toast items templates
89

910
## 0.2.0
1011

config/definition.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
->integerNode('timer')->defaultValue(5000)->end()
2525
->booleanNode('dismissible')->defaultTrue()->end()
2626
->booleanNode('progress_bar')->defaultFalse()->end()
27+
->stringNode('template')->isRequired()->end()
2728
->end()
2829
->end()
2930
->end()

docs/config.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ asmitta_toast:
1313
timer: 5000
1414
progress_bar: false
1515
dismissible: true
16+
template: '@AsmittaToast/toast_items/default.html.twig'
1617
```
1718
1819
## Configuration Options
@@ -47,3 +48,8 @@ asmitta_toast:
4748

4849
- Adds a close button to manually dismiss toasts
4950
- When `false`, toasts can only be dismissed by timer
51+
52+
**template** (string, default: `@AsmittaToast/toast_items/default.html.twig`)
53+
54+
- Twig template used to render individual toast items
55+
- More info in [the templating doc](./templating.md)

docs/index.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# ToastBundle
2+
3+
A Symfony bundle for displaying [Bootstrap toasts](https://getbootstrap.com/docs/5.3/components/toasts/) from flash messages.
4+
5+
## Installation
6+
7+
Make sure Composer is installed globally, as explained in the
8+
[installation chapter](https://getcomposer.org/doc/00-intro.md)
9+
of the Composer documentation.
10+
11+
Open a command console, enter your project directory and execute:
12+
13+
```console
14+
composer require asmitta-01/toast-bundle
15+
```
16+
17+
## Configuration
18+
19+
if your application dont't use Symfony Flex, enable the bundle in `config/bundles.php`:
20+
21+
```php
22+
return [
23+
// ...
24+
Asmitta\ToastBundle\AsmittaToastBundle::class => ['all' => true],
25+
];
26+
```
27+
28+
Create a configuration file at `config/packages/asmitta_toast.yaml`.
29+
30+
## Resources
31+
32+
- [Configuration file](./config.md)
33+
- [Templating](./templating.md)

docs/templating.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Templating
2+
3+
The bundle provides three(03) toast items templates actually:
4+
5+
- `@AsmittaToast/toast_items/default.html.twig` (Default)
6+
- `@AsmittaToast/toast_items/with_icon.html.twig`
7+
- `@AsmittaToast/toast_items/colored_icon.html.twig`
8+
9+
:warning: If you're using a template displaying icons, **you should include bootstrap icons package** in your view:
10+
11+
```html
12+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
13+
```
14+
15+
You can create custom templates and specify their path here. Ensure your custom template accepts the following variables: `type`, `message`, `toast_type`, `timer`, `is_dismissible`, `show_progress_bar`(See [default template](../templates/toast_items/default.html.twig)).

src/AsmittaToastBundle.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public function loadExtension(array $config, ContainerConfigurator $container, C
4444
->arg(0, $config['toast_item']['timer'])
4545
->arg(1, $config['toast_item']['dismissible'])
4646
->arg(2, $config['toast_item']['progress_bar'])
47+
->arg(3, $config['toast_item']['template'])
4748
;
4849
}
4950
}

src/Service/ToastItemConfig.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ class ToastItemConfig
77
public function __construct(
88
private int $timer,
99
private bool $dismissible,
10-
private bool $showProgressBar
10+
private bool $showProgressBar,
11+
private string $template
1112
) {}
1213

1314
public function getTimer(): int
@@ -24,4 +25,9 @@ public function showProgressBar(): bool
2425
{
2526
return $this->showProgressBar;
2627
}
28+
29+
public function getTemplate(): string
30+
{
31+
return $this->template;
32+
}
2733
}

src/Twig/ToastExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public function renderToasts(): string
6060
'timer' => $this->toastItemConfig->getTimer(),
6161
'is_dismissible' => $this->toastItemConfig->isDismissible(),
6262
'show_progress_bar' => $this->toastItemConfig->showProgressBar(),
63+
'template' => $this->toastItemConfig->getTemplate(),
6364
]);
6465
}
6566

templates/toast_container.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<div class="toast-container position-fixed {{ position }} p-3">
22
{% for type, messages in flashes %}
33
{% for message in messages %}
4-
{% include '@AsmittaToast/toast_item.html.twig' with {
4+
{% include template with {
55
'type': type,
66
'message': message,
77
'toast_type': toast_types[type] ?? 'info',
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{% set class = 'text-' ~ type %}
2+
3+
{% set accessibility_attr = {
4+
'success': 'role="status" aria-live="polite"',
5+
'warning': 'role="alert" aria-live="assertive"',
6+
'danger': 'role="alert" aria-live="assertive"',
7+
'info': 'role="status" aria-live="polite"'
8+
}[type] ?? 'role="alert"' %}
9+
10+
{% set icon = {
11+
info: 'bi-info-circle-fill',
12+
danger: 'bi-exclamation-circle-fill',
13+
warning: 'bi-exclamation-triangle-fill',
14+
success: 'bi-check-circle-fill'
15+
}[type] ?? 'bi-info-circle' %}
16+
17+
<div class="toast d-flex position-relative" {{ accessibility_attr|raw }} data-bs-autohide="{{ timer > 0 ? 'true' : 'false' }}" data-bs-delay="{{ timer }}">
18+
<i class="{{ icon }} ms-2 mt-2 fs-2 {{ class }}"></i>
19+
20+
<div class="w-100">
21+
<div class="toast-header border-0 pb-0">
22+
<strong class="me-auto">{{ toast_type|trans({}, 'asmitta_toast_bundle') }}</strong>
23+
24+
{% if is_dismissible %}
25+
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="toast"></button>
26+
{% endif %}
27+
</div>
28+
<div class="toast-body">
29+
{{ message }}
30+
</div>
31+
</div>
32+
33+
{% if show_progress_bar and timer > 0 %}
34+
<div class="asmitta-toast-progress-bar active" style="--asmitta-toast-progress-timer: {{ timer }}ms; --asmitta-toast-progress-color: var(--bs-{{ type }})"></div>
35+
{% endif %}
36+
</div>

0 commit comments

Comments
 (0)