<p>If you're looking for the current (logged in) user, you should use the <a href="http://buildwithcraft.com/docs/templating/global-variables#currentUser" rel="nofollow">global variable currentUser</a>.</p>

<pre><code>{% if currentUser %}
    {# currentUser returns a User Model if there is one, so do with it whatever you like #}
{% endif %}
</code></pre>

<p>However, if you want to find a user through a URL segment, you might want to get into <a href="http://buildwithcraft.com/docs/routing" rel="nofollow">using routes</a>. If you use advanced routing, you can get what (I think) you want by adding following to your <code>craft/config/routes.php</code>:</p>

<pre class="lang-php prettyprint-override"><code>return array(
    '(?P&lt;category&gt;[^/\]+)/(?P&lt;user&gt;[^/\]+)/' =&gt; 'path/to/template',
)
</code></pre>

<p>In your template, you can then use the variables <code>{{ category }}</code> and <code>{{ user }}</code>. Don't hang me on the RegExp-pattern, this is not tested but should get you further.</p>

CC BY-SA 3.0
2014-07-16T20:51:03.073Z
I'm looking to relate authored entries based on a username in the URL (i.e. `.com/news/sherlock_holmes`) without being logged in and without saving segment 1 to a variable first.
Ryan Shrum
CC BY-SA 3.0
2014-07-17T13:54:11.880Z
Why don't you want to save the segment?
Anna_MediaGirl
CC BY-SA 3.0
2014-07-18T13:55:51.030Z
Because the segment can be a category or tag as well (in this particular case). Really I'm just wondering, and seems to be not possible currently, if the conditional for user in my question will return true similarly to the category conditional when it exists in the URL.
Ryan Shrum
CC BY-SA 3.0
2014-07-16T20:39:52.337Z
2014-07-18T14:28:40.073Z
2014-07-18T14:28:40.073Z
Paul
<p>Similarly to how Craft knows that a category is defined when a category slug is used, is there a way to detect a user in the same way?</p>

<pre><code>{% if category is defined %}
...
{% endif %}
</code></pre>

<p>Something along the lines of (which only returns true if a user is logged in):</p>

<pre><code>{% if user is defined %}
...
{% endif %}
</code></pre>

<p>If a username is in the URL, would/should above code return true similarly to how <code>{% if category is defined %}</code> returns true if a category slug is in the URL? (i.e. <code>.com/news/sherlock_holmes</code>)</p>

<p>I'm able to return author posts using method listed in <a href="https://craftcms.stackexchange.com/questions/719/how-do-i-list-entries-from-a-single-author">this question</a>, I'm just trying to simplify even further and have one template <a href="https://craftcms.stackexchange.com/questions/1168/sharing-a-template-with-identical-category-and-tag-name">list all these views</a>.</p>

<entries><templating><users>
Setting a conditional for username in URL?
# Setting a conditional for username in URL?

(Asked by: username_0 on 2014-07-16)

Similarly to how Craft knows that a category is defined when a category slug is used, is there a way to detect a user in the same way?
{% if category is defined %}
...
{% endif %}

Something along the lines of (which only returns true if a user is logged in):
{% if user is defined %}
...
{% endif %}

If a username is in the URL, would/should above code return true similarly to how {% if category is defined %} returns true if a category slug is in the URL? (i.e. .com/news/sherlock_holmes)
I'm able to return author posts using method listed in this question, I'm just trying to simplify even further and have one template list all these views.

**Tags:** entries, templating, users

---

## 1 answers

### Answer by username_1 on 2014-07-16. Score: 2

If you're looking for the current (logged in) user, you should use the global variable currentUser.
{% if currentUser %}
    {# currentUser returns a User Model if there is one, so do with it whatever you like #}
{% endif %}

However, if you want to find a user through a URL segment, you might want to get into using routes. If you use advanced routing, you can get what (I think) you want by adding following to your craft/config/routes.php:
return array(
    '(?P[^/\]+)/(?P[^/\]+)/' => 'path/to/template',
)

In your template, you can then use the variables {{ category }} and {{ user }}. Don't hang me on the RegExp-pattern, this is not tested but should get you further.

> **Comments:**
> **username_0**: I'm looking to relate authored entries based on a username in the URL (i.e. `.com/news/sherlock_holmes`) without being logged in and without saving segment 1 to a variable first.  
> **username_2**: Why don't you want to save the segment?  
> **username_0**: Because the segment can be a category or tag as well (in this particular case). Really I'm just wondering, and seems to be not possible currently, if the conditional for user in my question will return true similarly to the category conditional when it exists in the URL.  

---

