Swiftpack.co - Package - nodes-vapor/flash

Flash ⚡️

Swift Version Vapor Version Circle CI codebeat badge codecov Readme Score GitHub license

This package allows you to display Flash messages between your views.

image

Installation

Update your Package.swift file.

.package(url: "https://github.com/nodes-vapor/flash.git", from: "3.0.0")

Getting started 🚀

First make sure that you've imported Flash everywhere when needed:

import Flash

Adding the provider

public func configure(_ config: inout Config, _ env: inout Environment, _ services: inout Services) throws {
    try services.register(FlashProvider())
}

Adding the middleware

You can either add the Flash middleware globally by doing:

public func configure(_ config: inout Config, _ env: inout Environment, _ services: inout Services) throws {
    var middlewares = MiddlewareConfig()
    middlewares.use(FlashMiddleware.self)
    middlewares.use(SessionsMiddleware.self)
    services.register(middlewares)
}

Alternatively, you can add the middleware to individual route groups where needed:

router.group(FlashMiddleware()) { router in
    // .. routes
}

Please note that the SessionsMiddleware needs to be added to the same route groups where Flash is added.

Adding the Leaf tag

Using a shared Leaf tag config

This package supports using a shared Leaf tag config which removes the task of registering the tags from the consumer of this package. Please see this description if you want to use this.

Manually registering the Leaf tag(s)

In order to render Flash messages, you will need to add the Flash Leaf tag:

public func configure(_ config: inout Config, _ env: inout Environment, _ services: inout Services) throws {
    services.register { _ -> LeafTagConfig in
        var tags = LeafTagConfig.default()
        tags.use(FlashTag(), as: "flash")
        return tags
    }
}

Using Flash messages

With Flash set up, you are now able to redirect while adding a Flash message, given a Request:

request.redirect(to: "/users").flash(.success, "Successfully saved")
request.redirect(to: "/users").flash(.info, "Email sent")
request.redirect(to: "/users").flash(.warning, "Updated user")
request.redirect(to: "/users").flash(.error, "Something went wrong")

Make sure the route you are redirecting to is rendering the view on a privateContainer. For example:

func renderRegister(req: Request) throws -> Future<View> {
  let viewRenderer = try req.privateContainer.make(LeafRenderer.self)
  return viewRenderer.render("User/register")
}

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 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:

<div class="alerts">
    #flash() {
        #for(flash in all) {
            Message: #(flash.message)
            Type: #(flash.kind)
        }
    }
</div>

Using the Bootstrap package

The below example uses the Vapor 3 Bootstrap package for generating the alert html.

<div class="alerts">
    #flash() {
        #for(flash in all) {
            #bs:alert(flash.bootstrapClass) {
                #(flash.message)
            }
        }
    }
</div>

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. The package owner for this project is Brett.

📄 License

This package is open-sourced software licensed under the MIT license

Github

link
Stars: 24
Help us keep the lights on

Releases

3.0.0 - Jun 21, 2018

Changed

  • Uses Sugars new MutableLeafTagConfig to register the tag.
  • Removed the static tags from the FlashProvider.

1.0.1 - Jun 20, 2018

This release is compatible with Vapor 2

Fixes

  • Fixed strong reference cycle that lead to leaking Request and Helper

2.0.0 - Jun 11, 2018

Changed

  • Flash has been rewritten to support Vapor 3.

2.0.0-beta.3 - Jun 5, 2018

Changed

  • bootstrapType renamed to bootstrapClass.

2.0.0-beta.2 - May 4, 2018

Fixed

  • Made the EventLoopFuture extension public (thanks @mcdappdev)