Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
720 views
in Technique[技术] by (71.8m points)

css - Working with Zend Framework FlashMessenger and jQuery UI state classes

I have a Zend Framework application that is making use of jQuery UI.

In my controllers, I am setting error/success messages with the FlashMessenger helper like this:

// ExampleController.php
$this->_helper->FlashMessenger('Sorry, could not complete your request.');

In my layout, I am displaying the messages by using the Noumenal FlashMessenger View Helper

// layout.phtml
<?php echo $this->flashMessenger(); ?>

I want to make use of my jQuery UI theme's CSS styles to style my error messages like this:

<div class="ui-state-error ui-corner-all"> 
    <p><span class="ui-icon ui-icon-alert"></span> 
    <strong>Alert:</strong> Sample ui-state-error style.</p>
</div>

...but the View Helper is doing all the work, so I can't change the classes how I want to. So before going down a dead end route, I thought I'd ask the community. Here are my questions:

  1. How can I use the Zend Framework FlashMessenger so that I can set different messages depending on the state (error/success)?
  2. How can I get the messages from the FlashMessenger and display them in a single place without having to create duplicate code in all of my controllers?
  3. How can I output a different class for each of the different message states? For example: 'error'=>'ui-state-error', 'info'=>'ui-state-highlight', etc.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Having written the Noumenal FlashMessenger View Helper I should be able to help. :-)

To answer your question:

Adding Messages

You can set different message levels, e.g. error, warning etc, by passing an array to the FlashMessenger Action Helper rather than a simple string:

// ExampleController.php
$this->_helper->FlashMessenger(
     array('error'=>'Sorry, could not complete your request.')
);

The view helper is designed to recognise this.

Outputting Messages

When outputting FlashMessages in your layout, there are optional parameters that you can pass to specify the default message level (which is warning by default) and a template for your message.

Adapting your code snippet to account for differing message levels you could achieve the desired result by making the following call in your layout:

// layout.phtml
$template = '<div class="ui-state-error ui-corner-all"> 
    <p class="%s"><span class="ui-icon ui-icon-alert"></span> 
    <span class="flash-message">%s</span></p>
</div>';
echo $this->flashMessenger('error', $template);

(You may find it better to set the template as a view variable in, say, your bootstrap.)

Doing this the view helper will construct the appropriately formatted flash messages for you as you wish.

Some Simple Styling

Using CSS there would be plenty of room to style the messages appropriately. For example:

.alert {
    color: red;
}

.alert .flash-message:before {
    content: "<strong>Alert</strong> ";
}

.notice {
   color:yellow;
}

.notice .flash-message:before {
    content: "<strong>Notice</strong> ";
}    

I leave you to improvise...

I wrote a guide to Zend Framework FlashMessenger and the view helper on my blog. Perhaps give that a read. Also please do email me to let me know your difficulties -- it will help me know what I need to improve.

I hope that helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...