我想知道是否有可能(在Twig中)从元素列表中找到(例如书籍实例之类的)一本满足特定条件的特定书籍(例如,如前一卷那样具有特定的其他书籍) 。 PS:换句话说,是否可以查询Twig中的列表? 谢谢!
答案 0 :(得分:0)
您可以遍历您的图书并找到您正在寻找的图书:
{# Look for a book with previous book = 9 #}
{% set previousBook = 9 %}
{# Define the books array #}
{% set books = {
1: {'title': 'A', 'previous': [7, 8]},
2: {'title': 'B', 'previous': [9, 10]}
} %}
{# By default there is no result #}
{% set foundBook = null%}
{# Loop through books #}
{% for bookId, book in books %}
{# Apply the test #}
{% if previousBook in book.previous %}
{# The book has been found, store it #}
{% set foundBook = book %}
{% endif %}
{% endfor %}
{# If a book has been found, display it #}
{% if foundBook is not null %}
I found the "{{ foundBook.title }}" book which has previous book
"{{ previousBook }}".
{% endif %}
但是通过在Controller中使用这个逻辑,你很可能更容易做同样的事情。