In XenForo 2 , <xf:if> is a conditional template tag that is used to check a condition in XenForo templates. It allows for dynamically showing or hiding elements based on certain logic.
It can also contain <xf:else /> if you want to display something else on false :
Explanation:
Example 2: Checking if the user is an administrator
Explanation:
Example 3: Checking a user group
Explanation:
Example 4: Show a button only if the user has permissions for a given action
Explanation:
Example 5: Checking a value in a variable
Explanation:
Syntax
HTML:
<xf:if is="$condition">
<!-- The code here will be executed if the condition is true -->
</xf:if>
HTML:
<xf:if is="$condition">
<!-- Code if condition is true -->
<xf:else />
<!-- Code if condition is false -->
</xf:if>
Usage examples
Example 1: Checking if the user is logged in to their account
HTML:
<xf:if is="$xf.visitor.user_id">
<p>Hello, {$xf.visitor.username}!</p>
<xf:else />
<p>Please <a href="{{ link('login') }}">log in</a> to your account.</p>
</xf:if>
![Pushpin :pushpin: 📌](https://cdn.jsdelivr.net/joypixels/assets/8.0/png/unicode/64/1f4cc.png)
- $xf.visitor.user_id checks if there is a logged in user.
- If there is, it shows "Hello, {username}!"
- If not, it shows a login link.
![Check mark button :white_check_mark: ✅](https://cdn.jsdelivr.net/joypixels/assets/8.0/png/unicode/64/2705.png)
HTML:
<xf:if is="$xf.visitor.is_admin">
<p>You are the administrator!</p>
</xf:if>
![Pushpin :pushpin: 📌](https://cdn.jsdelivr.net/joypixels/assets/8.0/png/unicode/64/1f4cc.png)
- $xf.visitor.is_admin returns true if the user is an admin.
![Check mark button :white_check_mark: ✅](https://cdn.jsdelivr.net/joypixels/assets/8.0/png/unicode/64/2705.png)
HTML:
<xf:if is="in_array(3, $xf.visitor.user_group_ids)">
<p>You are part of the VIP group!</p>
</xf:if>
![Pushpin :pushpin: 📌](https://cdn.jsdelivr.net/joypixels/assets/8.0/png/unicode/64/1f4cc.png)
- in_array(3, $xf.visitor.user_group_ids) checks if the user is in a group with ID 3 .
![Check mark button :white_check_mark: ✅](https://cdn.jsdelivr.net/joypixels/assets/8.0/png/unicode/64/2705.png)
HTML:
<xf:if is="$xf.visitor.hasPermission('forum', 'postThread')">
<a href="{{ link('forums/post-thread') }}" class="button">Create a new topic</a>
</xf:if>
![Pushpin :pushpin: 📌](https://cdn.jsdelivr.net/joypixels/assets/8.0/png/unicode/64/1f4cc.png)
- $xf.visitor.hasPermission('forum', 'postThread') checks if the user has permission to create threads.
![Check mark button :white_check_mark: ✅](https://cdn.jsdelivr.net/joypixels/assets/8.0/png/unicode/64/2705.png)
HTML:
<xf:set var="$showMessage" value="1" />
<xf:if is="$showMessage">
<p>This message is displayed.</p>
</xf:if>
![Pushpin :pushpin: 📌](https://cdn.jsdelivr.net/joypixels/assets/8.0/png/unicode/64/1f4cc.png)
- With <xf:set> we set the value of $showMessage = 1.
- <xf:if> checks if $showMessage is true (other than 0, false, or empty ).
âš Important:
- <xf:if> does not support complex logical operators directly – &&, || and ! should be used only for simple checks.
- If you have complex conditions , it is recommended to process them in PHP code and pass them as a variable in the template.
- Nested <xf:if> are allowed , but use them carefully to avoid complicating your code.