Jump to content

Invision Community Blog


Managing successful online communities

Rikki
Sign in to follow this  
 

Theme Tip: Use HTML logic to display content to specific groups

HTML Logic is our name for the additional tags available in IPS4's templates that allow runtime logic to be executed. It comprises if/then/else statements as well as loops and more.

Since HTML Logic has access to all of the underlying PHP framework in IPS4, it's very powerful and a lot can be achieved with it. One common use is to limit certain content within a template to particular member groups. Let's see how that might be done.

 

Showing or hiding content only to guests

We'll first look at a simpler idea: showing or hiding content specifically to guests (i.e. anyone who isn't logged in). Within IPS4, the \IPS\Member::loggedIn() object contains information about the current user. Guests always have a member_id of NULL (i.e. no value), so we can simply check that value in our logic tag:

{{if \IPS\Member::loggedIn()->member_id === NULL}}
	This content *only* shows to guests, since they have a NULL member_id.
{{endif}}

{{if \IPS\Member::loggedIn()->member_id}}
	This content *only* shows to logged-in users since their member_id is a number, which will equal true.
{{endif}}

 

Showing content only to specific groups

Let's go a bit further and this time show content to specific (primary) member groups. First, you need to get the IDs for the group(s) you want to deal with. You can find this by editing the group in the AdminCP, and making a note of the id parameter in the URL. On my installation, the Administrator group is ID 4 so we'll use that in our example.

Once again, we're using the \IPS\Member::loggedIn() object, but this time we're using the member_group_id property.

{{if \IPS\Member::loggedIn()->member_group_id === 4}}
	This content only shows to members in the "Administrators" group (ID 4 in our example)
{{endif}}

 

Working with multiple groups at once

Following the code above, you could simply repeat the check against \IPS\Member::loggedIn()->member_group_id several times, for each ID you want to allow. However, since our templates allow arbitrary PHP expressions to be used, there's a neater way: use an array of member group IDs you want to allow, and check against that using PHP's in_array function. Here's an example where we only show content to group IDs 2, 4 and 6:

{{if in_array( \IPS\Member::loggedIn()->member_group_id, array( 2, 4, 6 ) )}}
	This content only shows to members in groups with the ID 2, 4 or 6.
{{endif}}

 

Have a request for a theme tip? Let us know in the comments and we'll try and help out in a future tip! 

Sign in to follow this  

Comments

Recommended Comments

Custom themes are definitely different, aren't they? So having content available for certain groups wouldn't be a messy job for a high modified theme.

make things interesting, members group have the ability to view a content, and what if I want 20% of those people in members group to not have the ability to view it without the need to move them to another group. Sounds impossible

Edited by EmpireKickAss

Share this comment


Link to comment
Share on other sites

@Rikki there is a shorthand way to get value of a custom profile fields on template?

like this

\IPS\Member::loggedIn()->profileFields()->field_26 or get by key without destroy page with error...

 

now i adopt this:

{{$member = \IPS\Member::loggedIn();}}
{{if $pf=$member->profileFields() AND isset($pf['core_pfieldgroups_ID GROUP']['core_pfield_ID FIELD'])}}
{$pf['core_pfieldgroups_ID GROUP']['core_pfield_ID FIELD']}
{{endif}}
Edited by BomAle

Share this comment


Link to comment
Share on other sites
19 hours ago, IBResource ltd. said:

 

in the 3.x versions, we can determine the forum id by $ this->request ['f'] in every template even in topicView and globalTemplate. Whether it is possible to do the same in 4th version?

this could give more issue than utils... i prefer adopt this below anyway:

{{if request.app == 'forums' and request.module == 'forums'}}
	{{$forum_id = request.controller == 'topic' ? request.forum_id : request.id;}}
	FORUM ID IS: {$forum_id}
{{endif}}

 

Edited by BomAle

Share this comment


Link to comment
Share on other sites
4 hours ago, BomAle said:

this could give more issue than utils... i prefer adopt this below anyway:


{{if request.app == 'forums' and request.module == 'forums'}}
	{{$forum_id = request.controller == 'topic' ? request.forum_id : request.id;}}
	FORUM ID IS: {$forum_id}
{{endif}}

 

This does not work in topics.

Quote

When you view a forum forum id = \IPS\Request::i()->id

When you view a topic first you need get topic data


$topic = \IPS\forums\Topic::loadAndCheckPerms(\IPS\Request::i()->id);

Then forum id = $topic->forum_id


On 28.03.2016 at 1:07 AM, Martin A. said:

You can also used the built-in inGroup method to check if a member is in a certain group, instead of the in_array option

inGroup check primary and secondary groups.

Share this comment


Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...