Symfony2 and Twig sidebar

两盒软妹~` 提交于 2019-12-12 03:42:50

问题


I am developing an application using Symfony2. I am using Twig for templating an a 3 level interface architecture. In my application there are 3 kind of users, anonimous users, udentified users and Administrators. The Page is divided in sections this way:

<!-- app/Resources/views/base.html.twig -->
<!DOCTYPE html>

<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />

        <title>{% block title %}Anotatzailea{% endblock %} - Anotatzailea</title>
            <!--[if lt IE 9]>
                    <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
            <![endif]-->

            {% block stylesheets %}
        <link href='http://fonts.googleapis.com/css?family=Voces' rel='stylesheet' type='text/css'>
                <link href='http://fonts.googleapis.com/css?family=La+Belle+Aurore' rel='stylesheet' type='text/css'>
                <link href="{{ asset('css/screen.css') }}" type="text/css" rel="stylesheet" />
            {% endblock %}

            <link rel="shortcut icon" href="{{ asset('favicon.ico') }}" />
        </head>

    <body>
    <section id="wrapper">
                <header id="header">

                <div id="logo">
                    <text>Anotatzailea</text>
                </div>
                        <div class="top">
                                 {% block navigation %}
                     {# Ikusi behar da ea erabiltzailea kautotu den, horren arabera aukera desberdinak erakusteko #}
                        {% if is_granted('ROLE_USER') %}
                                    <nav>
                                        <ul class="navigation">
            <li class="first current_page_item"><a href="{{ path('AnotatzaileaAnotatzaileaBundle_homepage') }}">Hasiera</a></li>
                    <li><a href="{{ path('AnotatzaileaAnotatzaileaBundle_informazioa') }}">Argibideak</a></li>
            <li><a href="{{ path('AnotatzaileaAnotatzaileaBundle_nirekontua') }}">Kontua</a></li>
            <li><a href="">Estatistikak</a></li>
            <li><a href="{{ path('AnotatzaileaAnotatzaileaBundle_testuaanotatu') }}">Anotatu</a></li>
            <li><a href="{{ path('AnotatzaileaAnotatzaileaBundle_FAQ') }}">FAQ</a></li>
            <li><a href="{{ path('AnotatzaileaAnotatzaileaBundle_kontaktatu') }}">Kontaktatu</a></li>
            <li class="last"><a href="{{ path('saioa_amaitu') }}">Irten</a></li>
                                        </ul>           
                                    </nav>
                    {% elseif is_granted('ROLE_ADMIN')%}
                        <nav>
                                        <ul class="navigation">
            <li class="first current_page_item"><a href="{{ path('AnotatzaileaAnotatzaileaBundle_homepage') }}">Hasiera</a></li>
            <li><a href="{{ path('AnotatzaileaAnotatzaileaBundle_informazioa') }}">Argibideak</a></li>
            <li><a href="{{ path('AnotatzaileaAnotatzaileaBundle_nirekontua') }}">Kontua</a></li>
            <li><a href="{{ path('AnotatzaileaAnotatzaileaBundle_dokumentuak') }}">Dokumentuak</a></li>
            <li><a href="">Erabiltzaileak</a></li>
            <li><a href="">Estatistikak</a></li>
            <li class="last"><a href="{{ path('saioa_amaitu') }}">Irten</a></li>
                        </ul>           
                                    </nav>
                    {% else %}
                        <nav>
                                        <ul class="navigation">
            <li class="first current_page_item"><a href="{{ path('AnotatzaileaAnotatzaileaBundle_homepage') }}">Hasiera</a></li>
                    <li><a href="{{ path('AnotatzaileaAnotatzaileaBundle_informazioa') }}">Argibideak</a></li>
            <li><a href="{{ path('AnotatzaileaAnotatzaileaBundle_saioahasi') }}">Sartu</a></li>
            <li><a href="{{ path('AnotatzaileaAnotatzaileaBundle_erregistratu') }}">Erregistratu</a></li>
            <li><a href="{{ path('AnotatzaileaAnotatzaileaBundle_FAQ') }}">FAQ</a></li>
            <li class="last"><a href="{{ path('AnotatzaileaAnotatzaileaBundle_kontaktatu') }}">Kontaktatu</a></li>
                                        </ul>           
                                    </nav>
                    {% endif %}                 
                                {% endblock %}
                        </div>
        </header>
        <section id="page">
                    <section class="main-col">
                        {% block body %}{% endblock %}
                </section>
                         {% if is_granted('ROLE_USER') or is_granted('ROLE_ADMIN') %}
                    <section class="sidebar">
                        {% block sidebar %}{% endblock %}
                    </section>
                         {% endif %}
            <div id="footer">
                        {% block footer %}
                <!-- Partekatu -->
                <div class="addthis_toolbox addthis_default_style addthis_32x32_style">
                    <a class="addthis_button_preferred_1"></a>
                    <a class="addthis_button_preferred_2"></a>
                    <a class="addthis_button_preferred_3"></a>
                    <a class="addthis_button_preferred_4"></a>
                    <a class="addthis_button_compact"></a>
                    <a class="addthis_counter addthis_bubble_style"></a>
                </div>
        <script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#pubid=xa-4f4e593d146466e9"></script>
                <!--------------->
                <script type="text/javascript">
                        var addthis_config = {
                        ui_language : "[eu]"   
                        }
                </script>
                                 Testua anotatzeko tresna - created by H.Salaberri</a>
                        {% endblock %}
                    </div>


            </section>

    </section>

            {% block javascripts %}


        {% endblock %}
        </body>
</html>

What I would like to do is to show the sidebar just when the identified users are logged in the application. The way I do it I just achieve not show the sidebar for anonymous users. The other problem is I don't know how to make the main-col section bigger so that the gap left by the sidebar can not be seen.


回答1:


I think you can get what you need to,easily by applying the condictions as

   {% if is_granted('ROLE_USER') or is_granted('ROLE_ADMIN') %}
                      <section class="main-col">
     {%else%} 
                      <section class="main-col">
                     {# modify the class of this section as your requirement #}
     {%endif%}

                            {% block body %}{% endblock %}
                      </section>
{% if is_granted('ROLE_USER') or is_granted('ROLE_ADMIN') %}
                      <section class="sidebar">
                            {% block sidebar %}{% endblock %}
                      </section>
{%endif%}

hope this helps



来源:https://stackoverflow.com/questions/10624433/symfony2-and-twig-sidebar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!