2011-12-18 48 views
0

我有几个前端模块,它有自己的边栏菜单链接。我想创建一个类模块的操作这些链接:在Symfony 1.4中创建动态边栏部分的最佳方法

public function preExecute() 
    { 
    $items['plan/new'] = 'Create Plan'; 
    $items['plan/index'] = 'Plans Listing'; 
    $this->getResponse()->setSlot('sidebar', $items); 
    } 

插槽文件的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() ?> 

layout.php中:

<?php if (has_slot('sidebar')): ?> 
     <div id="sidebar"><?php include_slot('sidebar') ?></div> 
    <?php endif ?> 

,但我的输出为阵,怎么能我渲染我的插槽?

回答

2

您似乎在混合插槽和部分。在您的操作中,您将您的槽设置为阵列,稍后您拨打include_slot,字符串表示形式为Array,这是正确的。

您应通过$this->items = $items传递项目,然后在您的操作中查看isset($items)是否为真,如果有必要请致电include_partial("sidebar", array("items" => $items))。这将查找名为_sidebar.php的文件。

有关这些东西如何工作的更多详细信息,请阅读sf1.4书的Inside the View Layer: Code fragments部分。

+0

如果您使用主视图(layout.php):$ this-> sidebar_menu = $ items;只是不起作用($ sidebar_menu总是在layout/php中未定义):/ – drupality 2011-12-18 14:06:05

+1

看起来像你想要一个部分但有逻辑,使用一个组件,这正是他们的目的。 – Maerlyn 2011-12-18 17:22:19