1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

XF 1.5 Conditional If On Certain Page

Discussion in 'Styling and Customization Questions' started by L33t, Aug 23, 2016.

  1. L33t

    L33t New Member

    Hey Guys!

    Long time lurker, first time buyer/poster. Working on getting things set up and I'm hitting a roadblock. I added a menu item to the navigation template and now I am trying to add the class "selected" when on a certain page or route.

    Here is the code I got right now:

    Code:
    <li class="navTab PopupClosed {xen:link 'pageName', 'selected'}"><a href="https://www.mysite.com/pageName" class="navLink">Page Name</a></li>
    I've tried looking at the examples for conditionals, but those didn't seem to help me out. Anyone got an idea of what to change this to?

    - L33t
     
  2. Arty

    Arty Well-Known Member

    xen:link creates a link, its not a conditional statement. xen:if is. Then for conditional statement to work you need something to compare it to.

    However you are doing it wrong. Even if you do get conditional statement to work, the rest of code in navigation and navigation_visitor will mess your code up because there will be another tab that is selected. 2 selected tabs will cause secondary navigation to overlap.

    Selected tab is not decided in template. Template only adds class "selected" to currently selected tab, it doesn't choose which tab to select. Tab is selected in route handler. For example, look in any php file in library/XenForo/Route/Prefix/. Those are route handlers for different routes. Add-ons have similar files for their routes. In each file you'll find function match() and near end of function you'll find line like this
    Code:
    return $router->getRouteMatch('XenForo_ControllerPublic_Forum', $action, 'forums');
    Third parameter tells forum which tab to select.

    So if you want to change selected tab, you should do it in route that handles your page.

    Another issue is adding tab. You shouldn't do it by editing navigation template, you should add it in php code. Add event handler for navigation_tabs event, add your custom tab in that event handler.

    Most likely easiest solution is to use one of add-ons for that instead of writing your own code. Take a look at this add-on: https://xenforo.com/community/resources/custom-tabs.4117/
     
  3. L33t

    L33t New Member

    Hey Arty,

    Thanks for the reply. I hate adding add ons/plugins so I like to avoid them if I can. It looks like in this case it might not have been the best option. :D I'll take a look at that add-on and consider changing my ways based on your advice.
     

Share This Page