2010-02-15 11 views
2

我試圖從完全不同的角度攻擊this problem,因爲它看起來不像我能以這種方式實現我的目標。在Zend HeadScript視圖助手中修改堆棧

我要循環的項目組在HeadScript View Helper,並進行修改它。 The documentation,這和其他一些視圖助手使得這樣的說法:

用HeadScript覆蓋各追加(), offsetSet(),前置()和()設置的,以 執行的特殊方法 使用如上所列。在內部,它將 中的每個項存儲爲stdClass標記,它後來使用 itemToString()方法對其進行序列化。這使 您在 堆棧上的項目進行檢查,並可選通過簡單修改 返回的對象修改這些 項目。

所以,這是哪裏 「對象返回」?我在這裏錯過了一塊難題。

感謝您的幫助!

回答

4

Zend_View_Helper_HeadScripttoString()方法,我注意到一個foreach()環路上$this,所以我想這和它的工作。這裏有一個用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(); ?> 

如果我的解決辦法是在任何方面不清楚,讓我知道,我會更新,以澄清。

+0

你如何使用它?我試圖讓這個工作http://blog.hines57.com/2011/03/13/zendframework-minify/只需要改變一個像在佈局echo $ this-> miniHeadScript() – max4ever 2011-05-03 14:29:25

+0

@ max4ever - 我在解決方案中添加了更多細節。它重載了現有的'$ this-> headScript()'調用。更改爲'miniHeadScript()'是另一步。 – Sonny 2011-05-03 15:03:14

+0

我不知道爲什麼,但foreach($ this ...)總是跳過我的配置,我發現解決方案是,遍歷$ this-> view-> headLink();我在博客上發佈了一些評論http://blog.hines57.com/2011/03/13/zendframework-minify/comment-page-1/#comment-353 – max4ever 2011-05-03 15:59:43