Zend framework2 Zend Framework 2在一个布局中包含两个模板?
在我的应用程序的每个模块中,我都有一个主内容部分和一个侧栏菜单 在我的布局中,我有以下内容Zend framework2 Zend Framework 2在一个布局中包含两个模板?,zend-framework2,Zend Framework2,在我的应用程序的每个模块中,我都有一个主内容部分和一个侧栏菜单 在我的布局中,我有以下内容 <div id="main" class="span8 listings"> <?php echo $this->content; ?> </div> <div id="sidebar" class="span4"> <?php echo $this->sidebar; ?> </div> 我觉得我在这
<div id="main" class="span8 listings">
<?php echo $this->content; ?>
</div>
<div id="sidebar" class="span4">
<?php echo $this->sidebar; ?>
</div>
我觉得我在这里做了一些根本错误的事情。您可以使用
在您可以使用的控制器中,以及: 现在,您只需在布局脚本中回显变量:
<?php
// 'sidebar' here is the same passed as the second parameter to addChild() method
echo $this->sidebar;
?>
//Module.php添加它是
use Zend\View\Model\ViewModel;
public function onBootstrap($e)
{
$app = $e->getParam('application');
$app->getEventManager()->attach('dispatch', array($this, 'setLayout'));
}
public function setLayout($e)
{
// IF only for this module
$matches = $e->getRouteMatch();
$controller = $matches->getParam('controller');
if (false === strpos($controller, __NAMESPACE__)) {
// not a controller from this module
return;
}
// END IF
// Set the layout template
$template = $e->getViewModel();
$footer = new ViewModel(array('article' => "Dranzers"));
$footer->setTemplate('album/album/footer');
$template->addChild($footer, 'sidebar');
}
谢谢你,布拉姆。但是,这是可行的,侧边栏是站点范围的,但是上面的解决方案只有在添加到每个特定视图时才起作用。
侧边栏.phtml
partial在布局中呈现,因此它是侧边栏。参数是特定于操作的,但是如果您不需要在侧边栏中执行任何特定于操作的逻辑,则不必传递该参数。谢谢Josias!正是我需要的!
public function fooAction()
{
// Sidebar content
$content = array(
'name' => 'John'
'lastname' => 'Doe'
);
// Create a model for the sidebar
$sideBarModel = new Zend\View\Model\ViewModel($content);
// Set the sidebar template
$sideBarModel->setTemplate('my-module/my-controller/sidebar');
// layout plugin returns the layout model instance
// First parameter must be a model instance
// and the second is the variable name you want to capture the content
$this->layout()->addChild($sideBarModel, 'sidebar');
// ...
}
<?php
// 'sidebar' here is the same passed as the second parameter to addChild() method
echo $this->sidebar;
?>
use Zend\View\Model\ViewModel;
public function onBootstrap($e)
{
$app = $e->getParam('application');
$app->getEventManager()->attach('dispatch', array($this, 'setLayout'));
}
public function setLayout($e)
{
// IF only for this module
$matches = $e->getRouteMatch();
$controller = $matches->getParam('controller');
if (false === strpos($controller, __NAMESPACE__)) {
// not a controller from this module
return;
}
// END IF
// Set the layout template
$template = $e->getViewModel();
$footer = new ViewModel(array('article' => "Dranzers"));
$footer->setTemplate('album/album/footer');
$template->addChild($footer, 'sidebar');
}