{% set imagesLength = book|length %}
{% set groupIndex = 0 %}
{% set imageInGroupIndex = 0 %}
{% for p in book %}
{% if p and p.hd %}
{% set orientation = 'portrait' %}
{% if book[loop.index0].is_landscape %}
{% set orientation = 'landscape' %}
{% endif %}
{% set nbImagesPerGroup = 2 %}
{% if loop.index0 == 0 or
book[loop.index0].is_landscape or
loop.index0 == (imagesLength - 1) or
(loop.index0 < (imagesLength - 1) and imageInGroupIndex == 0 and book[loop.index0 + 1].is_landscape)
%}
{% set nbImagesPerGroup = 1 %}
{% endif %}
{% if imageInGroupIndex == 0 %}
{% endif %}
{% if imageInGroupIndex == (nbImagesPerGroup - 1) %}
{% set imageInGroupIndex = 0 %}
{% set groupIndex = groupIndex + 1 %}