php5中Iterator與smarty整合 – 馬永占 譯

來源:互聯網
上載者:User
著作權聲明:原創作品,允許轉載,轉載時請務必以超連結形式標明文章原始出版、作者資訊和本聲明。否則將追究法律責任。http://blog.csdn.net/mayongzhan - 馬永占,myz,mayongzhan

php5中Iterator與smarty整合

Iterator(迭代器)在PHP5中是非常重要的,我注意到Iterator在Smarty中不能正常的工作。
Smarty會自動將一個object(對象)轉換成array(數組),所以當年在Smarty中迴圈輸出一個object時,模板會自動迴圈這個object的屬性。
例如,建立一個類,然後在函數中定義某些要迴圈的部分,將這些部分放到protected類型的$_data變數中。
<?php
    class MyClass implements Iterator
    {
        protected $_data = array();
 
        public function rewind()
        {
            reset($this->_data);
        }
 
        public function current()
        {
            return current($this->_data);
        }
 
        public function key()
        {
            return key($this->_data);
        }
 
        public function next()
        {
            return next($this->_data);
        }
 
        public function valid()
        {
            return $this->current() !== false;
        }
 
        public function size()
        {
            return count($this->_data);
        }
    }
?>
然後在Smarty使用這個類
{foreach from=$myClassObj item=row}
    {$row}
{/foreach}
這樣不會輸出想要的結果,下面做一下簡單的修改,將$myClassObj改成$myClassObj->getData():
{foreach from=$myClassObj->getData() item=row}
    {$row}
{/foreach}
這樣就能輸出正確的結果了。

 

 

phpRiot.com: Using the PHP 5 Iterator interface with Smarty

The PHP 5 Iterator interface is very useful for defining custom behaviour for looping over objects, however I just noticed that looping over such objects in Smarty will not work correctly.

Smarty will in fact cast an object back to an array, so when you loop over it, the template will loop over the object's properties (as opposed to using the rules defined by the Iterator methods).

For example, I used code similar to the following to create a class over which I can loop. This lets me loop over the protected $_data array.

<?php
    class MyClass implements Iterator
    {
        protected $_data = array();
 
        public function rewind()
        {
            reset($this->_data);
        }
 
        public function current()
        {
            return current($this->_data);
        }
 
        public function key()
        {
            return key($this->_data);
        }
 
        public function next()
        {
            return next($this->_data);
        }
 
        public function valid()
        {
            return $this->current() !== false;
        }
 
        public function size()
        {
            return count($this->_data);
        }
    }
?>

What I want to be able to do is loop over each element in the array with the following code:

{foreach from=$myClassObj item=row}
    {$row}
{/foreach}

However, this will not work as expected in Smarty. To get around, I implemented as method called getData() which simply returned the array. This meant my template had to look as follows:

{foreach from=$myClassObj->getData() item=row}
    {$row}
{/foreach}

It's a bit annoying to have to do this, since in some ways it defeats the purpose of the using the Iterator interface, but I guess sometimes small sacrifices need to be made.

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.