SSbits - Home page
Site by Carbon Crayon
Submit a Post >

Tutorials - Big bits of code to help you do more

Setting and displaying custom messages in your site


There is a method on SilverStripe's Form class to set a success or error message when the form is submitted. That's cool... but sometimes you need to display a message that is not related to a form. I've created a couple of simple controller methods to enable setting and displaying of message banners anytime.

In Page.php

class Page_Controller extends ContentController {

	public function setMessage($type, $message)
		Session::set('Message', array(
			'MessageType' => $type,
			'Message' => $message

	public function getMessage()
		if($message = Session::get('Message')){
			$array = new ArrayData($message);
			return $array->renderWith('Message');

The setMessage() method on the controller takes two arguments; $type is used as the CSS id for the message template. It defines the color and styles for the message. I use one of 3 message types - Error, Success, Notice. You can use whatever message types you want, just add styles accordingly. The $message argument is the message you want to be displayed to the user.

When setMessage() is called, a Message array is stored in Session, ready to be retrieved by, you guessed it... getMessage(). The getMessage method grabs the message if it has been set, removes it from Session (we only want it to display once) and then returns the Message array rendered with the Message template. 

In themes/yourtheme/templates/Includes/ 

<% if Message %>
	<p class='message' id='{$MessageType}Message'>
<% end_if %>

First we check if a message has been set, if so, render the HTML to display the message, with a css id that will identify the message type for our CSS styles.


p.message {
	margin: 10px 10px 0;
	border: 1px solid #FFF;

p#SuccessMessage {

p#NoticeMessage {

p#ErrorMessage {
	color: #DA1D1D;
	background: #FFDFDF;
	border-color: #FFBBBB;

So that's the guts of it. We can now place $Message in our page template wherever we want the message displayed.

Example usage (in page controller or extension of)

if($foo ! = $bar){
	$this->setMessage('Error', 'Error: Yo fool, foo is not bar');
  • Peter Toi
    31/10/2011 12:38pm (6 years ago)

    Very simple solution. Thanks!

  • Frank Mullenger
    02/11/2011 11:33pm (6 years ago)

    Hey Shea, I made a module that decorates page_controller and does pretty much the same thing - great minds think alike! :-)

    One problem I came across was setting the session message then a call to header() using Director::redirect() or similar was not passing the session ID or something? Have you noticed this as well?

  • MRKDevelopment
    17/11/2011 3:39pm (6 years ago)

    This is very similar to a flash message in CakePHP. Does anyone know if the new silverstripe have this feature built in ?

    It would certainly be a handy feature to have and most other fraeworks have it.

  • Adam Stead
    11/02/2012 1:47pm (5 years ago)

    We have been doing something similar, except your solution is nice and simple. Love it, great work.

  • Shea
    12/02/2012 12:47am (5 years ago)

    Thanks for the feedback guys.

    @Frank Nice module! No, I havn't had the problem you describe...

    Sounds like a few people are implementing similar solutions... i think it would be awesome if something like this could be added to SS3. Might look into that ;)

  • ryduhartchas
    27/10/2015 3:49pm (2 years ago)

  • ryduhartchas
    27/10/2015 3:49pm (2 years ago)

  • ryduhartchas
    27/10/2015 3:50pm (2 years ago)

Post a comment ...

You cannot post comments until you have logged in. Login Here.


Site of the Month

Find SSbits on

Top Contributers

Rank Avatar Name
1 article image Aram Balakjian
2 article image wfacewomens
3 article image Daniel Hensby
4 article image Marcus Dalgren
5 article image ryduhartchas
6 article image Hamish Campbell
7 article image Ty Barho
8 article image Martijn van Nieuwenhoven
9 article image Darren-Lee
10 article image Roman Schmid

View full leaderboard