Conditional Statements for XenForo 2

Conditional Statements for XenForo 2

ehd

Well-known member
Great!

Should this:


6. How can I show different content to members and guests?

Code:
<xf:if is="!$xf.visitor.user_id">
Show only members
<xf:else />
Show only guests
</xf:if>


not be:

6. How can I show different content to members and guests?

Code:
<xf:if is="$xf.visitor.user_id">
Show only members
<xf:else />
Show only guests
</xf:if>

?

The "!" could be too much?
 

Alan_SP

Active member
To the best of my knowledge this should work; i don't see a reason why it wouldn't.. 🤔
This is error I get:
Line 28: Found a contentcheck tag without a contentcheck-based if tag. - Template modifications: public:message_macros
And I used it like this for example I mentioned:
HTML:
$0

                    <xf:if is="$xf.visitor.is_admin OR $xf.visitor.is_moderator">
                    <dl class="pairs pairs--justified">
                        <dt>{{ phrase('last_seen') }}</dt>
                        <dd dir="auto">
                            <xf:contentcheck><xf:useractivity user="$user" class="pairs--plainLabel" /></xf:contentcheck>
                        </dd>
                    </dl>
                    </xf:if>
 

nocte

Well-known member
The error message says all ;)

You can try adding 2 lines:


HTML:
$0

                    <xf:if is="$xf.visitor.is_admin OR $xf.visitor.is_moderator">
<xf:if contentcheck="true">
                    <dl class="pairs pairs--justified">
                        <dt>{{ phrase('last_seen') }}</dt>
                        <dd dir="auto">
                            <xf:contentcheck><xf:useractivity user="$user" class="pairs--plainLabel" /></xf:contentcheck>
                        </dd>
                    </dl>
</xf:if>
                    </xf:if>
 

Dragonfruit

Well-known member
Hi, how would you handle an apostrophe when writing an if conditional statement, example, if the data was "Jack's iPhone", that apostrophe would conflict with the statement:< xf:if is="{$xf.visitor.Profile.custom_fields.PhoneName} == 'Jack's iPhone'">

Just wondering how to correctly fix this.
 

nicodak

Well-known member
Did you try <xf:if is="{$xf.visitor.Profile.custom_fields.PhoneName} == 'Jack\'s iPhone'"> ?
It's really random... no certainty on that.
 

Dragonfruit

Well-known member
Did you try <xf:if is="{$xf.visitor.Profile.custom_fields.PhoneName} == 'Jack\'s iPhone'"> ?
It's really random... no certainty on that.

I just tried that, unfortunately it didn't work, I originally tried putting a back slash before and after it.
 

nocte

Well-known member
Try this:

HTML:
<xf:set var="$var" value="Jack's iPhone" />
<xf:if is="{$xf.visitor.Profile.custom_fields.PhoneName} == $var">

untested, but may work.
 

TheSkullKid

Active member
Why is it not working in XenForo?

If I do that via PHP it works:
PHP:
if (!(FALSE OR TRUE OR TRUE)) {
    echo "5)\n";
}

if (!(FALSE AND TRUE AND TRUE)) {
    echo "6)\n";
}
Code:
Result:
6)

but in Xenforo not:
PHP:
<xf:if is="(!(FALSE OR TRUE OR TRUE))">
FTT.
</xf:if>

<xf:if is="(!(FALSE AND TRUE AND TRUE))">
FTT and.
</xf:if>
Code:
Result:
FTT.
FTT and.

and why does array_intersect do not work.
PHP:
<xf:if is="(array_intersect([3,4], [3,4,5,6]))">
Result
</xf:if>
 

Brogan

XenForo moderator
Staff member
Template code is not a substitute for PHP.

The is attribute supports logical operators:
  • OR / || - used to link alternative conditions
  • AND / && - used to link additional conditions
  • ! - place before a condition to invert it
  • XOR - returns true if only one of two conditions is true
Look at src/XF/Template/Templater.php to see what filters and functions are available - array_intersect is not.
 

nocte

Well-known member
Why is it not working in XenForo?
Interesting question!

I gave your code a test run and it was compiled to:

PHP:
    if (!(false OR true) OR true) {
        $__finalCompiled .= '
FTT.
';

Template code is not a substitute for PHP.
Yeah, but this seems to be a bug in XF core, because the template code is translated to something different.

Bug-report:

 

TheSkullKid

Active member
Interesting question!

I gave your code a test run and it was compiled to:

PHP:
    if (!(false OR true) OR true) {
        $__finalCompiled .= '
FTT.
';


Yeah, but this seems to be a bug in XF core, because the template code is translated to something different.

Bug-report:

How did you get the compiled code?
 
Top