Question

In a CakePHP 1.2 app, I'm using

<?php $session->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.

Was it helpful?

Solution 3

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');

OTHER TIPS

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.

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top