在 Zend HeadScript 视图助手中修改堆栈

发布于 2024-08-21 14:39:27 字数 804 浏览 11 评论 0原文

我正在尝试解决这个问题一个完全不同的角度,因为看起来我无法通过这种方式实现我的目标。

我想循环 HeadScript View Helper,并对其进行修改。 文档为此和其他一些视图助手做出了这样的声明:

HeadScript 会覆盖每个append(), offsetSet()、prepend() 和 set() 为 强制使用特殊方法 如上所列。在内部,它存储 每个项目作为一个 stdClass 令牌,其中 它稍后使用序列化 itemToString() 方法。这允许 您对以下项目进行检查 堆栈,并可选择修改这些 通过简单地修改对象来修改项目 返回。

那么,这个“返回的对象”在哪里呢?我在这里遗漏了一块拼图。

感谢您的帮助!

I am trying to attack this problem from a completely different angle, because it doesn't look like I can achieve my goal that way.

I want to loop over the item stack in the HeadScript View Helper, and make modifications to it. The documentation for this and some of the other view helpers makes this statement:

HeadScript overrides each of append(),
offsetSet(), prepend(), and set() to
enforce usage of the special methods
as listed above. Internally, it stores
each item as a stdClass token, which
it later serializes using the
itemToString() method. This allows
you to perform checks on the items in
the stack, and optionally modify these
items by simply modifying the object
returned.

So, where is this "object returned"? I am missing a piece of the puzzle here.

Thanks for your help!

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

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

发布评论

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

评论(1

ペ泪落弦音 2024-08-28 14:39:27

Zend_View_Helper_HeadScripttoString() 方法中,我注意到 $this 上有一个 foreach() 循环,所以我尝试了这确实有效。这是我编写的 HeadScript 扩展,它说明了解决方案:

class My_View_Helper_HeadScript extends Zend_View_Helper_HeadScript
{
    public function toString($indent = null)
    {
        $files = array();
        foreach ($this as $key => $item) {
            if (!empty($item->attributes)
                    && array_key_exists('src', $item->attributes)
                    && ('scripts' == substr($item->attributes['src'], 1, 7))) {
                $files[] = $item->attributes['src'];
                unset($this[$key]);
            }
        }
        if (0 < count($files)) {
            $this->prependFile('/combo.php?type=scripts&files=' . implode(',', $files));
        }
        return parent::toString($indent);
    }
}

Bootstrap.php 中,以下几行指向我的助手:

$this->bootstrap('view');
$view = $this->getResource('view');
$view->addHelperPath('My/View/Helper', 'My_View_Helper');

在我的布局中,我有这一行:

<?php echo $this->headScript(); ?>

如果我的解决方案在任何方面都不清楚,让我知道,我会更新以澄清。

In the toString() method of Zend_View_Helper_HeadScript I noticed a foreach() loop on $this, so I tried that and it worked. Here's a HeadScript extension I wrote that illustrates the solution:

class My_View_Helper_HeadScript extends Zend_View_Helper_HeadScript
{
    public function toString($indent = null)
    {
        $files = array();
        foreach ($this as $key => $item) {
            if (!empty($item->attributes)
                    && array_key_exists('src', $item->attributes)
                    && ('scripts' == substr($item->attributes['src'], 1, 7))) {
                $files[] = $item->attributes['src'];
                unset($this[$key]);
            }
        }
        if (0 < count($files)) {
            $this->prependFile('/combo.php?type=scripts&files=' . implode(',', $files));
        }
        return parent::toString($indent);
    }
}

In Bootstrap.php the following lines to point to my helpers:

$this->bootstrap('view');
$view = $this->getResource('view');
$view->addHelperPath('My/View/Helper', 'My_View_Helper');

In my layout, I have this line:

<?php echo $this->headScript(); ?>

If my solution is unclear in any way, let me know and I'll update it to clarify.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文