Jump to content

Guides

Common logic checks using HTML Logic

HTML Logic is a very powerful way of conditionally showing different elements in your theme, depending on the values of certain properties. Since the entire IPS4 framework is available within a logic expression, there's a lot of scope for using different kinds of data to determine what should show.

In this guide we'll provide a range of examples of common logic checks you might want to do. Even if the exact expression you need isn't listed here, it should provide a good starting point to help you write your own expressions.

 

Logic Recap

Let's quickly recap how HTML Logic works. For a more complete tutorial, be sure to read through the Template Syntax guide.

In IPS4, logic checks are done using the special {{if}}, {{else}} and {{elseif}} tags. As with standard programming logic, if the expression results in true, the block of code within is executed. If it is false, it isn't (and if there's an else or elseif block, that is tested for a true result instead). So, a block of logic in a template might look like this:

{{if member.member_id == 3}}
	<!-- If the member has ID 3, this will be shown -->
{{elseif member.member_id == 9}}
	<!-- But if the member has ID 9, this will be shown instead -->
{{else}}
	<!-- If the member isn't ID 3 or 9, then this will show -->
{{endif}}

If you need help constructing a logic check, feel free to check out the Customization Resources forum.

 

Examples. I want to...

Check if the user is logged in

{{if member.member_id}}
	<!-- this will show if the member is logged in -->
{{endif}}

 

Check if the user isn't logged in

{{if !member.member_id}}
	<!-- this will show if the user is a guest -->
{{endif}}

 

Check if the user's ID is one of x, y or z
You can check as many values as you like; just add more numbers to the array.

{{if in_array( member.member_id, array( 5, 28, 472 ) )}}
	<!-- Shows if the member's ID is 5, 28 or 472 -->
{{endif}}

 

Check if the user is in group x
Where x is the group ID number. Note that this also checks secondary member groups.

{{if member.inGroup('x')}}
	<!-- Shows if member is in group 'x' -->
{{endif}}

 

Check if the user has more than x posts
In IPS4, all content in all apps counts as a 'post'.

{{if member.member_posts > 3}}
	<!-- Shows if the member has more than 3 posts -->
{{endif}}

 

Check if the user has fewer than x posts
In IPS4, all content in all apps counts as a 'post'.

{{if member.member_posts < 3}}
	<!-- Shows if the member has fewer than 3 posts -->
{{endif}}

 

Check if the user is an administrator
Note that this also checks if any of the user's secondary member groups has admin permissions.

{{if member.isAdmin()}}
	<!-- Shows if the user is an administrator -->
{{endif}}

 

Check if the user is banned

{{if member.isBanned()}}
	<!-- Shows if the user is banned -->
{{endif}}

 

Check if the current page is part of app x
You need to check the application key. Most are obvious (e.g. forums is the forums app), but there are some others to be aware of. For custom/third-party apps, ask the author which app key they use.

  • core = Any system page that isn't part of another app, e.g. search, login/registration, profiles etc.
  • cms = Pages
  • nexus = Commerce
{{if request.app == 'forums'}}
	<!-- Shows if the user is viewing any page in the 'forums' app -->
{{endif}}

 

Check if a system setting has value x
You can check whether system settings have a given value, although you will need to know the setting key used by the backend. Values may not be simple to check, depending on their type - consult our Customization Resources forum if you aren't sure how to check a particular setting.

{{if settings.auto_polling_enabled}}
	<!-- Shows if the 'auto_polling_enabled' setting is true (i.e. enabled) -->
{{endif}}

 

Check a variable in a template has value x
Template bits in IPS4 may receive one or more variables from the backend code. You can check the values of these within the template to do something based on the value. This only works within the template into which the variable you are checking is passed - they are not inherited.

{{if $myVariable == 'some_value'}}
	<!-- Shows if $myVariable is equal to 'some_value' -->
{{endif}}

 

Check if the current forum is forum ID x
Within the forums app, you can check whether the current page is showing the forum with ID x

{{if request.app == 'forums' && request.module == 'forums' && request.id == 3}}
	<!-- Shows if the user is in the forums app, viewing a forum with the ID 3 -->
{{endif}}

.

Edited by Rikki

  Report Guide


×