What is <xf:if for and how can it be used?

Started Start date Start date - by Thread starter Thread starter Milen and have 0 Replies and 60 Views
Tags Tags
syntaxis tag xenforo 2

Milen

Owner
Staff member
Admin
Messages
98
Reaction score
15
Points
8
Credits
119
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.

📌 Syntax​

HTML:
<xf:if is="$condition">
    <!-- The code here will be executed if the condition is true -->
</xf:if>
It can also contain <xf:else /> if you want to display something else on false :
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>
📌 Explanation:
  • $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.
✅ Example 2: Checking if the user is an administrator
HTML:
<xf:if is="$xf.visitor.is_admin">
    <p>You are the administrator!</p>
</xf:if>
📌 Explanation:
  • $xf.visitor.is_admin returns true if the user is an admin.
✅ Example 3: Checking a user group
HTML:
<xf:if is="in_array(3, $xf.visitor.user_group_ids)">
    <p>You are part of the VIP group!</p>
</xf:if>
📌 Explanation:
  • in_array(3, $xf.visitor.user_group_ids) checks if the user is in a group with ID 3 .
✅ Example 4: Show a button only if the user has permissions for a given action
HTML:
<xf:if is="$xf.visitor.hasPermission('forum', 'postThread')">
    <a href="{{ link('forums/post-thread') }}" class="button">Create a new topic</a>
</xf:if>
📌 Explanation:
  • $xf.visitor.hasPermission('forum', 'postThread') checks if the user has permission to create threads.
✅ Example 5: Checking a value in a variable
HTML:
<xf:set var="$showMessage" value="1" />

<xf:if is="$showMessage">
    <p>This message is displayed.</p>
</xf:if>
📌 Explanation:
  • With <xf:set> we set the value of $showMessage = 1.
  • <xf:if> checks if $showMessage is true (other than 0, false, or empty ).

âš  Important:​

  1. <xf:if> does not support complex logical operators directly – &&, || and ! should be used only for simple checks.
  2. If you have complex conditions , it is recommended to process them in PHP code and pass them as a variable in the template.
  3. Nested <xf:if> are allowed , but use them carefully to avoid complicating your code.
 

Attachments

  • xfif-is.webp
    xfif-is.webp
    8.2 KB · Views: 124

More for you :)

The Repository folder
Maybe this is where we should start before creating any add-on for XenForo, but each in their own way. Here I will try to collect information...
by Milen in XenForo 2 AddOns

Thread statistics

Created
Milen,
Replies
0
Views
60

New threads

Hot

[XenBg] Portal
Day 1 I've been stubborn about making a portal anyway :D Why not start? After a lot of really hard work, I managed to get the forum to show some...
by Milen in Working on
Daily moon calendar
When we live in accordance with the lunar calendar, we begin to feel the natural rhythms that influence us and we can ride the energy wave, like a...
by Milen in Moon Calendar
[XenBg] Portal Pro [Addons]
[XenBg] Portal thread for updates and discussion. If you see this in the Last repli widget, there is likely a new update. You can take a look to...
by Milen in AddOn releases
XenBg Pro Style
Perhaps every person who owns any kind of website (whether it's a forum or a blog, it doesn't matter) has had a moment when they saw something and...
by Milen in Working on
Xenforo seo
! It's important google search engine to like your forum ! Day 1 Working on this addon is starting at 27.01.2025. I've been a webmaster for a...
by Milen in Working on
Back
Top