<p>Personally, in situations like this (particularly if there's only a few items, and no easy way to automate it with inflection or similar,) I just use an array as a kind of dictionary.</p>

<pre><code>{% set sectionHandlesBySegment = { 
    'hotel-california': 'hotelsCalifornia',
    'hotel-florida':    'hotelsFlorida' }
%}

{% if not craft.request.getSegment(2) in sectionHandlesBySegment|keys %}
    {% exit 404 %}
{% endif %}

{% set sectionHandle = sectionHandlesBySegment[craft.request.getSegment(2)] %}
{% set hotels = craft.entries.section(sectionHandle) %}
</code></pre>

CC BY-SA 3.0
2014-09-09T15:58:34.367Z
2014-09-09T15:58:34.367Z
Mike Pepper
<p>You could use the <a href="http://buildwithcraft.com/docs/templating/elementcriteriamodel#setting-the-parameters" rel="nofollow">"object syntax"</a> of the ElementCriteriaModel, in combination with an <a href="http://twig.sensiolabs.org/doc/tags/if.html" rel="nofollow">if-statement</a>:</p>

<pre class="lang-twig prettyprint-override"><code>// Checks if the 2. segment of the url is 'hotel-california'
{% if craft.request.getSegment(2) == 'hotel-california' %}
    {% set params = { section: 'hotelCalifornia' } %}
{% else %}
    {% set params = { section: 'anotherSection' } %}
{% endif %}

{% for entry in craft.entries(params) %}
    {{ entry.title }}
{% endif %}
</code></pre>

CC BY-SA 3.0
2014-09-09T15:43:58.957Z
2014-09-09T15:43:58.957Z
Victor
<p>I'd like to use a single template to pull in entries from different channels based on url segment 2. But it seems I need to change the section attribute to match the url segment:</p>

<pre><code>{% for entry in craft.entries.section('mySection').find() %}
</code></pre>

<p>Given that the url segment and section handle are different, can this be done?</p>

<p>For example the url segment might be <code>hotel-california</code> and the section handle of the section I want entries from could be <code>hotelCalifornia</code> or <code>hotelsInCalifornia</code>.</p>

<templating>
Use a single template to display entries from different channels?
# Use a single template to display entries from different channels?

(Asked by: username_0 on 2014-09-09)

I'd like to use a single template to pull in entries from different channels based on url segment 2. But it seems I need to change the section attribute to match the url segment:
{% for entry in craft.entries.section('mySection').find() %}

Given that the url segment and section handle are different, can this be done?
For example the url segment might be hotel-california and the section handle of the section I want entries from could be hotelCalifornia or hotelsInCalifornia.

**Tags:** templating

---

## 2 answers

### Answer by username_1 on 2014-09-09. Score: 3 | Accepted answer

Personally, in situations like this (particularly if there's only a few items, and no easy way to automate it with inflection or similar,) I just use an array as a kind of dictionary.
{% set sectionHandlesBySegment = { 
    'hotel-california': 'hotelsCalifornia',
    'hotel-florida':    'hotelsFlorida' }
%}

{% if not craft.request.getSegment(2) in sectionHandlesBySegment|keys %}
    {% exit 404 %}
{% endif %}

{% set sectionHandle = sectionHandlesBySegment[craft.request.getSegment(2)] %}
{% set hotels = craft.entries.section(sectionHandle) %}


---

### Answer by username_2 on 2014-09-09. Score: 1

You could use the "object syntax" of the ElementCriteriaModel, in combination with an if-statement:
// Checks if the 2. segment of the url is 'hotel-california'
{% if craft.request.getSegment(2) == 'hotel-california' %}
    {% set params = { section: 'hotelCalifornia' } %}
{% else %}
    {% set params = { section: 'anotherSection' } %}
{% endif %}

{% for entry in craft.entries(params) %}
    {{ entry.title }}
{% endif %}


---

