Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

I'm running some checks to see if there is a user logged in, and if that user is someone specific. When accessing user.isCurrent it's returning 1 when I'm logged in, but returns null when I'm not. Should this be returning 0 or false in that case, or should I be checking {% if user.isCurrent is defined %} ?

share|improve this question

1 Answer 1

up vote 4 down vote accepted

To determine if the user is logged in at all, you'll want to use the currentUser variable:

{% if currentUser %}
    Welcome, {{ currentUser.friendlyName }}!
{% endif %}

To determine if the logged in user is a specific user, then simply compare against their username:

{% if currentUser and currentUser.username is 'johndoe' %}
    Hey Johnny Boy!
{% endif %}

The isCurrent method is useful when targeting a more generic UserModel (where you don't know if the user would be the current user).

{% for user in craft.users %}
    {% if user.isCurrent %}
        {{ user.friendlyName }} is you!
    {% else %}
        {{ user.friendlyName }} is someone else.
    {% endif %}
{% endfor %}
share|improve this answer
    
This works great thanks for clarifying. I did have an issue with using is in this context though: Unexpected token "string" of value "bill" ("name" expected) I swapped is with == and it seems to working fine now. Maybe some kind of typecast comparison issue? –  Bill Columbia Jun 20 at 18:21
    
Huh, interesting... I wonder if it's just a grouping issue. Perhaps put parenthesis around the second part and try it as if currentUser and (currentUser.username is 'johndoe'). In theory, is should be parsed exactly the same as ==. twig.sensiolabs.org/doc/templates.html#test-operator –  Lindsey D Jun 20 at 18:29
    
Here's another question addressing the topic of is vs is same as vs == vs === ... craftcms.stackexchange.com/questions/218/… –  Lindsey D Jun 20 at 18:34
    
@BillColumbia I just bumped into that same issue on a site I'm working on (is vs ==), so I did a little more digging. It looks like is represents a "test operator"... meaning, it runs tests. Meanwhile, == is a "comparison operator". So, I learned something new today... is does not directly translate to == –  Lindsey D Jun 21 at 0:50
    
Interesting! Thanks for posting that! –  Bill Columbia Jun 21 at 3:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.