<p>Alan describes how to setup a route via the <code>Settings-&gt;Routes</code> area in the CP. If you happen to be managing file based routes you can setup a similar route like so:</p>

<pre><code>return array(

    // Your Custom Section 
    'username/blog/(?P&lt;slug&gt;.*)' =&gt;
    'blog/_entry'

);
</code></pre>

<p>The third segment in your route <code>(?P&lt;slug&gt;.*)</code> creates the variable that will be accessible in your template on that page.  You can name the dynamic route variable whatever you would like (for example, <code>(?P&lt;anythingYouWantHere&gt;.*)</code>).</p>

<p>When your <code>blog/_entry</code> template gets loaded.  That <code>slug</code> variable will be available just like any other global variable.  You can access it using the standard output syntax: <code>{{ slug }}</code> or by passing it to your <code>craft.entries</code> tag:</p>

<pre><code>{% set entry = craft.entries.slug(slug).first() %}

{{ entry.title }}
</code></pre>

<p><code>.slug(slug)</code> will limit your query to just return the entry you want and, <code>.first()</code> will make sure you can access that entry without needing to treat it like an array and loop through it.</p>

CC BY-SA 3.0
2015-03-26T20:31:24.803Z
Thanks, I'll be using file based routes. I skimmed through your video on this yesterday. I'll give it a more careful viewing to make sure I understand all this better.
Doug
CC BY-SA 3.0
2015-03-26T08:14:08.040Z
2015-03-26T17:40:11.213Z
2015-03-26T17:40:11.213Z
Ben Parizek
<p>You can retrieve the entry by grabbing it from the URL as a variable:</p>

<pre><code>{% set entrySlug = craft.request.lastSegment %}
</code></pre>

<p>And then using craft.entries to find the matching content:</p>

<pre><code>{% set entry = craft.entries.slug(entrySlug).find() %}
</code></pre>

<p>Alternatively when you set up your route, so that Craft knows which template to load when someone is requesting your entry, you can use the "slug" token which will expose a slug var in your template automatically.</p>

<pre><code>{% set entry = craft.entries.slug(slug).find() %}
</code></pre>

<p>Let me know if I understood the question correctly and my apologies for saying anything you already knew!</p>

CC BY-SA 3.0
2015-03-26T04:06:12.527Z
2015-03-26T04:06:12.527Z
Alan Miller
<p><strong>Short Question:</strong>
I'm coming from ExpressionEngine where 'dynamic' would do the trick. However I can't seem to figure out how to do this in Craft. </p>

<p>I want to be able to use one Channel to show content in different places on the site. Can Craft look at a particular URL segment and load the entry? </p>

<p><strong>Longer explanation</strong>
I have many users who will have their own 'site' on a website. Each 'site' will have four basic pages
-welcome
-my story
-my work
-blog</p>

<p>The Blog page will show entries authored by them, but all users will share the same channel. </p>

<p>I have it all working for the main pages. I'm creating a Structure section for each user. I point it to a template (_userTemplates/johnDoe) that contains some variables and includes:</p>

<pre><code>{% extends "_layout" %}
{% set author = "12" %}
{% set sectionName = "johnDoe" %}

{% set content %}
{% include "_partials/content" %}
{% endset %}
</code></pre>

<p>Layout looks like this:</p>

<pre><code>&lt;!DOCTYPE html&gt;
&lt;html&gt;
{% include "_partials/htmlHead" %}
&lt;body&gt;

{% include "_partials/nav" %}

{{ content }}

{% include "_partials/footer" %}

&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>_partials/nav has dynamic navigation. It pulls the variable "sectionName" from the user's template to show their navigation links for their structure pages.</p>

<pre><code>&lt;nav class="desktop {{ type is defined ? type }}" role="navigation"&gt;
    &lt;div&gt;
        &lt;ul&gt;
            {% set pages = craft.entries.section(sectionName) %}
            {% nav page in pages %}
            &lt;li&gt;{{ page.getLink() }}&lt;/li&gt;
            {% endnav %}
        &lt;/ul&gt;
    &lt;/div&gt;
&lt;/nav&gt;
</code></pre>

<p>_partials/content looks like this:</p>

<pre><code>{% if craft.request.getSegment(3) != '' %}
    &lt;section&gt;
        &lt;div class="grid padded full-768"&gt;
            {% include "_partials/contentBuilder" %}
        &lt;/div&gt;
    &lt;/section&gt;
{% elseif craft.request.getSegment(2) == 'blog' %}
    &lt;section&gt;
        &lt;div class="grid padded full-768"&gt;  
            {% include "_partials/blogListing" %}
        &lt;/div&gt;
    &lt;/section&gt;
{% elseif craft.request.getSegment(3) == '' %}
    &lt;section&gt;
        &lt;div class="grid padded full-768"&gt;
            {% include "_partials/contentBuilder" %}
        &lt;/div&gt;
    &lt;/section&gt;
{% endif %}
</code></pre>

<p>The template that contains all the content, _partials/contentBuilder, looks like this:</p>

<pre><code>    &lt;header&gt;
        &lt;h1 class="alpha highlighted"&gt;{{ entry.title }}&lt;/h1&gt;
    &lt;/header&gt;
    {% for content in entry.contentEditor %}
        {% if content.type == 'heading' %}
            &lt;h2&gt;{{ content.heading }}&lt;/h2&gt;
        {% elseif content.type == 'subheading' %}
            &lt;h3&gt;{{ content.subheading }}&lt;/h3&gt;
        {% elseif content.type == 'image' %}
            {% for image in content.imageFile %}
                &lt;img src="{{ image.url }}"/&gt;
            {% endfor %}
        {% elseif content.type == 'text' %}
            {{ content.text }}
        {% elseif content.type == 'blockquote' %}
            &lt;blockquote&gt;{{ content.blockquoteQuote }}&lt;/blockquote&gt;
            {% if content.blockquoteCitation %}
            &lt;cite&gt;{{ content.blockquoteCitation }}&lt;/cite&gt;
            {% endif %}
        {% elseif content.type == 'video' %}
            {% if content.videoTitle != '' %}
                &lt;h3&gt;{{ content.videoTitle }}&lt;/h3&gt;
            {% endif %}
            {{ craft.embedder.embed (content.videoUrl) }}
        {% endif %}
    {% endfor %}
&lt;/article&gt;
</code></pre>

<p>This works great for the main Structure pages. However when the template route "johndoe/blog/{slug} points to _userTemplates/johnDoe this does not work. I get "Variable "entry" does not exist". I thought that the template route would look for an entry and then know which entry it is? I will try pulling the entry from the URL segment per one of the answers and see how that works. However I thought the route would work.</p>

<p>EDIT - Got it working using this in _partials/content:</p>

<pre><code>{% if craft.request.getSegment(3) != '' %}
    &lt;section&gt;
        &lt;div &gt;
            {% for entry in craft.entries.slug(slug).find() %}
                {% include "_partials/contentBuilder" %}
            {% endfor %}
        &lt;/div&gt;
    &lt;/section&gt;
{% elseif craft.request.getSegment(2) == 'updates' %}
    &lt;section&gt;
        &lt;div&gt;   
            {% include "_partials/updateListing" %}
        &lt;/div&gt;
    &lt;/section&gt;
{% elseif craft.request.getSegment(3) == '' %}
    &lt;section&gt;
        &lt;div&gt;
            {% include "_partials/contentBuilder" %}
        &lt;/div&gt;
&lt;/section&gt;
{% endif %}
</code></pre>

<p>It looks like I was expecting too much from the template route. </p>

<p>The goal of all this is to be able to give people their own "site", and do it quickly with code that is easy to manage and maintain. This method only requires a new section be created, set up the user with proper permissions, create their template partial, and set up the route. If there is a better way to do this, let me know. </p>

CC BY-SA 3.0
2015-03-26T12:08:06.693Z
Is the URL structure for each user's site something like: www.website.com/johnDoe/blog. That is what I had assumed based on your code above for getSegment(2) == 'blog' that the first URL segment was the username. Let us know if that is the structure or if it is another way as it will help fine tune our answers. Thanks!
Alan Miller
CC BY-SA 3.0
2015-03-26T15:53:20.090Z
Yes, the URL structure is www.website.com/johndoe/blog. I've updated the question with my latest code and the error I'm getting.
Doug
CC BY-SA 3.0
2015-03-26T18:15:10.103Z
Yeah I am not sure why Craft doesn't return the entry model automatically with routes but my guess would be because you may want to do any number of things with the token data. Have you considered trying to automate any of this process with a plugin? That is beyond my skill set but according the documentation a plug-in can create dedicated url's that will preload entry or category variables. http://buildwithcraft.com/docs/routing
Alan Miller
CC BY-SA 3.0
2015-03-26T20:29:40.033Z
Plugins are out of reach for me at the moment. We are wanting to get his out the door quickly, then once we get it working for the few who need it I can look into a plugin.
Doug
CC BY-SA 3.0
2015-03-26T20:40:15.697Z
Now that it is all working, this is much easier to maintain and edit in Craft. In ExpressionEngine I ran into parse order issues often. I was using Stash, Zoo Visitor, Channel Images, and some other plugins for functionality. Between the somewhat unconventional approach and the third party plugins, it was very finicky to work on. 

Once I got this sorted out, migrating the system over from EE only took a few hours. It is so much cleaner in Craft. Everything I was using those third party plugins for is baked into Craft.
Doug
<routing><channels>
Show entry in channel based on URL segment
# Show entry in channel based on URL segment

(Asked by: username_0 on 2015-03-26)

Short Question:
I'm coming from ExpressionEngine where 'dynamic' would do the trick. However I can't seem to figure out how to do this in Craft. 
I want to be able to use one Channel to show content in different places on the site. Can Craft look at a particular URL segment and load the entry? 
Longer explanation
I have many users who will have their own 'site' on a website. Each 'site' will have four basic pages
-welcome
-my story
-my work
-blog
The Blog page will show entries authored by them, but all users will share the same channel. 
I have it all working for the main pages. I'm creating a Structure section for each user. I point it to a template (_userTemplates/johnDoe) that contains some variables and includes:
{% extends "_layout" %}
{% set author = "12" %}
{% set sectionName = "johnDoe" %}

{% set content %}
{% include "_partials/content" %}
{% endset %}

Layout looks like this:


{% include "_partials/htmlHead" %}


{% include "_partials/nav" %}

{{ content }}

{% include "_partials/footer" %}




_partials/nav has dynamic navigation. It pulls the variable "sectionName" from the user's template to show their navigation links for their structure pages.

    
        
            {% set pages = craft.entries.section(sectionName) %}
            {% nav page in pages %}
            {{ page.getLink() }}
            {% endnav %}
        
    


_partials/content looks like this:
{% if craft.request.getSegment(3) != '' %}
    
        
            {% include "_partials/contentBuilder" %}
        
    
{% elseif craft.request.getSegment(2) == 'blog' %}
    
          
            {% include "_partials/blogListing" %}
        
    
{% elseif craft.request.getSegment(3) == '' %}
    
        
            {% include "_partials/contentBuilder" %}
        
    
{% endif %}

The template that contains all the content, _partials/contentBuilder, looks like this:
    
        {{ entry.title }}
    
    {% for content in entry.contentEditor %}
        {% if content.type == 'heading' %}
            {{ content.heading }}
        {% elseif content.type == 'subheading' %}
            {{ content.subheading }}
        {% elseif content.type == 'image' %}
            {% for image in content.imageFile %}
                
            {% endfor %}
        {% elseif content.type == 'text' %}
            {{ content.text }}
        {% elseif content.type == 'blockquote' %}
            {{ content.blockquoteQuote }}
            {% if content.blockquoteCitation %}
            {{ content.blockquoteCitation }}
            {% endif %}
        {% elseif content.type == 'video' %}
            {% if content.videoTitle != '' %}
                {{ content.videoTitle }}
            {% endif %}
            {{ craft.embedder.embed (content.videoUrl) }}
        {% endif %}
    {% endfor %}


This works great for the main Structure pages. However when the template route "johndoe/blog/{slug} points to _userTemplates/johnDoe this does not work. I get "Variable "entry" does not exist". I thought that the template route would look for an entry and then know which entry it is? I will try pulling the entry from the URL segment per one of the answers and see how that works. However I thought the route would work.
EDIT - Got it working using this in _partials/content:
{% if craft.request.getSegment(3) != '' %}
    
        
            {% for entry in craft.entries.slug(slug).find() %}
                {% include "_partials/contentBuilder" %}
            {% endfor %}
        
    
{% elseif craft.request.getSegment(2) == 'updates' %}
    
           
            {% include "_partials/updateListing" %}
        
    
{% elseif craft.request.getSegment(3) == '' %}
    
        
            {% include "_partials/contentBuilder" %}
        

{% endif %}

It looks like I was expecting too much from the template route. 
The goal of all this is to be able to give people their own "site", and do it quickly with code that is easy to manage and maintain. This method only requires a new section be created, set up the user with proper permissions, create their template partial, and set up the route. If there is a better way to do this, let me know.

**Tags:** routing, channels
> **Comments:**
> **username_1**: Is the URL structure for each user's site something like: www.website.com/johnDoe/blog. That is what I had assumed based on your code above for getSegment(2) == 'blog' that the first URL segment was the username. Let us know if that is the structure or if it is another way as it will help fine tune our answers. Thanks!  
> **username_0**: Yes, the URL structure is www.website.com/johndoe/blog. I've updated the question with my latest code and the error I'm getting.  
> **username_1**: Yeah I am not sure why Craft doesn't return the entry model automatically with routes but my guess would be because you may want to do any number of things with the token data. Have you considered trying to automate any of this process with a plugin? That is beyond my skill set but according the documentation a plug-in can create dedicated url's that will preload entry or category variables. http://buildwithcraft.com/docs/routing  
> **username_0**: Plugins are out of reach for me at the moment. We are wanting to get his out the door quickly, then once we get it working for the few who need it I can look into a plugin.  
> **username_0**: Now that it is all working, this is much easier to maintain and edit in Craft. In ExpressionEngine I ran into parse order issues often. I was using Stash, Zoo Visitor, Channel Images, and some other plugins for functionality. Between the somewhat unconventional approach and the third party plugins, it was very finicky to work on.   Once I got this sorted out, migrating the system over from EE only took a few hours. It is so much cleaner in Craft. Everything I was using those third party plugins for is baked into Craft.  

---

## 2 answers

### Answer by username_1 on 2015-03-26. Score: 8 | Accepted answer

You can retrieve the entry by grabbing it from the URL as a variable:
{% set entrySlug = craft.request.lastSegment %}

And then using craft.entries to find the matching content:
{% set entry = craft.entries.slug(entrySlug).find() %}

Alternatively when you set up your route, so that Craft knows which template to load when someone is requesting your entry, you can use the "slug" token which will expose a slug var in your template automatically.
{% set entry = craft.entries.slug(slug).find() %}

Let me know if I understood the question correctly and my apologies for saying anything you already knew!


---

### Answer by username_2 on 2015-03-26. Score: 6

Alan describes how to setup a route via the Settings->Routes area in the CP. If you happen to be managing file based routes you can setup a similar route like so:
return array(

    // Your Custom Section 
    'username/blog/(?P.*)' =>
    'blog/_entry'

);

The third segment in your route (?P.*) creates the variable that will be accessible in your template on that page.  You can name the dynamic route variable whatever you would like (for example, (?P.*)).
When your blog/_entry template gets loaded.  That slug variable will be available just like any other global variable.  You can access it using the standard output syntax: {{ slug }} or by passing it to your craft.entries tag:
{% set entry = craft.entries.slug(slug).first() %}

{{ entry.title }}

.slug(slug) will limit your query to just return the entry you want and, .first() will make sure you can access that entry without needing to treat it like an array and loop through it.

> **Comments:**
> **username_0**: Thanks, I'll be using file based routes. I skimmed through your video on this yesterday. I'll give it a more careful viewing to make sure I understand all this better.  

---

