currentUser: Impossible to invoke a method ("isInGroup") on a null variable I couldn't find this anywhere else, I'm looking up if the currentUser.isInGroup('verified') and if not, display another link. Works fine when logged in but throws a Template and Runtime error when using the {{logoutUrl}} or viewing any page when not logged in.

I expected this would work the same as currentUser by itself, which throws no errors. But obviously there's something I'm missing. Should I be using something like craft.session in combination with the isInGroup? Or some other method of detection?




Update

{% if currentUser.isInGroup('verified') is defined %}


Seems to get rid of the errors. So a couple of questions:


Is this expected behaviour?


I'm yet to wrap my head around the |length is defined etc methods of checking, and when to use them. I'll have to go over this post a few more times!
Yes, that's expected behavior.  currentUser will not exist if there is not a currently logged in user.

{% if currentUser.isInGroup('verified') is defined %}


That's one workaround for it.

Another would be:

{%if currentUser is defined and currentUser.isInGroup('verified') %}


which to me reads a little better even though it's more verbose.