Symfony2 session-flash 与树枝中的 if 子句不起作用 [英] Symfony2 session-flash with if clause in twig isn't working

查看:21
本文介绍了Symfony2 session-flash 与树枝中的 if 子句不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试对设置的 session-flash 做出反应,但总是得到 else-path

Symfony 2.1.3

控制器:

$this->get('session')->getFlashBag()->set('contactActionNoticeError', 'Message not sent');

查看(尝试旧"和新样式)但我得到 bla2

{% if app.session.flashbag.has("contactActionNoticeError") 或 app.session.hasFlash("contactActionNoticeError") %}bla1{% 别的 %}bla2{% 万一 %}

当显示所有闪烁时:

{% for label, flash in app.session.flashbag.all %}{% for flash in flash %}{{ 标签 }} - {{ 闪光 }}{% 结束为 %}{% 结束为 %}

我明白了:

contactActionNoticeError - 消息未发送

解决方案

获取 flashbag 内容,然后查看是否为空:

{% set contactActionNoticeError = app.session.flashbag.get("contactActionNoticeError") %}{% if (contactActionNoticeError 不为空) %}bla1{% 别的 %}bla2{% 万一 %}

您仍然可以显示错误(代码取自 文档):

{% for flashMessage in contactActionNoticeError %}<div>{{ flashMessage }}

{% 结束为 %}

I try to react on a setted session-flash but get always the else-path

Symfony 2.1.3

Controller:

$this->get('session')->getFlashBag()->set('contactActionNoticeError', 'Message not sent');

View (tried "old" and new style) But I get bla2

{% if app.session.flashbag.has("contactActionNoticeError") or app.session.hasFlash("contactActionNoticeError") %}
    bla1
{% else %}
    bla2
{% endif %}

when showing all flashes with this:

{% for label, flashes in app.session.flashbag.all %}
    {% for flash in flashes %}
        {{ label }} - {{ flash }}
    {% endfor %}
{% endfor %}

I get this:

contactActionNoticeError - Message not sent

解决方案

Get the flashbag content then see if it's empty or not:

{% set contactActionNoticeError = app.session.flashbag.get("contactActionNoticeError") %}

{% if (contactActionNoticeError is not empty) %}
    bla1
{% else %}
    bla2
{% endif %}

You can still display the errors (code taken from the documentation):

{% for flashMessage in contactActionNoticeError %}
    <div>
        {{ flashMessage }}
    </div>
{% endfor %}

这篇关于Symfony2 session-flash 与树枝中的 if 子句不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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