Better way to find a specific asset? 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?
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 %}