Symfony2 和 Twig 侧边栏
[英] Symfony2 and Twig sidebar
本文介绍了Symfony2 和 Twig 侧边栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 Symfony2 开发应用程序.我正在使用 Twig 来模板化一个 3 级界面架构.在我的应用程序中有 3 种用户,匿名用户、身份不明的用户和管理员.页面以这种方式划分为多个部分:
<!DOCTYPE html><头><meta http-equiv="content-type" content="text/html; charset=utf-8"/><title>{% block title %}Anotatzailea{% endblock %} - Anotatzaila</title><!--[如果是 IE 9]><script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script><![endif]-->{% 块样式表 %}<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"/>{% 结束块 %}<link rel="shortcut icon" href="{{ asset('favicon.ico') }}"/>头部><身体><section id="包装器"><header id="header"><div id="标志"><text>Anotatzailea</text>
<div class="top">{% 块导航 %}{# Ikusi behar da ea erabiltzaila kautotu den, horren arabera aukera desberdinak erakusteko #}{% if is_granted('ROLE_USER') %}<导航><ul class="导航"><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_testuaaotatu') }}">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></nav>{% elseif is_granted('ROLE_ADMIN')%}<导航><ul class="导航"><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></nav>{% 别的 %}<导航><ul class="导航"><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></nav>{% 万一 %}{% 结束块 %}
</标题><section id="页面"><section class="main-col">{% 块体 %}{% 端块 %}</节>{% if is_granted('ROLE_USER') 或 is_granted('ROLE_ADMIN') %}<section class="sidebar">{% block sidebar %}{% endblock %}</节>{% 万一 %}<div id="页脚">{% 块页脚 %}<!-- 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>
<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 : "[欧盟]"}Testua anotatzeko tresna - 由 H.Salaberri </a> 创建{% 结束块 %}
</节></节>{% 阻止 javascripts %}{% 结束块 %}</html>
我想做的是在识别出的用户登录应用程序时显示侧边栏.我这样做的方式只是实现了不为匿名用户显示侧边栏.另一个问题是我不知道如何将主列部分变大,以便看不到侧边栏留下的间隙.
解决方案
我认为您可以通过应用条件轻松获得所需的内容
{% if is_granted('ROLE_USER') 或 is_granted('ROLE_ADMIN') %}<section class="main-col">{%别的%}<section class="main-col">{# 根据需要修改本节的类#}{%万一%}{% 块体 %}{% 端块 %}</节>{% if is_granted('ROLE_USER') 或 is_granted('ROLE_ADMIN') %}<section class="sidebar">{% block sidebar %}{% endblock %}</节>{%万一%}
希望能帮到你
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.
解决方案
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
这篇关于Symfony2 和 Twig 侧边栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!