View 在Symfony 1.4中创建动态侧栏部分的最佳方法
我有几个前端模块有自己的侧边栏菜单链接。我想在模块的actions类中创建这些链接:View 在Symfony 1.4中创建动态侧栏部分的最佳方法,view,symfony1,symfony-1.4,View,Symfony1,Symfony 1.4,我有几个前端模块有自己的侧边栏菜单链接。我想在模块的actions类中创建这些链接: public function preExecute() { $items['plan/new'] = 'Create Plan'; $items['plan/index'] = 'Plans Listing'; $this->getResponse()->setSlot('sidebar', $items); } Slot文件sidebar.php #
public function preExecute()
{
$items['plan/new'] = 'Create Plan';
$items['plan/index'] = 'Plans Listing';
$this->getResponse()->setSlot('sidebar', $items);
}
Slot文件sidebar.php
#apps/frontend/templates/sidebar.php
<?php slot('sidebar') ?>
<ul>
<?php foreach($items as $url => $title) : ?>
<li><?php echo link_to($url, $title) ?></li>
<?php endforeach ?>
</ul>
<?php end_slot() ?>
#apps/frontend/templates/sidebar.php
layout.php:
<?php if (has_slot('sidebar')): ?>
<div id="sidebar"><?php include_slot('sidebar') ?></div>
<?php endif ?>
但我的输出是数组,如何渲染插槽?您似乎在混合插槽和分区。在操作中,将插槽设置为数组,然后调用
include\u slot
,字符串表示形式为array
,这是正确的
您应该通过$this->items=$items
传递项目,然后在您的操作中查看isset($items)
是否为true,并在必要时调用include_partial(“侧边栏”,数组(“items”=>$items))
。这将查找名为\u sidebar.php的文件
有关这些东西如何工作的更多详细信息,请阅读sf1.4手册的部分。如果您使用主视图(layout.php),请参阅:$this->sidebar_menu=$items;只是不起作用($sidebar_menu在layout/php中总是未定义):/似乎您需要一个部分,但在逻辑上,使用一个组件,这正是它们的用途。