views:

657

answers:

4

In a CakePHP 1.2 app, I'm using flash();?> to output messages like "Record edited". It's working great.

However, I want to add a link called "Dismiss" that will fade out the message. I know how to construct the link, but I don't know how to insert into the output of the flass message.

The flash message wraps itself in a DIV tag. I want to insert my dismiss code into that div, but I don't know how.

+1  A: 

Figured this out: Create a new layout in your layouts folder:

layouts/message.ctp

In that layout, include the call to output the content:

<?php echo $content_for_layout; ?>

Then when you set the flash message, specify the layout to use:

$this->Session->setFlash('Your record has been created! Wicked!','message');
Justin
I'm not sure why my php code line isn't appearing. Looks like a bug in the sanitizer.
Justin
You need to indent code using 4 spaces.
Paolo Bergantino
+1  A: 

You want to use the setflash function. If you pass setflash an empty string for $default it will not wrap your message in a div and just store it as is. This way you can display any markup you want or as Justin posted you can use another view page for your message so you don't mix your view and controllers.

gradbot
A: 

Hello Justin,

the default way to do is is to create a flash.ctp in your /app/views/layouts. This will override the default flash.ctp you can find in /cake/libs/view/layouts. So you don't need to use the additional param.

btw: this works for all CakePHP standard views and layouts.

rscherer
+1  A: 

You can achieve this with jQuery:

$(document).ready(function() {

    $("#flashMessage").each(function() {
     $close = $("<span class='close'>Close</span>");
     $close.click(function () {
      $(this).parent().hide("slow");
     });
     $(this).append($close);
    });

});

You will need to pretty it up with a bit of CSS, but I'm sure you get the idea.

RichardAtHome