【问题标题】:Working with Zend Framework FlashMessenger and jQuery UI state classes使用 Zend Framework FlashMessenger 和 jQuery UI 状态类
【发布时间】:2010-06-04 16:14:37
【问题描述】:

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

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

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

在我的布局中,我使用 Noumenal FlashMessenger View Helper 显示消息

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

我想使用我的jQuery UI theme's CSS styles 来设置我的错误消息的样式,如下所示:

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

...但是 View Helper 正在完成所有工作,所以我无法更改我想要的类。所以在走上死胡同之前,我想我会问社区。以下是我的问题:

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

【问题讨论】:

    标签: css zend-framework jquery-ui


    【解决方案1】:

    写完Noumenal FlashMessenger View Helper 我应该可以提供帮助。 :-)

    回答你的问题:

    添加消息

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

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

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

    输出消息

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

    调整您的代码 sn-p 以适应不同的消息级别,您可以通过在布局中进行以下调用来实现所需的结果:

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

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

    执行此操作,视图助手将根据您的需要为您构建适当格式的 Flash 消息。

    一些简单的样式

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

    .alert {
        color: red;
    }
    
    .alert .flash-message:before {
        content: "<strong>Alert</strong> ";
    }
    
    .notice {
       color:yellow;
    }
    
    .notice .flash-message:before {
        content: "<strong>Notice</strong> ";
    }    
    

    我让你即兴发挥......

    我写了一个guide to Zend Framework FlashMessenger and the view helper on my blog。也许读一读。也请email me to let me know your difficulties - 这将帮助我了解我需要改进的地方。

    希望对你有帮助。

    【讨论】:

    • 使用 Flash Messenger 的 Noumenal 库非常方便。允许我使用两行代码轻松简单地向我的布局添加 Flash(包括引导程序中的视图助手,打印出最后一个已知的 Flash 消息)。期待看到更多的本体类!
    【解决方案2】:

    我最终修改了 flashMessenger() 视图助手以使用我的 jQuery 模板。它根据键(错误、通知等)输出正确的模板。可能有更好的方法来做到这一点,但它可以完成工作。

    public function flashMessenger(...)
    {
        //...
    
        //process messages
        foreach ($messages as $message)
        {
            if (is_array($message)) {
                list($key,$message) = each($message);
            }
            $template = $this->_getJqueryTemplate($key);
            $output .= sprintf($template,$message);
        }
        return $output;
    }
    
    private function _getJqueryTemplate($messageLevel)
    {
        switch($messageLevel) {
            case 'error':
                $template = '
                    <div class="ui-state-error ui-corner-all" style="padding: 0pt 0.7em;"> 
                        <p><span class="ui-icon ui-icon-alert" style="float: left; margin-right: 0.3em;"></span> 
                        %s</p>
                    </div>';
                break;
            default:
                $template = '
                    <div class="ui-state-highlight ui-corner-all" style="padding: 0pt 0.7em;"> 
                        <p><span class="ui-icon ui-icon-info" style="float: left; margin-right: 0.3em;"></span> 
                        %s</p>
                    </div>';
        }
        return $template;
    }
    

    然后在控制器中,通过将其作为数组键传递来指定要发送的消息类型。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-11
      • 2011-10-26
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多