我的Iterator实现在Twig 1.12中不起作用

时间:2013-03-20 13:23:23

标签: symfony foreach twig

我读到foreach适用于数组或任何实现Traversable接口的对象。

由于Traversable仅仅是抽象的,我使用了Iterator。

class EPubHub_PageCollection implements Iterator
{
    protected $index0 = 0;
    protected $index = 1;
    protected $pages = array();

    public function __construct()
    {
        $this->index0 = 0;
        $this->index = 1;
        $this->pages = array();
    }

    public function updateIndex()
    {
        $this->index = $this->index0 + 1;
    }

    public function rewind()
    {
        $this->index0 = 0;
        $this->updateIndex();
    }

    public function current()
    {
        return $this->pages[$this->index0];
    }

    public function key() 
    {
        return $this->index0;
    }

    public function index() 
    {
        return $this->index;
    }

    public function next() 
    {
        ++$this->index0;
        $this->updateIndex();
    }

    public function valid() 
    {
        return isset($this->pages[$this->index0]);
    }

    public function length()
    {
        return count($this->pages);
    }

    public function prefixZero($index, $lengthOfKey = 3)
    {
        return str_pad($index, $lengthOfKey, '0', STR_PAD_LEFT);
    }

    public function getCurrentPageNumber()
    {
        $pageNumber = $this->prefixZero($this->index);
        return $pageNumber;
    }

    public function getCurrentPageFilename($pagenumber = '')
    {
        if ($pagenumber === '')
        {
            $pagenumber = $this->getCurrentPageNumber();
        }
        $pageFileName = 'page' . $pagenumber . '.xhtml' ;
        return $pageFileName;
    }

    public function getNth($index)
    {
        return $this->pages[$index - 1];
    }

    public function getCurrentPageItemId($pagenumber = '')
    {
        if ($pagenumber === '')
        {
            $pagenumber = $this->getCurrentPageNumber();
        }
        $pageItemId = 'pg' . $pagenumber;
        return $pageItemId;
    }

    public function add(EPubHub_PageInterface $page, $index = null)
    {
        if ($index === null) {
            // we just append to the pages from the end directly
            $this->pages[] = $page;
        } else {
            $this->pages = array_splice( $this->pages, $index - 1, 0, $page);
        }
    }

    /**
     *
     * delete either by page or by index
     * @param mixed $pageOrIndex If $pageOrIndex is instance of EPubHub_PageInterface
     * then delete by value. Else if integer delete by index
     */
    public function delete($pageOrIndex)
    {

        if ($pageOrIndex instanceof EPubHub_PageInterface)
        {
            $page = $pageOrIndex;
            if(($key = array_search($page, $this->pages)) !== false)
            {
                unset($this->pages[$key]);
            }
        } 

        if (is_numeric($pageOrIndex)) 
        {
            $key = $pageOrIndex;

            if((array_key_exists($key - 1, $this->pages)))
            {
                unset($this->pages[$key - 1]);
            }

        }

    }

    public function getImages()
    {
        $images = new EPubHub_ImageCollection();

        foreach($this->pages as $page)
        {
            $image = $page->getImage();
            $images->add($image);
        }
        return $images;
    }

    public function getPages()
    {
        return $this->pages;
    }


}

这个没用。

我想了解原因。我尝试运行dump(),但实例上的转储破坏了一些东西。

目前,我的解决方法是使用此类的内部数组。

我问这个问题是为了满足我的好奇心。

1 个答案:

答案 0 :(得分:0)

您需要配置调试:

services:
    acme_hello.twig.extension.debug:
        class:        Twig_Extension_Debug
        tags:
            - { name: 'twig.extension' }

请看这里:debugging。而不是:for loop in twig

例如:

{{ dump(users) }}

{% for user in users %}
    {{ user.username }}
{% endfor %}