在 Module.php 中访问部分内容
Access partial in Module.php
我有两个模块。
- 申请
- 用户
布局在应用程序模块中设置,在布局中有我需要显示的模块特定导航元素,这就是我正在尝试做的。
这是我在 layout/layout 应用程序模块中调用的部分。
//file: module/Application/view/layout/container/layout.phtml
<?php echo $this->partial('toolbar'); ?>
这是我的toolbar.phtml
//file: module/Application/view/layout/chunks/toolbar.phtml
<div class="new-top-menu">
<div class="navbar-header">
<a class="navbar-brand sidebar-toggle-box" href="#">
<div class="sidebar-left-trigger">
<span class="fa fa-align-right"></span>
</div>
</a>
</div>
<ul class="nav navbar-nav">
<?php echo $this->placeholder('notification/top-menu'); ?>
</ul>
<ul class="nav navbar-nav navbar-right">
<?php echo $this->placeholder('toolbar/search'); ?>
<?php echo $this->placeholder('toolbar/right-position1')->set($this->position1); ?>
<?php echo $this->placeholder('toolbar/right-position2')->set($this->position2); ?>
<?php echo $this->placeholder('toolbar/right-position3')->set($this->position3); ?>
</ul>
</div>
基本上我想从用户模块中读取另一个名为 toolbar/user-menu.phtml
的部分并将其设置为该占位符 $this->placeholder('toolbar/right-position3');
的内容
我尝试在用户模块的 Module.php 中执行此操作。
namespace User;
public function onBootstrap(MvcEvent $e)
{
$viewModel = $e->getViewModel();
$viewModel->setVariable('position1', 'test string');
}
这绝对没问题,问题是我想用位于 file: module/User/view/toolbar/user-menu.phtml
的部分内容替换内容 'test string'
欢迎任何有关如何在 ZF2 中完成此操作的指示。
谢谢。
更新 1:
我能够使用下面的代码做到这一点,无论如何,我很期待这是否可以改进。
在 User/Module.php 中,我更新了以下内容。
use Zend\View\Renderer\PhpRenderer;
use Zend\View\Resolver;
public function onBootstrap(MvcEvent $event)
{
$application = $event->getApplication();
$eventManager = $application->getEventManager();
$serviceManager = $application->getServiceManager();
//create a Template Stack
$stack = new Resolver\TemplatePathStack(array('script_paths' => array(__DIR__ . '/view/user/toolbar')));
//create a Resolver
$resolver = new Resolver\AggregateResolver();
$resolver->attach($stack);
//create a Renderer
$renderer = new PhpRenderer();
$renderer->setResolver($resolver);
$viewHelperManager = $serviceManager->get('ViewHelperManager');
$urlViewHelper = $viewHelperManager->get('url');
$partial = $renderer->partial('user-menu', array(
'signOutUrl' => $urlViewHelper('user-account-sign-out')
));
$viewModel = $event->getViewModel();
$viewModel->setVariable('position1', $partial);
}
就是这样,我现在可以将 HTML 内容放在占位符中,我现在面临的唯一问题是访问 user-menu.phtml 文件中的视图助手,我是当前在 Module.php 中生成它并将其传递给视图文件。
我认为您走在正确的轨道上,因为您开始关注本机视图助手,但您不应该直接在模块中操作它们,也不应该尝试更改组合工具栏在您的视图脚本中。
相反,制作您自己的工具栏视图助手,通过其工厂(包括部分模板)注入依赖项,然后您可以简单地 echo $this->toolbar()
在您的布局视图脚本中。
此外,如果没有别的,您可以看看 Spiffy Navigation,了解视图助手的实现方式。 https://github.com/spiffyjr/spiffy-navigation
我有两个模块。
- 申请
- 用户
布局在应用程序模块中设置,在布局中有我需要显示的模块特定导航元素,这就是我正在尝试做的。
这是我在 layout/layout 应用程序模块中调用的部分。
//file: module/Application/view/layout/container/layout.phtml
<?php echo $this->partial('toolbar'); ?>
这是我的toolbar.phtml
//file: module/Application/view/layout/chunks/toolbar.phtml
<div class="new-top-menu">
<div class="navbar-header">
<a class="navbar-brand sidebar-toggle-box" href="#">
<div class="sidebar-left-trigger">
<span class="fa fa-align-right"></span>
</div>
</a>
</div>
<ul class="nav navbar-nav">
<?php echo $this->placeholder('notification/top-menu'); ?>
</ul>
<ul class="nav navbar-nav navbar-right">
<?php echo $this->placeholder('toolbar/search'); ?>
<?php echo $this->placeholder('toolbar/right-position1')->set($this->position1); ?>
<?php echo $this->placeholder('toolbar/right-position2')->set($this->position2); ?>
<?php echo $this->placeholder('toolbar/right-position3')->set($this->position3); ?>
</ul>
</div>
基本上我想从用户模块中读取另一个名为 toolbar/user-menu.phtml
的部分并将其设置为该占位符 $this->placeholder('toolbar/right-position3');
我尝试在用户模块的 Module.php 中执行此操作。
namespace User;
public function onBootstrap(MvcEvent $e)
{
$viewModel = $e->getViewModel();
$viewModel->setVariable('position1', 'test string');
}
这绝对没问题,问题是我想用位于 file: module/User/view/toolbar/user-menu.phtml
欢迎任何有关如何在 ZF2 中完成此操作的指示。
谢谢。
更新 1:
我能够使用下面的代码做到这一点,无论如何,我很期待这是否可以改进。
在 User/Module.php 中,我更新了以下内容。
use Zend\View\Renderer\PhpRenderer;
use Zend\View\Resolver;
public function onBootstrap(MvcEvent $event)
{
$application = $event->getApplication();
$eventManager = $application->getEventManager();
$serviceManager = $application->getServiceManager();
//create a Template Stack
$stack = new Resolver\TemplatePathStack(array('script_paths' => array(__DIR__ . '/view/user/toolbar')));
//create a Resolver
$resolver = new Resolver\AggregateResolver();
$resolver->attach($stack);
//create a Renderer
$renderer = new PhpRenderer();
$renderer->setResolver($resolver);
$viewHelperManager = $serviceManager->get('ViewHelperManager');
$urlViewHelper = $viewHelperManager->get('url');
$partial = $renderer->partial('user-menu', array(
'signOutUrl' => $urlViewHelper('user-account-sign-out')
));
$viewModel = $event->getViewModel();
$viewModel->setVariable('position1', $partial);
}
就是这样,我现在可以将 HTML 内容放在占位符中,我现在面临的唯一问题是访问 user-menu.phtml 文件中的视图助手,我是当前在 Module.php 中生成它并将其传递给视图文件。
我认为您走在正确的轨道上,因为您开始关注本机视图助手,但您不应该直接在模块中操作它们,也不应该尝试更改组合工具栏在您的视图脚本中。
相反,制作您自己的工具栏视图助手,通过其工厂(包括部分模板)注入依赖项,然后您可以简单地 echo $this->toolbar()
在您的布局视图脚本中。
此外,如果没有别的,您可以看看 Spiffy Navigation,了解视图助手的实现方式。 https://github.com/spiffyjr/spiffy-navigation