<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>

CC BY-SA 3.0
2014-09-02T19:46:47.883Z
@SeanO'Grady That [`nth()`](http://buildwithcraft.com/docs/templating/elementcriteriamodel#nth) function made it into [Craft 2.2](http://buildwithcraft.com/updates#build2579) ;)
Brandon Kelly
CC BY-SA 3.0
2014-09-04T22:45:15.730Z
Awesome news Brandon!
Seán O'Grady
CC BY-SA 3.0
2014-06-14T21:24:42.503Z
Brandon, is it save to omit the brackets in `first()` and in all other empty parameters?
carlcs
CC BY-SA 3.0
2014-06-14T21:26:21.330Z
@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.
Brandon Kelly
CC BY-SA 3.0
2014-06-14T21:30:34.623Z
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!
Seán O'Grady
CC BY-SA 3.0
2014-06-14T21:31:51.827Z
@SeanO'Grady I like that `nth()` idea. Adding to the list :)
Brandon Kelly
CC BY-SA 3.0
2014-06-14T21:08:28.583Z
2014-06-14T21:08:28.583Z
Brandon Kelly
<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>

<gallery><assetfilemodel><getsegment><index><loop>
Better way to find a specific asset?
# 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 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 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**: Brandon, 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 Brandon!  

---

