使用 Zend Framework FlashMessenger 和 jQuery UI 状态类 [英] Working with Zend Framework FlashMessenger and jQuery UI state classes

查看:18
本文介绍了使用 Zend Framework FlashMessenger 和 jQuery UI 状态类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 jQuery UI 的 Zend Framework 应用程序.

在我的控制器中,我使用 FlashMessenger 助手设置错误/成功消息,如下所示:

//ExampleController.php$this->_helper->FlashMessenger('抱歉,无法完成您的请求.');

在我的布局中,我使用Noumenal FlashMessenger 查看助手

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

我想使用我的 jQuery UI 主题的 CSS 样式 来设置我的错误消息的样式,如下所示:

<p><span class="ui-icon ui-icon-alert"></span><strong>警报:</strong>示例 ui-state-error 样式.</p>

...但是视图助手正在做所有的工作,所以我不能改变我想要的类.所以在走上一条死胡同之前,我想我会问社区.以下是我的问题:

  1. 如何使用 Zend Framework FlashMessenger 以便根据状态(错误/成功)设置不同的消息?
  2. 如何从 FlashMessenger 获取消息并将它们显示在一个地方,而不必在我的所有控制器中创建重复的代码?
  3. 如何为每个不同的消息状态输出不同的类?例如:'error'=>'ui-state-error'、'info'=>'ui-state-highlight'

解决方案

已编写Noumenal FlashMessenger View Helper 我应该能帮上忙.:-)

回答您的问题:

添加消息

您可以设置不同的消息级别,例如errorwarning 等,通过向 FlashMessenger Action Helper 传递一个数组而不是一个简单的字符串:

//ExampleController.php$this->_helper->FlashMessenger(array('error'=>'抱歉,无法完成您的请求.'));

视图助手旨在识别这一点.

输出消息

在您的布局中输出 FlashMessage 时,您可以传递一些可选参数来指定默认消息级别(默认为 warning)和消息模板.

调整您的代码片段以适应不同的消息级别,您可以通过在布局中进行以下调用来获得所需的结果:

//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>

';echo $this->flashMessenger('error', $template);

(你可能会发现在你的引导程序中将模板设置为视图变量会更好.)

执行此操作后,视图助手将根据您的意愿为您构建适当格式的 Flash 消息.

一些简单的样式

使用 CSS 将有足够的空间来适当地设置消息样式.例如:

.alert {红色;}.alert .flash-message:before {内容:<strong>警报</strong>";}.注意 {颜色:黄色;}.notice .flash-message:before {内容:注意事项";}

我让你即兴发挥...

我写了一个 Zend Framework FlashMessenger 指南和在我的博客上查看助手.也许读一读.也请给我发电子邮件让我知道你的困难——这将帮助我知道我需要改进的地方.

希望能帮到你.

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.

解决方案

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.

这篇关于使用 Zend Framework FlashMessenger 和 jQuery UI 状态类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆