Symfony/Twig else nie działa z is_granted()

0

Mam taki kod, mimo że jestem zalogowany to oba if się wykonują.

{% extends "base.html.twig" %}
    {% if is_granted('IS_AUTHENTICATED_FULLY') %}
        {% block alerts %}
        <div class="alert alert-danger">
            <h4 class="alert-heading">The login form can not be displayed</h4>
            You are already logged in as <b> {{ app.user.username }} </b>!<br>
            Return to the
            <a class="link-custom alert-link" style="color: #491217;" onmouseover="this.style.textDecoration='underline';"
               onmouseout="this.style.textDecoration='none';" onclick="window.history.go(-1); return false;">previous page</a>
            or go to the <a class="alert-link" href="/">homepage</a>.
            <hr>
            If you want to log in, you have to <a class="alert-link" href="/logout">logout</a> first.
        </div>
        {% endblock %}
    {% else %}
{% block body %}



{% if error %}
    <div>{{ error.messageKey|trans(error.messageData, 'security') }}</div>
{% endif %}

<form action="{{ path('login') }}" method="post">
    <label for="username">Username:</label>
    <input type="text" id="username" name="_username" value="{{ last_username }}" />

    <label for="password">Password:</label>
    <input type="password" id="password" name="_password" />

    {#
        If you want to control the URL the user
        is redirected to on success (more details below)
        <input type="hidden" name="_target_path" value="/account" />
    #}

    <button type="submit">login</button>
</form>

{% endblock %}
    {% endif %}
0

ref

0

Musisz wrzucić ifa do środka bloku - tak po prostu działa Twig.

0

@Patryk27:
Zmieniłem, ale nadal pokazuje oba:

{% extends "base.html.twig" %}
        {% block alerts %}
            {% if is_granted('IS_AUTHENTICATED_FULLY') %}
        <div class="alert alert-danger">
            <h4 class="alert-heading">The login form can not be displayed</h4>
            You are already logged in as <b> {{ app.user.username }} </b>!<br>
            Return to the
            <a class="link-custom alert-link" style="color: #491217;" onmouseover="this.style.textDecoration='underline';"
               onmouseout="this.style.textDecoration='none';" onclick="window.history.go(-1); return false;">previous page</a>
            or go to the <a class="alert-link" href="/">homepage</a>.
            <hr>
            If you want to log in, you have to <a class="alert-link" href="/logout">logout</a> first.
        </div>
            {% else %}
                {% block body %}



                {% if error %}
                    <div>{{ error.messageKey|trans(error.messageData, 'security') }}</div>
                {% endif %}

                <form action="{{ path('login') }}" method="post">
                    <label for="username">Username:</label>
                    <input type="text" id="username" name="_username" value="{{ last_username }}" />

                    <label for="password">Password:</label>
                    <input type="password" id="password" name="_password" />

                    {#
                        If you want to control the URL the user
                        is redirected to on success (more details below)
                        <input type="hidden" name="_target_path" value="/account" />
                    #}

                    <button type="submit">login</button>
                </form>

        {% endblock %}
            {% endif %}
        {% endblock %}

0

Ayyy carramba:

{% block alerts %}
  {% if ... %}
    foo
  {% endif %}
{% endblock %}

{% block body %}
  {% if ... %}
    bar
  {% endif %}
{% endblock %}

1 użytkowników online, w tym zalogowanych: 0, gości: 1