简体   繁体   中英

Can I write a Twig Extension to access previous and next element in a loop

I was wondering if there is a way to create functions (or whatever) in Twig, so I can access the next and the previous element in a for loop. Something like this:

{% for i in items %}

    {% if i == previous() %}
        <p>yes</p>
    {% endif %}

{% endfor %}

Update

The goal is that I have a lot of checks like

if current.name == prev.name 
    do somethig 
else 
    do another thing 

and the same with next

The problem became bigger after I wrote a sorting filter, bacause now

{% set items = allitems|sortbyname %}

{% for item in items %}
    {{ item.name }}
{% endfor %}

Here the items are in sorted order

and

{% for item in items %}
    {{ items[loop.index0].name }}
{% endfor %}

Here they are not

so I can't use something like:

if item.name == items[ loop.index0 + 1 ].name for accessing the next element

I can't figure out how to overcome these problems :( can you help me please?

There is no such thing as previous() in twig.
You can take a look at loop variables

A workaround in your case, is to build a custom iterator.
Here's an example

/**
 * Previous Next Iterator adds two methods to the ArrayIterator
 *
 *  -> getPreviousElement() To get the previous element of the iterator
 *  -> getNextElement()     To get the next element of the iterator
 *
 * These methods will not affect the internal pointer
 */
class PreviousNextIterator extends ArrayIterator
{
    protected $indexKeys = array();
    protected $keyIndexs = array();
    protected $elements  = array();
    protected $dirty     = true;

    /**
     * Constructor
     *
     * @param array   $array Input Array
     * @param integer $flags Flags
     */
    public function __construct($array = array(), $flags = 0)
    {
        parent::__construct($array, $flags);

        $this->load();
    }

    /**
     * Helper class to self create from an ArrayIterator
     *
     * @param  ArrayIterator        $iterator ArrayIterator to fetch
     * @return PreviousNextIterator New self instance
     */
    public static function createFromIterator(ArrayIterator $iterator)
    {
        return new self($iterator->getArrayCopy());
    }

    /**
     * Get the previous element of the iterator
     *
     * @return mixed Previous element
     */
    public function getPreviousElement()
    {
        $index = $this->getIndexKey($this->key());

        if (--$index < 0) {
            return;
        }

        $key = $this->getKeyIndex($index);

        return $this->elements[$key];
    }

    /**
     * Get the next element of the iterator
     *
     * @return mixed Next element
     */
    public function getNextElement()
    {
        $index = $this->getIndexKey($this->key());

        if (++$index >= $this->count()) {
            return;
        }

        $key = $this->getKeyIndex($index);

        return $this->elements[$key];
    }

    /**
     * Loads up the keys
     *
     * $this->elements
     *     Contains the copy of the iterator array
     *     Eg: [ 'a' => $fooInstance1, 'b' => $fooInstance2 ...]
     *
     * $this->keyIndexs
     *     Contains the keys indexed numerically
     *     Eg: [ 0 => 'a', 1 => 'b' ...]
     *
     * $this->indexKeys
     *     Contains the indexes of the keys
     *     Eg: [ 'a' => 0, 'b' => 1 ...]
     */
    protected function load()
    {
        if (!$this->isDirty()) {
            return;
        }

        $this->elements  = $this->getArrayCopy();
        $this->keyIndexs = array_keys($this->elements);
        $this->indexKeys = array_flip($this->keyIndexs);
        $this->dirty     = false;

    }

    /**
     * Checks whether the loader is dirty
     *
     * @return boolean
     */
    protected function isDirty()
    {
        return $this->dirty;
    }

    /**
     * Get the Index of a given key
     *
     * @param  string  $key Key name
     * @return integer Key's index
     */
    protected function getIndexKey($key)
    {
        $this->load();

        return array_key_exists($key, $this->indexKeys)
            ? $this->indexKeys[$key]
            : null;
    }

    /**
     * Get the key of a given index
     *
     * @param  integer $index Key's index 
     * @return string  Key name
     */
    protected function getKeyIndex($index)
    {
        $this->load();

        return array_key_exists($index, $this->keyIndexs)
            ? $this->keyIndexs[$index]
            : null;
    }

    /**
     * Following methods overrides default methods which alters the iterator
     * in order to create a "Dirty state" which will force the reload
     *
     * You just need to write them all so as to get a complete working class
     */
    public function append($value)
    {
        $this->dirty = true;

        return parent::append($value);
    }
}

This iterator adds two methods getPreviousElement and getNextElement

Test Case

class Foo
{
    protected $name;

    public function __construct($name)
    {
        $this->name = $name;
    }
}


$array = array(
    'a' => new Foo('bar'),
    'b' => 42,
    'c' => new Foo('foobaz'),
    'czz' => 'bleh',
);

$iterator = new PreviousNextIterator($array);

foreach ($iterator as $key => $value) {
    echo '--- PREVIOUS ---', PHP_EOL;
    var_dump($iterator->getPreviousElement());
    echo '--- CURRENT  ---', PHP_EOL;
    var_dump($value);
    echo '---   NEXT   ---', PHP_EOL;
    var_dump($iterator->getNextElement());
    echo '----------------', PHP_EOL, PHP_EOL;
}

Outputs

--- PREVIOUS ---
NULL
--- CURRENT  ---
object(Foo)#1 (1) {
  ["name":protected]=>
  string(3) "bar"
}
---   NEXT   ---
int(42)
----------------

--- PREVIOUS ---
object(Foo)#1 (1) {
  ["name":protected]=>
  string(3) "bar"
}
--- CURRENT  ---
int(42)
---   NEXT   ---
object(Foo)#2 (1) {
  ["name":protected]=>
  string(6) "foobaz"
}
----------------

--- PREVIOUS ---
int(42)
--- CURRENT  ---
object(Foo)#2 (1) {
  ["name":protected]=>
  string(6) "foobaz"
}
---   NEXT   ---
string(4) "bleh"
----------------

--- PREVIOUS ---
object(Foo)#2 (1) {
  ["name":protected]=>
  string(6) "foobaz"
}
--- CURRENT  ---
string(4) "bleh"
---   NEXT   ---
NULL
----------------

In your example of filter, where you return your iterator, just replace

return $iterator;

By

return PreviousNextIterator::createFromIterator($iterator);

And then use it in TWIG like this

{% for i in items %}

    {% if i == items.previousElement %}
        <p>Yes</p>
    {% endif %}

    {% if i == items.nextElement %}
        <p>Same Next</p>
    {% endif %}

{% endfor %}

what about something like

{% set previous_name = '' %}
{% for i in items %}

    {% if i.name == previous_name %}
        <p>yes</p>
    {% else %}
        <p>no</p>
    {% endif %}

    {% set previous_name = i.name %}
{% endfor %}

source:http://twig.sensiolabs.org/doc/templates.html#setting-variables

There is a simple-ish way to do this right in twig with no extension:

{% for i in iterable_set %}
    {% if (loop.index0 + 1) < iterable_set|length %}
        {% set next = iterable_set[loop.index0 + 1] %}{# next #}
    {% endif %}

    {% if (loop.index0 - 1) >= 0 %}
        {% set prev = iterable_set[loop.index0 - 1] %}{# previous #}
    {% endif %}
{% endfor %}

I'm not sure what goal you wanna achieve, but try this one:

services:
    acme.twig.acme_extension:
        class: Acme\DemoBundle\Twig\AcmeExtension
        tags:
            - { name: twig.extension }

And:

namespace Acme\DemoBundle\Twig;

class AcmeExtension extends \Twig_Extension
{
    public function getFilters()
    {
        return array(
            new \Twig_SimpleFilter('filterX', array($this, 'filterX')),
        );
    }

    public function filterX(array $items)
    {
        $result = $prev = null;
        foreach ($items as $value) {
            if ($value === $prev && $prev !== null) {
                $result = 'yes';
            }
            $prev = $value;
        }
        return $result;
    }

    public function getName()
    {
        return 'acme_extension';
    }
}

And in your twig template:

{{ items|filterX}}

One possible solution to get prev and next items in array:

{% set prev = {} %}
{% set next = {} %}
{% set lastwascurrent = false %}
{% set last = {} %}
{% for item in items %}
  {% if lastwascurrent %}
    {% set next = item %}
    {% set lastwascurrent = false %}
  {% endif %}
  {% if item.id == 'some_id' %}
    {% set lastwascurrent = true %}
    {% set prev = last %}
  {% endif %}
  {% set last = item %}
{% endfor %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM