This package comes with a Leaf tag that makes it easy and convenient to display Flash messages. We suggest to use the Bootstrap package for rendering Bootstrap elements, but this package does not depend on it.
It’s possible to loop through the different kinds of messages using:
all: All Flash messages no matter the kind.
successes: All Flash messages of type success.
information: All Flash messages of type info.
warnings: All Flash messages of type warning.
errors: All Flash messages of type error.
Further, using the message property you will be able to pull out the message of the Flash message. You can also get the kind by using the kind property. This property will hold one of the following values: success, info, warning or error. Lastly, you can use the bootstrapClass to get the relevant Bootstrap class:
success will return success.
info will return info.
warning will return warning.
error will return danger.
Not using the Bootstrap package
Without using any dependencies, this is how Flash messages could be rendered:
Flash ⚡️
This package allows you to display Flash messages between your views.
Installation
Add
Flash
to the package dependencies (in yourPackage.swift
file):as well as to your target (e.g. “App”):
Getting started 🚀
First make sure that you’ve imported Flash everywhere when needed:
Adding the provider
Adding the middleware
You can either add the Flash middleware globally by doing:
Alternatively, you can add the middleware to individual route groups where needed:
Please note that the
SessionsMiddleware
needs to be added to the same route groups where Flash is added.Adding the Leaf tag
In order to render Flash messages, you will need to add the Flash Leaf tag to your application:
Using Flash messages
With Flash set up, you are now able to redirect while adding a Flash message, given a
Request
:Example of HTML
This package comes with a Leaf tag that makes it easy and convenient to display Flash messages. We suggest to use the Bootstrap package for rendering Bootstrap elements, but this package does not depend on it.
It’s possible to loop through the different kinds of messages using:
all
: All Flash messages no matter the kind.successes
: All Flash messages of typesuccess
.information
: All Flash messages of typeinfo
.warnings
: All Flash messages of typewarning
.errors
: All Flash messages of typeerror
.Further, using the
message
property you will be able to pull out the message of the Flash message. You can also get the kind by using thekind
property. This property will hold one of the following values:success
,info
,warning
orerror
. Lastly, you can use thebootstrapClass
to get the relevant Bootstrap class:success
will returnsuccess
.info
will returninfo
.warning
will returnwarning
.error
will returndanger
.Not using the Bootstrap package
Without using any dependencies, this is how Flash messages could be rendered:
Using the Bootstrap package
The below example uses the Vapor 3 Bootstrap package for generating the alert html.
Add the Flash html to one file and embed it in rest of your views or through a base layout, e.g.:
#embed("alerts")
.🏆 Credits
This package is developed and maintained by the Vapor team at Nodes.
📄 License
This package is open-sourced software licensed under the MIT license