如何在symfony布局中填充侧边栏内容?

时间:2010-11-22 15:09:07

标签: php symfony1

我正在使用Symfony构建一个简单的应用程序。页面布局由带有左侧和右侧边栏的主体组成。侧边栏包含几个用户可配置的模块。

Symfony提供的插槽似乎是填充侧边栏的正确方法:

layout.php中

<div id="left_sidebar">
<?php if (has_slot('left_sidebar')): ?>
<ul>
  <?php include_slot('left_sidebar') ?>
</ul>
<?php else: ?>
  <!-- default sidebar code -->
<?php endif; ?>
</div>

要填充我尝试使用过滤器的插槽。问题是侧边栏中的某些模块取决于操作中发生的事情(类别更新等)。所以它们应该在动作完成后生成。

msBootstrapFilter

class msBootstrapFilter extends sfFilter
{

   public function execute($filterChain)
   {
        // Generating the sidebars at this point is TOO early
        // as the content of some sidebars depends on the actions

    // Execute next filter
    $filterChain->execute();

    // Generate the sidebars after running through all the code
        // This is TOO LATE, the layout has been rendered

    $this->generateSidebars();          
   }
}

我不想为每个动作添加“运行边栏”调用,因为这似乎不灵活。

Symfony事件流中生成侧边栏内容的最佳点是什么?是否有适合我可以连接的活动?

1 个答案:

答案 0 :(得分:3)

你可以使用一个组件吗?这基本上是一个附加某种动作的插槽。在你的行动中,你可以做任何你的逻辑需要,并以与上面相同的方式呈现它,但有更多的逻辑。

来自manual

  

组件就像一个动作,除了   它快得多。一个逻辑   组件保存在一个类中   继承自sfComponents,位于   在actions / components.class.php中   文件。它的介绍保存在一个   部分。 sfComponents的方法   class以execute执行开头,   就像行动一样,他们可以通过   他们演示的变量   对应的方式与此相同   动作可以传递变量。