Php 我可以写一个细枝扩展来访问循环中的上一个和下一个元素吗
我想知道是否有一种方法可以在Twig中创建函数(或任何东西),这样我就可以访问for循环中的下一个和上一个元素。大概是这样的:Php 我可以写一个细枝扩展来访问循环中的上一个和下一个元素吗,php,symfony,twig,Php,Symfony,Twig,我想知道是否有一种方法可以在Twig中创建函数(或任何东西),这样我就可以访问for循环中的下一个和上一个元素。大概是这样的: {% for i in items %} {% if i == previous() %} <p>yes</p> {% endif %} {% endfor %} 下一个也一样 在我写了一个排序过滤器之后,问题变得更大了,因为现在 {%set items=allitems | sortbyname%} {%
{% for i in items %}
{% if i == previous() %}
<p>yes</p>
{% endif %}
{% endfor %}
下一个也一样
在我写了一个排序过滤器之后,问题变得更大了,因为现在
{%set items=allitems | sortbyname%}
{% for item in items %}
{{ item.name }}
{% endfor %}
这里的项目是按顺序排列的
及
他们不在这儿
所以我不能用这样的东西:
如果item.name==items[loop.index0+1]。访问下一个元素的名称
我不知道如何克服这些问题:(你能帮帮我吗?我不确定你想要达到什么目标,但试试这个:
services:
acme.twig.acme_extension:
class: Acme\DemoBundle\Twig\AcmeExtension
tags:
- { name: twig.extension }
以及:
在您的小树枝模板中:
{{ items|filterX}}
像这样的怎么样
{% 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 %}
{%set-previous\u-name='''%}
{items%%中的i的%s}
{%if i.name==previous_name%}
对
{%else%}
没有
{%endif%}
{%set previous_name=i.name%}
{%endfor%}
来源:在twig中没有
previous()
。你可以看看 在您的情况下,一个解决方法是构建一个自定义迭代器。
这里有一个例子
/**
* 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);
}
}
输出
在过滤器示例中,返回迭代器,只需替换
return $iterator;
借
然后像这样用在树枝上
{% for i in items %}
{% if i == items.previousElement %}
<p>Yes</p>
{% endif %}
{% if i == items.nextElement %}
<p>Same Next</p>
{% endif %}
{% endfor %}
{items%]中的i的%
{%if i==items.previousElement%}
对
{%endif%}
{%if i==items.nextElement%}
下一个一样
{%endif%}
{%endfor%}
获取阵列中上一个和下一个项目的一种可能解决方案:
{% 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 %}
有一种简单的方法可以在无扩展的细枝中正确执行此操作:
{% 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在iterable_集合%中的百分比}
{%if(loop.index0+1)=0%}
{%set prev=iterable_集[loop.index0-1]]}{{previous}
{%endif%}
{%endfor%}
请查看:谢谢,但我已经读了好几遍了,我知道如何编写扩展,问题是对于这种特定情况,我不知道应该在其中编写什么-函数的主体应该是什么。谢谢,但这不起作用:(我更新了我的问题,提供了有关该问题的更多信息。@Faery请向我展示您的sortbyname
过滤器实现。如下所示
--- 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
----------------
return $iterator;
return PreviousNextIterator::createFromIterator($iterator);
{% for i in items %}
{% if i == items.previousElement %}
<p>Yes</p>
{% endif %}
{% if i == items.nextElement %}
<p>Same Next</p>
{% endif %}
{% endfor %}
{% 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 %}
{% 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 %}