开发者

Manipulate $session->flash() view output in CakePHP

I am looking to manipulate the $session->flash() output in my CakePHP app, Currently I have the very simple default implementation of showing flash and auth error messages:

<?php
    $session->flash();
    $session->flash('auth');
?>

This produces a 开发者_JAVA技巧<div> with an ID and class that has the message inside. What I would like to do is wrap/replace the generated HTML, specifically with some jQuery UI classes, but wrapping is difficult as I am unable to tell when there is actually a message going to be displayed so I end up with an empty but style error div. What I really need for wrapping to work is to check in $session->flash() returns anything, but I get 'can't use method return value in write context' when checking it with empty();

As far as I can tell the generated HTML is hard coded into the session helper! Bonus points if you can work out how to change the class on the auth message and normal flash message independently.


To check if a message is going to be flashed, put this in the layout

<?php if($session->check('Message')){ echo $this->Session->flash();} ?>

CSS attributs can be set when you set the message to be flashed

http://book.cakephp.org/view/1311/Methods#setFlash-1313


read up on setFlash() and use the other params that the method takes to define your own elements. you can then do what ever you like.

http://book.cakephp.org/view/400/setFlash


A work-around solution I have found is to set $session->flash() to a variable and then check it with empty() so I can echo out the appropriate <div> if necessary.

$flashMessage = $this->flash();
$authMessage = $this->flash('auth');

.. and then check if each one is empty. Of course I have some unnecessary html inside there but as far as the auth message goes, I think this is as flexible as I can get.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜