Conditionals Based on User Authentication
Learn how to show or hide content in templates based on user authentication, including guest access, admin privileges, user groups, and listing ownership.
Sometimes you may want to write a condition to show or hide content based on user authentication information. JReviews has an authentication service made available in templates through the $this->auth
variable.
Below you'll find a few examples of how this can be used in templates, and of course you can mix and match at your discretion.
Visible to Guests
<?php if ($this->auth->guest): ?>
Visible content
<?php endif;?>
Visible to Admin
<?php if ($this->auth->admin): ?>
Visible content
<?php endif;?>
Visible to Logged In User
<?php if ($this->auth->connected): ?>
Visible content
<?php endif;?>
Visible to User Groups
<?php if ($this->auth->belongsToGroups([1,2,3])) :?>
Visible content for users in user group IDs 1, 2 and 3
<?php endif;?>
Or you can show for users that don't belong to a group:
<?php if ($this->auth->notBelongsToGroups([1,2,3])) :?>
Visible content for users not in user groups IDs 1, 2 or 3
<?php endif;?>
Visible to Listing Owner
<?php if ($listing['User']['user_id'] == $this->auth->id): ?>
Content visible to listing owner
<?php endif; ?>