Template: Inline conditional to match a string


In the navigation template I see the following line which adds a class based on if a variable is true or false:
{xen:if $tabs.forums.selected, 'selected', 'Popup PopupControl PopupClosed'}
I'd like to be able to check if a variable matches a string, such as:
<!-- games -->
<li class="navTab games {xen:if $selectedCustomTab == 'games', 'selected', ''}"><a href="../games/" class="navLink">Games</a></li>
What I want to achieve is applying a 'selected' class to a tab if the $selectedCustomTab is set to that tab's string, in this case "games". Is there any way I can check this?

Chris D

XenForo developer
Staff member
<!-- games -->
<li class="navTab games {xen:if '{$selectedCustomTab} == "games"', 'selected'}"><a href="../games/" class="navLink">Games</a></li>
That should work.

Note: xen:if accepts three parameters. The conditional, the if and the else. The third parameter, the else statement, isn't necessary unless you need to do something - which from your code example you aren't.

Also, are you doing this the right way?

You can add custom tabs by creating an Event listener using the navigation_tabs event.

Also, you'll be better using XenForo links instead of absolute/relative links as they automatically cater for various factors including whether the board has friendly URLs enabled or not.

So you would use:

{xen:link games} and depending on the configuration the link would automatically become index.php?games or simply /games/


Thank you for your reply, @Chris Deeming !

I'm using absolute URLs because the Games section is in my main application at mysite.com/games/ whereas the forums are at mysite.com/community/. I'm loading the XenForo in my custom (CakePHP) application as well, so that the header is the same on both applications, giving the user a sense of integration. It's a lot of hassle, seeing as the URLs loaded by XenForo in my main application are all relative, and I need to prepened "/community/" to all URLs in href and src attributes (for anchor links and images). But it's the only way I found that gives me the result that I'm looking for.

Comparing strings like in your example does not seem to give me any syntax errors, thanks for that!

I would love to implement the approach with an Event Listener like you said, but would not know where to begin implementing such a thing. Is this done through an addon?