<p>First, you should create a <a href="https://craftcms.com/docs/routing#dynamic-routes" rel="noreferrer">dynamic route</a> (something like <code>"games/glossary/&lt;slug&gt;"</code>) and point it to a template. A dynamic route means you won't have to create a template for every letter. Note the use of the <code>&lt;slug&gt;</code> token, which – although not entirely semantic – should do nicely in this case.</p>

<p>In your template, you'll need to pull the letter from the URL. Note that it's also a good idea to test if the variable is a single character, and in the a-z range (i.e. a valid letter, and not some other entity).</p>

<pre><code>{% set letter = slug is defined and slug|length == 1 and slug matches '/^[a-z]/i' ? slug %}
</code></pre>

<p>If there is no valid letter in the <code>slug</code> variable, it's a good idea to throw a 404:</p>

<pre><code>{% if not letter %}
  {% exit 404 %}
{% endif %}
</code></pre>

<p>Next, you can actually use <a href="https://craftcms.com/docs/searching#supported-syntaxes" rel="noreferrer">search syntax</a> for the <code>title</code> parameter (specifically, a wildcard <code>*</code>), in order to pull all entries with a title starting with the relevant letter:</p>

<pre><code>{% set entries = craft.entries.section('games').title(letter~'*').limit(null).order('title asc') %}
</code></pre>

<p>If you want to throw a 404 whenever there aren't any entries for any given letter, it's simply a matter of testing the <code>length</code> property for <code>entries</code>:</p>

<pre><code>{% if not entries|length %}
    {% exit 404 %}
{% endif %}
</code></pre>

<p>If you need <a href="https://craftcms.com/docs/templating/paginate" rel="noreferrer">pagination</a> in your template, it'll work out of the box as usual:</p>

<pre><code>{% paginate craft.entries.section('games').title(letter~'*').limit(10).order('title asc') as pageInfo, pageEntries %}

{% if not pageEntries|length %}
    {% exit 404 %}
{% endif %}

{% for entry in pageEntries %}
    ...
</code></pre>

CC BY-SA 3.0
2016-12-26T14:59:55.380Z
That works like a charm!!!!
DevServe
CC BY-SA 3.0
2016-12-26T15:01:39.640Z
Only thing not working is going to a letter without entries doesn't give me the 404. Need to ponder where to place your snippet as I also have pagination code in my template.
DevServe
CC BY-SA 3.0
2016-12-26T16:19:15.123Z
If you're doing pagination, you'll probably need to use a named token in your dynamic route, because with pagination the page number will be the last segment in the URL, not the letter. For the 404 issue, it's simply a matter of testing the length of the returned entries. Updated my answer on both counts.
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2016-12-26T09:31:01.567Z
2016-12-26T16:19:12.650Z
2016-12-26T16:19:12.650Z
Mats Mikkel Rummelhoff
<p>I have a list of all my games using pagination. It works great:
<a href="http://www.colorcomputergames.com/games" rel="nofollow noreferrer">http://www.colorcomputergames.com/games</a></p>

<p>However, I'd like to add links at the top of the page like:
A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z</p>

<p>And so I want to be able to click on any letter and display only entries starting with that letter.</p>

<p>I was was going to make a template for every page but that seems inefficient.</p>

<p>How should I tackle this?</p>

<search><conditional>
Select entries starting with same letter from alphabet list
# Select entries starting with same letter from alphabet list

(Asked by: username_0 on 2016-12-26)

I have a list of all my games using pagination. It works great:
<http://www.colorcomputergames.com/games>

However, I'd like to add links at the top of the page like:
A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z

And so I want to be able to click on any letter and display only entries starting with that letter.

I was was going to make a template for every page but that seems inefficient.

How should I tackle this?

**Tags:** search, conditional

---

## 1 answers

### Answer by username_1 on 2016-12-26. Score: 11 | Accepted answer

First, you should create a dynamic route (something like "games/glossary/") and point it to a template. A dynamic route means you won't have to create a template for every letter. Note the use of the  token, which – although not entirely semantic – should do nicely in this case.
In your template, you'll need to pull the letter from the URL. Note that it's also a good idea to test if the variable is a single character, and in the a-z range (i.e. a valid letter, and not some other entity).
{% set letter = slug is defined and slug|length == 1 and slug matches '/^[a-z]/i' ? slug %}

If there is no valid letter in the slug variable, it's a good idea to throw a 404:
{% if not letter %}
  {% exit 404 %}
{% endif %}

Next, you can actually use search syntax for the title parameter (specifically, a wildcard *), in order to pull all entries with a title starting with the relevant letter:
{% set entries = craft.entries.section('games').title(letter~'*').limit(null).order('title asc') %}

If you want to throw a 404 whenever there aren't any entries for any given letter, it's simply a matter of testing the length property for entries:
{% if not entries|length %}
    {% exit 404 %}
{% endif %}

If you need pagination in your template, it'll work out of the box as usual:
{% paginate craft.entries.section('games').title(letter~'*').limit(10).order('title asc') as pageInfo, pageEntries %}

{% if not pageEntries|length %}
    {% exit 404 %}
{% endif %}

{% for entry in pageEntries %}
    ...

> **Comments:**
> **username_0**: That works like a charm!!!!  
> **username_0**: Only thing not working is going to a letter without entries doesn't give me the 404. Need to ponder where to place your snippet as I also have pagination code in my template.  
> **username_1**: If you're doing pagination, you'll probably need to use a named token in your dynamic route, because with pagination the page number will be the last segment in the URL, not the letter. For the 404 issue, it's simply a matter of testing the length of the returned entries. Updated my answer on both counts.  

---

