<p>Is the problem you're not getting the segment from the URL?</p>

<p><code>{% set locationSlugInUrl = craft.request.getSegment(1) %}</code> or <code>{% set locationSlugInUrl = craft.request.firstSegment %}</code> should do that for you.</p>

<p>Or you're not getting the entry? In which case you need to either specify the template in that section's settings or do something like <code>{% set entry = craft.entries.section('pricing').first %}</code> or whatever the section slug is (assuming it's a single).</p>

<p>And what kind of field is <code>entry.pricingPlans</code>? If it is a matrix field, what type of field is <code>plan.location</code>?</p>

CC BY-SA 3.0
2017-06-01T15:11:15.220Z
I was able to figure this out by using something like `{% set entry = craft.entries.section('pricing').first %}` if `entry` was null, that way I can handle cases where a user goes to `mysite.com/pricing` (where the section is passing the entry data) or `mysite.com/boston/pricing` (where I need to pull from `craft.entries.section`). Thanks.
GMStevens
CC BY-SA 3.0
2017-06-02T08:15:53.550Z
Yep, sounds familiar.
Clive Portman
CC BY-SA 3.0
2017-05-31T16:44:47.767Z
2017-05-31T16:50:14.697Z
2017-05-31T16:50:14.697Z
Clive Portman
<p>I'm trying to set up a site for clients that have locations in multiple cities and who want to be able to display something specific to that location on certain pages. I would like to set up routes using the city name as a variable, followed by the page (i.e. <code>mysite.com/chicago/pricing</code>, <code>mysite.com/boston/pricing</code>, etc.), and have it route to the same section and template with all of that section's corresponding entry fields.</p>

<p>I was planning to grab the URL parameter as a variable that enables me to dynamically load content based on a simple string match for that variable. I want to do something like this</p>

<pre><code>{% set location = matches[1] %}

{% if not location %}
  {% set selectedLocation = locations.defaultLocation %}
{% else %}
  {% set selectedLocation = location %}
{% endif %}

....

{% for plan in entry.pricingPlans %}
    {% if plan.location == selectedLocation %}
      ...
    {% endif %}
{% endfor %}
</code></pre>

<p>I can route to the template itself just fine, but I don't get the data. Any suggestions on how best to achieve this? I'm not very PHP savvy and have only recently started getting to more advanced tasks in Craft.</p>

<routing>
How do I do routing for something like {city}/pricing, {city}/how-it-works, etc. while loading the same template and dataset?
# How do I do routing for something like {city}/pricing, {city}/how-it-works, etc. while loading the same template and dataset?

(Asked by: username_0 on 2017-05-31)

I'm trying to set up a site for clients that have locations in multiple cities and who want to be able to display something specific to that location on certain pages. I would like to set up routes using the city name as a variable, followed by the page (i.e. mysite.com/chicago/pricing, mysite.com/boston/pricing, etc.), and have it route to the same section and template with all of that section's corresponding entry fields.
I was planning to grab the URL parameter as a variable that enables me to dynamically load content based on a simple string match for that variable. I want to do something like this
{% set location = matches[1] %}

{% if not location %}
  {% set selectedLocation = locations.defaultLocation %}
{% else %}
  {% set selectedLocation = location %}
{% endif %}

....

{% for plan in entry.pricingPlans %}
    {% if plan.location == selectedLocation %}
      ...
    {% endif %}
{% endfor %}

I can route to the template itself just fine, but I don't get the data. Any suggestions on how best to achieve this? I'm not very PHP savvy and have only recently started getting to more advanced tasks in Craft.

**Tags:** routing


---

## 1 answers

### Answer by username_1 on 2017-05-31. Score: 1 | Accepted answer

Is the problem you're not getting the segment from the URL?

`{% set locationSlugInUrl = craft.request.getSegment(1) %}` or `{% set locationSlugInUrl = craft.request.firstSegment %}` should do that for you.

Or you're not getting the entry? In which case you need to either specify the template in that section's settings or do something like `{% set entry = craft.entries.section('pricing').first %}` or whatever the section slug is (assuming it's a single).

And what kind of field is `entry.pricingPlans`? If it is a matrix field, what type of field is `plan.location`?

> **Comments:**
> **username_0**: I was able to figure this out by using something like `{% set entry = craft.entries.section('pricing').first %}` if `entry` was null, that way I can handle cases where a user goes to `mysite.com/pricing` (where the section is passing the entry data) or `mysite.com/boston/pricing` (where I need to pull from `craft.entries.section`). Thanks.  
> **username_1**: Yep, sounds familiar.  

---

