Skip to content

View Helpers

flashMessages()

flashMessages() — returns string

Available in: controller Category: Miscellaneous Functions

Displays a marked-up listing of messages that exist in the Flash.

NameTypeRequiredDefaultDescription
keysstringno—The key (or list of keys) to show the value for. You can also use the key argument instead for better readability when accessing a single key.
classstringnoflash-messagesHTML class to set on the div element that contains the messages.
includeEmptyContainerbooleannofalseIncludes the div container even if the Flash is empty.
encodebooleannotrueUse this argument to decide whether the output of the function should be encoded in order to prevent Cross Site Scripting (XSS) attacks. Set it to true to encode all relevant output for the specific HTML element in question (e.g. tag content, attribute values, and URLs). For HTML elements that have both tag content and attribute values you can set this argument to attributes to only encode attribute values and not tag content.
// 1. Display all Flash messages in a view
// In the controller action:
flashInsert(success="Your post was successfully submitted.");
flashInsert(alert="Don't forget to tweet about this post!");
flashInsert(error="This is an error message.");

// In the layout or view:
writeOutput(flashMessages());

// Generates (keys sorted alphabetically):
// <div class="flash-messages">
//   <p class="alert-message">Don't forget to tweet about this post!</p>
//   <p class="error-message">This is an error message.</p>
//   <p class="success-message">Your post was successfully submitted.</p>
// </div>


// 2. Show only a single Flash key using the `key` alias
writeOutput(flashMessages(key="success"));

// Generates:
// <div class="flash-messages">
//   <p class="success-message">Your post was successfully submitted.</p>
// </div>


// 3. Show a specific set of keys in a defined order using `keys`
writeOutput(flashMessages(keys="success,alert"));

// Generates (in the order supplied, not alphabetically):
// <div class="flash-messages">
//   <p class="success-message">Your post was successfully submitted.</p>
//   <p class="alert-message">Don't forget to tweet about this post!</p>
// </div>


// 4. Always render the container div, even when the Flash is empty
writeOutput(flashMessages(includeEmptyContainer=true));

// Generates:
// <div class="flash-messages"></div>


// 5. Use a custom CSS class on the outer container
writeOutput(flashMessages(class="notifications"));

// Generates:
// <div class="notifications">
//   <p class="alert-message">Don't forget to tweet about this post!</p>
//   <p class="error-message">This is an error message.</p>
//   <p class="success-message">Your post was successfully submitted.</p>
// </div>