Cameron
Cameron

Reputation: 28783

CakePHP custom setFlash

I've read on the CakePHP Book that you can define your own custom setFlash messages using an element... but what I would I put inside the element and how would I pass different content.

So for example two different messages:

<div id="flashMessage" class="message">
    <div class="content">
        <p>Please correct the errors</p>
    </div>
</div>


<div id="flashMessage" class="announcement message">
    <div class="content">
        <h3>Announcement</h3>
        <p>You have earned a new achievement</p>
    </div>
</div>

So as you can see I want to define a wrapper div and a content div and then also pass in an additional class if need be dependant on the kind of message and also show different content such as just a paragraph or a header and a paragraph.

Can anyone help? Thanks

Upvotes: 2

Views: 2847

Answers (1)

mentalic
mentalic

Reputation: 996

You can have two elements:

myflash.ctp

<div id="flashMessage" class="message">
    <div class="content">
        <p><? echo $message ?></p>
    </div>
</div>

announcement.ctp

<div id="flashMessage" class="announcement message">
    <div class="content">
        <h3>Announcement</h3>
        <p><? echo $message ?></p>
    </div>
</div>

and then:

$this->Session->setFlash($message,'myflash or announcement');

or one element: myflash.ctp

<div id="flashMessage" class="<? echo (isset($myclass)?$myclass.' ':'') ?>message">
 <div class="content">
  <? echo (isset($header)?'<h3>' . $header.'</h3>':'') ?>
   <p><? echo $message ?></p>
 </div>
</div>

and at your controller:

$this->Session->setFlash($message,'myflash',array('myclass'=>'announcement','header'=>'Announcement');

Upvotes: 4

Related Questions