Zend Framework 中布局所需的代码放在哪里?

发布于 2024-09-16 15:43:44 字数 214 浏览 12 评论 0原文

我使用 Zend_Layout 作为 Zend Framework 应用程序的布局。很简单,但是我在布局中还需要做一些操作。目前,代码包含在 PHP 括号之间,但我觉得这并不是最干净的方法。

我看到人们使用插件,但我认为这不是我想要的方式。显然,我可以提取“复杂”部分,以良好的操作/控制器方式进行操作,并在布局中使用占位符。这是要走的路吗?您是否有此类示例(例如在其自己的操作中委托的导航菜单)?

I am using Zend_Layout for the layout of my Zend Framework application. It is very simple, but I still have a few operations that I need to do in the layout. For now, the code is included between PHP brackets, but I feel like this is not really the cleanest way to go.

I saw people using plugins, but I don't think this is the way I want to go. Oviously, I could extract the "complicated" part, do it in a nice action/controller manner, and use a placeholder in the layout. Is this the way to go ? Do you have examples of such things (for instance the navigation menu delegated in its own action) ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美人骨 2024-09-23 15:43:44

navigation() 是占位符视图助手的具体实现。

您可以创建相同的占位符实现,或使用

  • include('myhtml.phtml');
  • $this->render('script.phtml');
  • 部分
  • partialLoops
  • 占位符
  • 视图助手

这取决于您需要什么。

正如您可能已经听说过的那样,动作堆栈是邪恶的。

navigation() is a concrete implementation of placeholder view helper.

You may create the same placeholder implementation, or use

  • include('myhtml.phtml');
  • $this->render('script.phtml');
  • partials
  • partialLoops
  • placeholders
  • view helpers

It depends what do you need.

Action stack is evil, as you probably already heard.

心在旅行 2024-09-23 15:43:44

正如 Takehin 所说,navigation() 占位符可能是正确的选择。但是,我不想针对这种特殊情况进行 Zend_Navigation 的设置,因此我使用了 自定义视图助手

application/views/helpers/Menu.php

<?php

class Zend_View_Helper_Menu extends Zend_View_Helper_Abstract {

    public function menu() {
        // my code ...
        $this->view->menu = $menu;

        return $this->view->render('helpers/menu.phtml');
    }

}

application/views/scripts/helpers/menu.phtml

<ul>
    <?php foreach ($this->menu as $item) { // print menu } ?>
</ul>

这样,我可以简单地从我的布局中调用这个助手:

...
<div id="menu">
    <?php echo $this->menu(); ?>
</div>
...

As takeshin said, the navigation() placeholder is probably the way to go. However, I did not want to go through the setup of Zend_Navigation for this particular case, so I used a custom view helper.

application/views/helpers/Menu.php :

<?php

class Zend_View_Helper_Menu extends Zend_View_Helper_Abstract {

    public function menu() {
        // my code ...
        $this->view->menu = $menu;

        return $this->view->render('helpers/menu.phtml');
    }

}

application/views/scripts/helpers/menu.phtml :

<ul>
    <?php foreach ($this->menu as $item) { // print menu } ?>
</ul>

This way, I can simply call this helper from my layout :

...
<div id="menu">
    <?php echo $this->menu(); ?>
</div>
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文