Answers.Body: <p>That’s definitely the way to do it!</p>
  
  <p>I would set the URL segments to their own variables, to clean it up a little bit.</p>
  
  <pre><code>{% set entrySlug = craft.request.getSegment(2) %}
  {% set imageOffset = craft.request.getSegment(4) - 1 %}
  </code></pre>
  
  <p>Also, probably a good idea to include the <a href="http://buildwithcraft.com/docs/templating/craft.entries#section">section</a> param when fetching the entry, in case you have another entry in another section with the same slug.</p>
  
  <pre><code>{% set entry = craft.entries.section('mySection').slug(entrySlug).first() %}
  </code></pre>
  
  <p>The only issue I can think of is, if the 4th segment is greater than the total number of assets, you’re going to get an error. You can grab the asset using the <a href="http://buildwithcraft.com/docs/templating/craft.assets#offset">offset</a> param instead, and check to see if anything is returned:</p>
  
  <pre><code>{% set image = entry.images.offset(imageOffset).first() %}
  
  {% if not image %}
      {% exit 404 %}
  {% endif %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-02T19:46:47.883Z
Answers.Text: @SeanO'Grady That [`nth()`](http://buildwithcraft.com/docs/templating/elementcriteriamodel#nth) function made it into [Craft 2.2](http://buildwithcraft.com/updates#build2579) ;)
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-04T22:45:15.730Z
Answers.Text: Awesome news Brandon!
Answers.UserDisplayName: Seán O'Grady
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-14T21:24:42.503Z
Answers.Text: Brandon, is it save to omit the brackets in `first()` and in all other empty parameters?
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-14T21:26:21.330Z
Answers.Text: @ChristianSeelbach Sure. There’s a _slight_ performance improvement if you include them, because Twig knows for sure that it’s a method, so it doesn’t have to figure that out for itself every time, but it’s a micro-optimization at best.
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-14T21:30:34.623Z
Answers.Text: Cool! yeah I added in that section param shortly after. Is there any possibility of adding what I've done into a method for future users. Not sure how often the use case would be necessary but having a clean(er) way to call a particular asset from a field, like `file.nth(n)` would be quite useful!
Answers.UserDisplayName: Seán O'Grady
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-14T21:31:51.827Z
Answers.Text: @SeanO'Grady I like that `nth()` idea. Adding to the list :)
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-14T21:08:28.583Z
Answers.LastActivityDate: 2014-06-14T21:08:28.583Z
Answers.UserDisplayName: Brandon Kelly
Body: <p>I'm currently using a route to get a specific asset for a gallery section of my site. URL is similiar to <a href="http://craft.dev/place/place-name/gallery/1" rel="nofollow">http://craft.dev/place/place-name/gallery/1</a> (Where 1 is the number in the gallery.)</p>
  
  <p>I'm currently doing the following in the template</p>
  
  <pre><code>{% extends '_layout' %}
  
  {% set entry = craft.entries.slug(craft.request.getSegment(2)).first() %}
  
  {% set image = entry.images[craft.request.getSegment(4) - 1] %}
  
  {% block content %}
  
      {{image.getImg('featuredImage')}}
  
  {% endblock %}
  </code></pre>
  
  <p>It works currently but is there a cleaner way to do it without the <code>- 1</code>?</p>
  
Tags: <gallery><assetfilemodel><getsegment><index><loop>
Title: Better way to find a specific asset?
ThreadText: # Better way to find a specific asset?
  
  (Asked by: username_0 on 2014-06-14)
  
  I'm currently using a route to get a specific asset for a gallery section of my site. URL is similiar to <http://craft.dev/place/place-name/gallery/1> (Where 1 is the number in the gallery.)
  
  I'm currently doing the following in the template
  
  ```
  {% extends '_layout' %}
  
  {% set entry = craft.entries.slug(craft.request.getSegment(2)).first() %}
  
  {% set image = entry.images[craft.request.getSegment(4) - 1] %}
  
  {% block content %}
  
      {{image.getImg('featuredImage')}}
  
  {% endblock %}
  
  ```
  
  It works currently but is there a cleaner way to do it without the `- 1`?
  
  **Tags:** gallery, assetfilemodel, getsegment, index, loop
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2014-06-14. Score: 5 | Accepted answer
  
  That’s definitely the way to do it!
  
  I would set the URL segments to their own variables, to clean it up a little bit.
  
  ```
  {% set entrySlug = craft.request.getSegment(2) %}
  {% set imageOffset = craft.request.getSegment(4) - 1 %}
  
  ```
  
  Also, probably a good idea to include the [section](http://buildwithcraft.com/docs/templating/craft.entries#section) param when fetching the entry, in case you have another entry in another section with the same slug.
  
  ```
  {% set entry = craft.entries.section('mySection').slug(entrySlug).first() %}
  
  ```
  
  The only issue I can think of is, if the 4th segment is greater than the total number of assets, you’re going to get an error. You can grab the asset using the [offset](http://buildwithcraft.com/docs/templating/craft.assets#offset) param instead, and check to see if anything is returned:
  
  ```
  {% set image = entry.images.offset(imageOffset).first() %}
  
  {% if not image %}
      {% exit 404 %}
  {% endif %}
  
  ```
  
  > **Comments:**
  > **username_2**: username_1, is it save to omit the brackets in `first()` and in all other empty parameters?  
  > **username_1**: @ChristianSeelbach Sure. There’s a _slight_ performance improvement if you include them, because Twig knows for sure that it’s a method, so it doesn’t have to figure that out for itself every time, but it’s a micro-optimization at best.  
  > **username_0**: Cool! yeah I added in that section param shortly after. Is there any possibility of adding what I've done into a method for future users. Not sure how often the use case would be necessary but having a clean(er) way to call a particular asset from a field, like `file.nth(n)` would be quite useful!  
  > **username_1**: @SeanO'Grady I like that `nth()` idea. Adding to the list :)  
  > **username_1**: @SeanO'Grady That [`nth()`](http://buildwithcraft.com/docs/templating/elementcriteriamodel#nth) function made it into [Craft 2.2](http://buildwithcraft.com/updates#build2579) ;)  
  > **username_0**: Awesome news username_1!  
  
  ---
  
  