2

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?

enter image description here enter image description here

Update

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

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

  • Is this expected behaviour?
1

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.

  • Perfect, good to know. Using currentUser on it's own (without is defined) is perfectly fine though, right? – Rob Nov 25 '15 at 0:28
  • 2
    Yes, you can just do: {%if currentUser and currentUser.isInGroup('verified') %} – andrew.welch Nov 25 '15 at 0:31
  • Nice one, thanks @khalwat ... that feels a little cleaner – Rob Nov 25 '15 at 0:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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