简体   繁体   English

Symfony2和Twig侧栏

[英]Symfony2 and Twig sidebar

I am developing an application using Symfony2. 我正在使用Symfony2开发应用程序。 I am using Twig for templating an a 3 level interface architecture. 我正在使用Twig来模板化3级接口体系结构。 In my application there are 3 kind of users, anonimous users, udentified users and Administrators. 在我的应用程序中,有3种用户,匿名用户,无身份用户和管理员。 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. 另一个问题是我不知道如何使main-col部分变大,以致于无法看到侧边栏留下的间隙。

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 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM