Yii 1.1: Using CJuiDialog to display flash Messages in Dialogues

8 followers

In order to nicely display flash Messages, create view like this called views/site/dialog.php :

<?php
if($flashes = Yii::app()->user->getFlashes()) {
    foreach($flashes as $key => $message) {
        if($key != 'counters') {
            $this->beginWidget('zii.widgets.jui.CJuiDialog', array(
                        'id'=>$key,
                        'options'=>array(
                            'show' => 'blind',
                            'hide' => 'explode',
                            'modal' => 'true',
                            'title' => $message['title'],
                            'autoOpen'=>true,
                            ),
                        ));
 
            printf('<span class="dialog">%s</span>', $message['content']);
 
            $this->endWidget('zii.widgets.jui.CJuiDialog');
        }
    }
}
?>

add this line before your <?php echo $content; ?> of your layout file (most probably views/layouts/main.php) :

<?php $this->renderPartial('//site/dialog'); ?>

after that, a very nice jquery ui dialog will display your flash messages.

In addition to this, i suggest a wrapper function like this:

<?php
class Dialog {
    public static function Message($title, $message, $id = 0) {
        if($id == 0)
            $id = rand(1, 999999);
        Yii::app()->user->setflash($id, array('title' => $title, 'content' => $message) );
    }
}
?>

This way you can toggle your Dialogues, even with HTML content and different Buttons inside as easy as this:

$message = 'Hello World!';
        $message .= '<br /><strong>Where do you want to go today?</strong><br />';
        $message .= CHtml::Button('Home', array('submit' => array('home/admin')));
        $message .= '<br />';
        $message .= CHtml::Button('Far far away', array('submit' => array('far/away')));
        Dialog::message('Title', $message);

or just

Dialog::message('title', 'content');

I hope this Code snippet will be useful for you. Please send any advices, suggestions or errors to thyseus@gmail.com, thank you.

Total 1 comment

#2250 report it
Angelo at 2010/12/01 06:14pm
Great set up even without CJuiDialog

I use this setup without the widget and just add the $message['title'] to the printf statement. Now I can call any css styled message box i have with the same simple call.

Leave a comment

Please to leave your comment.

Write new article
  • Written by: thyseus
  • Category: Tutorials
  • Yii Version: 1.1
  • Votes: +11
  • Viewed: 17,288 times
  • Created on: Aug 8, 2010
  • Last updated: never