Skip to content

nielskrijger/FlashMessageBundle

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FlashMessage Bundle

The FlashMessage bundle is a basic script that allows you to show typical brief success or error messages after a user action. This plugin comes with four pre-styled message types: 'success', 'notice', 'warning' and 'error'.

Features:

  • renders the standard Symfony session component flash messages.
  • a twig extension making it easy to show manual messages if you want to.
  • a javascript interface enabling you to show flash messages without reloading.

Dependencies:

  • jQuery (tested with version 1.4.2 and 1.6.4)

Installation

  1. Add this bundle to your vendor/dir using the vendors script:
Add the following lines in your ``deps`` file:

    [ICEFlashMessageBundle]
        git=git://github.com/nielskrijger/FlashMessageBundle.git
        target=/bundles/ICE/FlashMessageBundle

Run the vendors script:

    php bin/vendors install
  1. Add the ICE namespace to your autoloader:

    // app/autoload.php $loader->registerNamespaces(array( 'ICE' => DIR.'/../vendor/bundles', ));

  2. Add this bundle to your application's kernel:

    // app/AppKernel.php public function registerBundles() { return array( // ... new ICE\FlashMessageBundle\ICEFlashMessageBundle(), // ... ); }

  3. Configure the ice_flash_message service in your config.yml:

    ice_flash_message: ~

  4. Add the following files to the head of your main template:

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript" src="{{ asset('bundles/iceflashmessage/js/jquery.flashMessage.js') }}"></script>
Instead of asset() you might want to use assetic here, see Symfony's documentation how to.
  1. Call twig function where you want to show the flash messages:

    bundle/Resources/views/template.html.twig

    {{ showFlashMessages() }}

Creating a flash message

You are advised to use the either one of the four flash message types: 'error', 'warning', 'notice', and 'success'. If you pass any other type it is set as the className of the message's enclosing div.

By default all messages passed to the session are rendered by the FlashMessage bundle. To add a flash message you can use the Symfony session service:

    # bundle/Controller/MyController.php
    $this->get('session')->setFlash('success', 'Well done! You\'ve been initialized');

The symfony session service renders the message after a new page request. If you want to show a flash message immediately you can render it in the template directly:

    # bundle/Resources/views/template.html.twig
    {{ flash('success', 'Scotty has beamed you up!') }}

Alternatively you can render a flash message in javascript after for example an ajax request:

    # bundle/Resources/views/template.html.twig
    <script language="javascript">
    $(function () {
        $.flash({ type: "success", message: "You are a success!" });  
    });
    </script>

jQuery Flash parameters

The jQuery function $.flash accepts the following parameters:

  • key: message, type: string, default: "No message set"
  • key: type, type: string, default: "success"

Credits

The icons are from the excellent FamFamFam icon set by Mark James.

About

A Symfony2 bundle for showing brief messages after a user action

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages