Skip to content

Usage

Initialization

To automatically display toasts and alerts in your templates, add the following Twig function in your base.html.twig (or the layout file):

{{ ux_sweet_alert_scripts() }}

Toasts

To use UX SweetAlert, inject the AlertManagerInterface service and use the toast() method to create toasts in PHP:

use Pentiminax\UX\SweetAlert\AlertManagerInterface;
use Pentiminax\UX\SweetAlert\Enum\Position;
class HomeController extends AbstractController
{
#[Route('/', name: 'app_homepage')]
public function index(AlertManagerInterface $alertManager): Response
{
$alertManager->toast(
title: 'title',
text: 'text',
position: Position::TOP_END,
timer: 3000,
timerProgressBar: true
);
return $this->render('home/index.html.twig');
}
}

Input dialogs

Use AlertManagerInterface::input() with an input type object when you need to collect a value:

use Pentiminax\UX\SweetAlert\AlertManagerInterface;
use Pentiminax\UX\SweetAlert\InputType\Text;
class ProfileController extends AbstractController
{
#[Route('/profile', name: 'app_profile')]
public function edit(AlertManagerInterface $alertManager): Response
{
$alertManager->input(
inputType: new Text(
label: 'Display name',
value: 'Tanguy',
placeholder: 'Enter your display name',
),
title: 'Update profile',
text: 'This value will be visible to other users.'
);
return $this->render('profile/index.html.twig');
}
}

For Live Component-based confirmation and input buttons, see the Live Component guide.