E
E
Elisey Basenko2017-12-15 17:34:53
Twig
Elisey Basenko, 2017-12-15 17:34:53

How to check the number of records in the database in TWIG?

Before executing the loop, I first need to check:
If there are many records (more than 1), then we output the cycle, if less, then we also output it but a little differently.
{% if records > 1 %} does not work))
How can this be done? I can't find something.
An example of the code to be executed if there are more than 1 records

{% if record %}

    <h2>{{ record.name }}</h2>

{% else %}
{% endif %}

An example of the code to be executed if there is only one record in the database.
{% if record %}

    <h1>{{ record.name }}</h2>

{% else %}
{% endif %}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
No Name, 2017-12-15
@Elisseii

It might be worth using the length filter:

{% if records|length > 1 %}
    {# вывести как список #}
{% else %}
    {# вывести как одиночную запись #}
{% endif %}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question