如何将var从控制器传递到父模板(base.html.twig)? 我已经在我的控制器中有一个返回语句到一个名为login的页面,并尝试用我的var添加另一个return语句,如下所示:
$session = $request->getSession();
return $this->render('login.html.twig');
return $this->render('base.html.twig', array('session'=>$session));
但是当我重新加载页面时,我收到错误,该会话未定义。
我希望在登录后使用会话参数进行调整导航。 在此先感谢,希望有人可以提供帮助:)
更新: 这是我的模板:
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Die 3 Meta-Tags oben *müssen* zuerst im head stehen; jeglicher sonstiger head-Inhalt muss *nach* diesen Tags kommen -->
<meta name="description" content="">
<meta name="author" content="">
<title>{% block title %}Welcome{% endblock %}</title>
<!-- Bootstrap-CSS -->
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<link href="{{ asset('css/grund.css')}}" rel="stylesheet">
{% block stylesheets %}{% endblock %}
<link rel="icon" type="image/x-icon" href="{{ asset('favicon.ico') }}" />
<!-- Nur für Testzwecke. Kopiere diese Zeilen nicht in echte Projekte! -->
<!--[if lt IE 9]><script src="../../assets/js/ie8-responsive-file-warning.js"></script><![endif]-->
<script src="../../assets/js/ie-emulation-modes-warning.js"></script>
<!-- Unterstützung für Media Queries und HTML5-Elemente in IE8 über HTML5 shim und Respond.js -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Navigation ein-/ausblenden</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="{{path('homepage')}}">Test-Projekt</a>
</div>
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
{% if session is null %}
<li><a href="{{path('homepage')}}">Home</a></li>
<li><a href="{{path('anmelden')}}">Anmelden</a></li>
<li><a href="{{path('logout')}}">Abmelden</a></li>
{{% endif %}}
</ul>
</div><!--/.nav-collapse -->
</div>
</nav>
<div class="container">
<div class="row">
<div class="col-md-12">
{% block body %}{% endblock %}
</div>
</div>
</div>
{% block javascripts %}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
{% endblock %}
<!-- Bootstrap-JavaScript
================================================== -->
<!-- Am Ende des Dokuments platziert, damit Seiten schneller laden -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="../../dist/js/bootstrap.min.js"></script>
<!-- IE10-Anzeigefenster-Hack für Fehler auf Surface und Desktop-Windows-8 -->
<script src="../../assets/js/ie10-viewport-bug-workaround.js"></script>
</body>
</html>
这是我的login.html.twig
{% extends 'base.html.twig' %}
{% block title %}Anmeldung{% endblock %}
{% block body %}
<h1 class="page-header">Anmeldung</h1>
{% if error %}
<div>{{ error.messageKey|trans(error.messageData, 'security')}}</div>
{% endif %}
<div class='well' id='logindiv'>
<form action="{{ path('anmelden') }}" method="post" class="form-horizontal">
<div class="form-group">
<label for="_username" class="control-label" id="lblname">Benutzername:</label>
<input type="text" id="username" name="_username" />
</div>
<div class="form-group">
<label for="_password" class="control-label" id="lblpwd">Passwort:</label>
<input type="password" id="passwort" name="_password" />
</div>
<input type="hidden" value="/homepage" name="_target_path" />
<button type="submit" class="btn btn-default">anmelden</button>
</form>
</div>
{% endblock %}
答案 0 :(得分:1)
你错过了一个背叛者,看:
return $this->render('base.html.twig, array('session'=>$session));
与
相比return $this->render('base.html.twig', array('session'=>$session));