So what I'd like is:
If URI would look like {username}, load _thisTemplate.
Couldn't find the way without solid point like author ID.
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It only takes a minute to sign up.
Sign up to join this communitySo what I'd like is:
If URI would look like {username}, load _thisTemplate.
Couldn't find the way without solid point like author ID.
I would not particularly advise creating a uri pattern that was just 'www.site.com/username' because usernames can essentially be anything which might cause routing conflicts (i.e. if the username is 'index' for example). You could however use something like 'www.site.com/members/username' or 'www.site.com/u/username'.
One way to do this would be to add a route using a 'subpattern'. For more info check out the routing docs on accessing subpatterns in your templates.
Here is the example given in the docs, which provides the variables 'year' and 'month' to the template that correspond to the matched uri segments (i.e. `news/2015/02'):
'news/(?P<year>\d{4})/(?P<month>\d{2})' => 'news/_archive',
You could essentially do the same thing but with a 'username' variable.
'u/(?P<username>[^\/]+)' => '_thisTemplate',
You can then use the 'username' variable in your template to retrieve the corresponding user.
{% set user = craft.users.username(username).first() %}
{% if user %}
{{ user.firstName }}
...
{% else %}
{% redirect 404 %}
{% endif %}
{% set user %}
you're overwriting Craft's global user
variable (i.e. the currently logged in user). Not a problem in most cases, but probably worth mentioning.
– Mats Mikkel Rummelhoff
Jun 1 '15 at 18:06
(?P<username>[^\/]+)
– carlcs
Jun 1 '15 at 20:30
/people/phil-collins/832
... – tom Jun 1 '15 at 12:53