ZF2-将变量从Module.php渲染到树枝布局 [英] ZF2 - Rendering variables from Module.php to twig layout
问题描述
我正在将ZF2网站从使用默认模板转换为树枝版本的过程中,到目前为止,主要方面,一切进展顺利,除了我在Bootstrap方法中在Module.php中定义了一些变量外我现在无法进入layout.twig模板.
I'm in the midst of converting a ZF2 site from using default templates into twig versions, and so far, for the main part, everything is going well, except I have some variables in Module.php defined from a bootstrap method that I'm now unable to get into the layout.twig template.
适用于默认模板的原始代码如下:
The original code that worked fine for the default templates is as:
public function bootstrapLayout(MvcEvent $e)
{
$config = $e->getApplication()->getServiceManager()->get('applicationconfig');
$authService = $e->getApplication()->getServiceManager()->get('AuthService');
$version = new \stdClass();
$version->major = $config['version']['major'];
$version->minor = $config['version']['minor'];
$dt = new \DateTime();
$thisYear = $dt->format('Y');
$dt->setTimestamp($config['version']['build_date']);
$version->buildDate = $dt->format('d/m/Y H:i');
$user = new \stdClass();
$user->hasIdentity = $authService->hasIdentity();
$user->username = $authService->getStorage()->read('username');
$e->getViewModel()->setVariables(array(
'version' => $version,
'thisYear' => $thisYear,
'user' => $user,
));
}
但是,版本","thisYear"或用户"都不会对树枝布局模板有所了解.我在这里进行搜索,找到的最接近的分辨率是通过以下方式获取ZfcTwigRenderer:
However, neither 'version', 'thisYear' nor 'user' make itself known to the twig layout template. I've searched on here and the closest resolution I came to finding was to obtain the ZfcTwigRenderer by:
$zfcTwigRenderer = $e->getApplication()->getServiceManager()->get('ZfcTwigRenderer');
但是现在我不知如何使用它,并将定义的var通过它传递到布局.
but now I'm at a loss how I can utilise this and pass my defined vars through it to the layout.
这是布局文件的内容:
{{ docType() }}
<html lang="en" class="no-js">
<head>
<meta charset="utf-8">
<title>{% block title %}{% endblock title %} - Website</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
{% block meta %}{% endblock meta %}
<!-- Le styles -->
<link href="/css/bootstrap.min.css" media="screen" rel="stylesheet" type="text/css">
<link href="/css/bootstrap-hidden-inline.css" media="screen" rel="stylesheet" type="text/css">
<link href="/css/theme.min.css" media="screen" rel="stylesheet" type="text/css">
<link href="/css/bootstrap-datetimepicker.min.css" media="screen" rel="stylesheet" type="text/css">
<link href="/css/style.css" media="screen" rel="stylesheet" type="text/css">
<link href="/favicon.ico" rel="shortcut icon" type="image/vnd.microsoft.icon">
{% block style %}{% endblock style %}
<!-- Scripts -->
<script type="text/javascript" src="/js/jquery.min.js"></script>
<script type="text/javascript" src="/js/bootstrap.min.js"></script>
<script type="text/javascript" src="/js/tooltip.min.js"></script>
<script type="text/javascript" src="/js/bootstrap-datetimepicker.min.js"></script>
<!--[if lt IE 9]><script type="text/javascript" src="/js/html5.js"></script><![endif]-->
<script type="text/javascript" src="/js/modernizr.min.js"></script>
<script type="text/javascript" src="/js/overlays.min.js"></script>
<script type="text/javascript" src="/js/common.min.js"></script>
{% block script %}{% endblock script %}
</head>
<body>
<div class="navbar navbar-inverse navbar-static-top">
<div class="container">
<div class="navbar-header">
{% if user.hasIdentity %}
<button class="navbar-toggle" data-target=".navbar-collapse" data-toggle="collapse" type="button">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
{% endif %}
<a class="navbar-brand" href="{{ url('home') }}">Website</a>
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<!--<li><a class="icon-home" href="{{ url('home') }}"><span></span>Home</a></li>-->
{% if user.hasIdentity %}
<li class="hidden-sm hidden-xs"><a class="icon-import" href="{{ url('import') }}"><span></span>Import</a></li>
<li class="hidden-sm hidden-xs"><a class="icon-export" href="{{ url('export') }}"><span></span>Export</a></li>
<li><a class="icon-about" href="{{ url('about') }}"><span></span>About</a></li>
<!-- <li><a class="icon-reports" href="#"><span></span>Reports</a></li> -->
<li><a class="icon-logout" href="{{ url('logout') }}"><span></span>Logout</a></li>
{% endif %}
</ul>
<ul class="nav navbar-nav navbar-right">
{% if user.hasIdentity %}
<!--<li><a class="icon-report-bug" href="{{ url('report-bug') }}"><span></span>Report bug</a></li>-->
{% endif %}
</ul>
</div>
</div>
</div>
{% if user.hasIdentity %}
<div class="container hidden-sm hidden-xs">
<div class="welcome-message"><span></span>Welcome {{ user.username }}</div>
</div>
{% endif %}
<div class="container">
{% block content %}{{ content|raw }}{% endblock content %}
<hr>
<footer>
Copyright © {{ thisYear }} Company name<br>
Version {{ version.major }}.{{ version.minor }} —
Build {{ version.buildDate }}
</footer>
</div> <!-- /container -->
{% block inline %}{% endblock inline %}
</body>
</html>
尽管在过去的几个小时中尝试了各种各样的事情,但我确实遇到了一个死胡同,因此在这方面的任何帮助将不胜感激.
Any help on this would be greatly appreciated as I've really come up against a dead end, despite trying all sorts of things for the past couple of hours.
推荐答案
对于在所有布局中都可用的真实全局变量,您需要在Module.php的onBootstrap函数中执行以下操作,因此您的示例将是
For true global variables that are available inside all layouts you need to do the following inside a onBootstrap function in Module.php so you example would be
public function bootstrapLayout(MvcEvent $e)
{
$config = $e->getApplication()->getServiceManager()->get('applicationconfig');
$authService = $e->getApplication()->getServiceManager()->get('AuthService');
$version = new \stdClass();
$version->major = $config['version']['major'];
$version->minor = $config['version']['minor'];
$dt = new \DateTime();
$thisYear = $dt->format('Y');
$dt->setTimestamp($config['version']['build_date']);
$version->buildDate = $dt->format('d/m/Y H:i');
$user = new \stdClass();
$user->hasIdentity = $authService->hasIdentity();
$user->username = $authService->getStorage()->read('username');
$enviroment = $e->getApplication()->getServiceManager()->get('ZfcTwigEnvironment');
$enviroment->addGlobal('version', $version);
$enviroment->addGlobal('thisYear', $thisYear);
$enviroment->addGlobal('user', $user);
}
然后,您将可以像上面所述那样使用上面的模板.
This would then allow you to use your above template like you have set out.
希望这会有所帮助
这篇关于ZF2-将变量从Module.php渲染到树枝布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!