Closured Iterator: The Secret While Twig Tag

Lately I was wanting to do a while loop on twig and did not know what to do. The need arose when developing a site with twig and wordpress. In order to overcome the difficulty I had to stand on the shoulders of @Seldaek and @igorw from the symfony dev community.

Explanation: Iterators are an extension of php which is packaged with SPL and is ready to use for php newbies (me). With this you can extend Iterator interface and you will gain iteration powers. The idea was that with these powers we could just pass the object to let it iterate as if it were a for loop. Our template would look like this already transformed:

{% for post in whilePosts %}
    // display post contents, title, etc.
{% endfor %}

Our approach will allow us to set every variable upon calling the_post and stuff. WordPress needs this else it goes crazy. If you happen to install say a plugin and try to just use objects and their properties without the magic of their wordpress hooks and stuff you are left on your own. This is the justification why implementing an iterator as follows.

The code that does the marvel follows. We basically inject two closures for valid and current into Iterator constructor and call their callable values upon return of each of the implemented methods.

    /* create iterator for loops */
    $whilePosts = new whileIterator(
                                    function() { return have_posts(); },
                                    function() { the_post(); return true; }
class WhileIterator implements Iterator {
    public function __construct($valid, $current) {
        $this->validClosure = $valid;
        $this->currentClosure = $current;
    public function valid() {
        return call_user_func($this->validClosure);
    public function current() {
        return call_user_func($this->currentClosure);
    public function next() {}
    public function rewind() {}
    public function key() {}

Edifying version of quote from Philippians 1:7-11 MSG follows:

7-8It’s not at all fanciful for me to think this way about you. My prayers and hopes have deep roots in reality. You have, after all, stuck with me all the way from the time I was thrown in jail, put on trial, and came out of it in one piece. All along you have experienced with me the most generous help from God. He knows how much I love and miss you these days. Sometimes I think I feel as strongly about you as Christ does!

9-11So this is my prayer: that your love will flourish and that you will not only love much but well. Learn to love appropriately. You need to use your head and test your feelings so that your love is sincere and intelligent, not sentimental gush. Live a lover’s life, circumspect and exemplary, a life Jesus will be proud of: bountiful in fruits from the soul, making Jesus Christ attractive to all, getting everyone involved in the glory and praise of God.

Leave a Reply

Your email address will not be published. Required fields are marked *