Answers.Body: <p>You could introduce a counter and count the images actually displayed in your nested for loops. Then add a conditional to the loops to stop the iteration at your set limit.</p>
  
  <p>With that many parameters set on your <code>craft.entries</code>
  <a href="http://buildwithcraft.com/docs/templating/elementcriteriamodel" rel="nofollow">ElementCriteriaModel</a> I would really recommend to use the <a href="http://buildwithcraft.com/docs/templating/elementcriteriamodel" rel="nofollow">object syntax</a> to set them. And instead of using the <code>search</code> parameter to check if there's images set to your entries, you would better use <code>relatedTo</code> and filter by entries with relations to an image model.</p>
  
  <pre><code>{% set assets = craft.assets.limit(null) %}
  
  {% set entries = craft.entries({
      section: 'pages',
      type: 'events',
      eventDate: '&lt;= ' ~ now,
      relatedTo: assets,
      order: 'eventDate desc',
  }) %}
  
  &lt;ul&gt;
      {% set count = 0 %}
      {% for entry in entries if count &lt; 5 %}
          {% for image in entry.gallery if count &lt; 5 %}
              &lt;li&gt;&lt;img src="{{ image.getUrl('smallImage') }}"&gt;&lt;/li&gt;
              {% set count = count + 1 %}
          {% endfor %}
      {% endfor %}
  &lt;/ul&gt;
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-21T12:20:40.627Z
Answers.Text: Thanks, why would it be beneficial to use the object syntax? And why a relatedto?
Answers.UserDisplayName: John Macpherson
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-21T12:25:28.317Z
Answers.Text: Don't you think the object syntax is more readable with that many parameters? And `relatedTo` because that's what as assets field does under the hood, it relates assets to your entry. I wouldn't use search unless it's absolutely necessary, because it relies on a search index file that might be not up-to-date.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-21T12:52:47.343Z
Answers.Text: Oh yes it's certainly more readable i thought there might be a performance issue. Thanks for your help. Appreciated.
Answers.UserDisplayName: John Macpherson
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-21T15:07:43.223Z
Answers.Text: I believe you can also just use  `loop.index` which is auto-incremented.
Answers.UserDisplayName: Douglas McDonald
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-21T15:09:11.610Z
Answers.Text: @Douglas wouldn't the loop var be reset for each outer loop iteration?
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-21T15:22:29.923Z
Answers.Text: @carlcs Just tested. You're right (as usual) — doesn't like `loop.index` in the conditional (throws error).
Answers.UserDisplayName: Douglas McDonald
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-20T17:56:56.793Z
Answers.LastActivityDate: 2015-04-21T06:41:49.160Z
Answers.LastEditDate: 2015-04-21T06:41:49.160Z
Answers.UserDisplayName: carlcs
Answers.Body: <p>Something like this should work:</p>
  
  <pre><code>{% 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 %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-20T17:39:47.623Z
Answers.LastActivityDate: 2015-04-20T17:39:47.623Z
Answers.UserDisplayName: Aaron Berkowitz
Body: <p>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.</p>
  
  <p>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.</p>
  
  <pre><code>{% 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 %}  
  </code></pre>
  
Tags: <entries>
Title: Limiting image rows by 5 but an unknown number of section entries
ThreadText: # Limiting image rows by 5 but an unknown number of section entries
  
  (Asked by: username_0 on 2015-04-20)
  
  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('<=' ~ now).search('gallery:*') %}
      {% for image in images.gallery.limit(5) %}
          <li><a href="{{image.getUrl}}"><img src="{{image.getUrl('smallImage')}}" alt="{{image.title}}" class="image-link" /></a></li>
      {% endfor %}
  
  {% endfor %}  
  
  ```
  
  **Tags:** entries
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2015-04-20. Score: 1 | Accepted answer
  
  You could introduce a counter and count the images actually displayed in your nested for loops. Then add a conditional to the loops to stop the iteration at your set limit.
  
  With that many parameters set on your `craft.entries`
  [ElementCriteriaModel](http://buildwithcraft.com/docs/templating/elementcriteriamodel) I would really recommend to use the [object syntax](http://buildwithcraft.com/docs/templating/elementcriteriamodel) to set them. And instead of using the `search` parameter to check if there's images set to your entries, you would better use `relatedTo` and filter by entries with relations to an image model.
  
  ```
  {% set assets = craft.assets.limit(null) %}
  
  {% set entries = craft.entries({
      section: 'pages',
      type: 'events',
      eventDate: '<= ' ~ now,
      relatedTo: assets,
      order: 'eventDate desc',
  }) %}
  
  <ul>
      {% set count = 0 %}
      {% for entry in entries if count < 5 %}
          {% for image in entry.gallery if count < 5 %}
              <li><img src="{{ image.getUrl('smallImage') }}"></li>
              {% set count = count + 1 %}
          {% endfor %}
      {% endfor %}
  </ul>
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks, why would it be beneficial to use the object syntax? And why a relatedto?  
  > **username_1**: Don't you think the object syntax is more readable with that many parameters? And `relatedTo` because that's what as assets field does under the hood, it relates assets to your entry. I wouldn't use search unless it's absolutely necessary, because it relies on a search index file that might be not up-to-date.  
  > **username_0**: Oh yes it's certainly more readable i thought there might be a performance issue. Thanks for your help. Appreciated.  
  > **username_2**: I believe you can also just use  `loop.index` which is auto-incremented.  
  > **username_1**: @username_2 wouldn't the loop var be reset for each outer loop iteration?  
  > **username_2**: @username_1 Just tested. You're right (as usual) — doesn't like `loop.index` in the conditional (throws error).  
  
  ---
  
  ### Answer by username_3 on 2015-04-20. Score: 3
  
  Something like this should work:
  
  ```
  {% for images in craft.entries.section('pages').type('events').order('eventDate desc').eventDate('<=' ~ now).search('gallery:*') %}
  
      {% set image_set = images.gallery %}
      {% for i in 0..4 %}
          {% if image_set[i] is defined %}
              <li><a href="{{image_set[i].getUrl}}"><img src="{{image_set[i].getUrl('smallImage')}}" alt="{{image_set[i].title}}" class="image-link" /></a></li>
          {% endif %}
      {% endfor %}
  {% endfor %}
  
  ```
  
  
  ---
  
  