Limiting image rows by 5 but an unknown number of section entries I'm trying to limit the total number of images shown to 5. The below clearly isn't correct but gives a rough idea of what i want to do.

One entry could contain 1 image and the next entry 3 images. So i can't set a limit on the craft.entries tag because of the unknown number of images in an entry. It's on the images for loop i want to limit to 5.

{% for images in craft.entries.section('pages').type('events').order('eventDate desc').eventDate('&lt;=' ~ now).search('gallery:*') %}
    {% for image in images.gallery.limit(5) %}
        &lt;li&gt;&lt;a href="{{image.getUrl}}"&gt;&lt;img src="{{image.getUrl('smallImage')}}" alt="{{image.title}}" class="image-link" /&gt;&lt;/a&gt;&lt;/li&gt;
    {% endfor %}

{% endfor %}
Something like this should work:

{% for images in craft.entries.section('pages').type('events').order('eventDate desc').eventDate('&lt;=' ~ now).search('gallery:*') %}

    {% set image_set = images.gallery %}
    {% for i in 0..4 %}
        {% if image_set[i] is defined %}
            &lt;li&gt;&lt;a href="{{image_set[i].getUrl}}"&gt;&lt;img src="{{image_set[i].getUrl('smallImage')}}" alt="{{image_set[i].title}}" class="image-link" /&gt;&lt;/a&gt;&lt;/li&gt;
        {% endif %}
    {% endfor %}
{% endfor %}