Forum

Thread tagged as: Question, Members, Shop

Showing navigation links based on a member logged in or out

Hi,

I already have a working version of this but I'd like to know if there's a more elegant solution. I've created two conditional links in a layout file (i.e. perch/templates/layouts/global.top.php). One link is shown if the user is logged in as a member, The other is shown if the user is not logged in. The Member App is installed as a requirement the Shop App, it's not being used for anything else. The code I'm using works fine:

<li>
                <?php 
                    if (perch_member_logged_in()) {
                        echo '<a href="#">Account</a>';
                    }

                    else {
                        echo '<a href="#">Login/Register</a>';
                    }
                ?>
            </li>

But I can't help thinking there might be a more elegant solution? Or one that separates logic and presentation a little better? Any thoughts appreciated.

Martin Jewiss

Martin Jewiss 0 points

  • 4 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Have you marked the page as requiring login using the page options?

Hi Drew

Do you mean the page options available in Perch's admin area? Or page options defined in a php statement in perch/templates/pages?

I can't see any require login in the options view for a page in Perch's admin area.

I'm working on the global main menu that's defined as a layout. It gets pulled into the top of every page on the site. I'm switching one link in the main menu from Register/Login to Account based on whether the user is logged in. I'm not using this to restrict access to a page based on whether the user is logged in.

I found the template tag reference for Logged in Conditional at https://docs.grabaperch.com/templates/apps/members/logged-in/

<perch:member logged-in="true">
     <p>You are logged in.</p>
 <perch:else:member />
     <p>You are not logged in.</p>
 </perch:member>

It doesn't appear to work in my templates at the moment (in layouts or pages) but I guess this could be the "more elegant" solution I was looking for?

Drew McLellan

Drew McLellan 2638 points
Perch Support

When you to go the Settings tab for a page there's an option "Restrict access to members with tags". Are you using this?

Thanks for clarifying Drew, no I'm not using the "Restrict access to members with tags". I guess the <perch:member logged-in="true"> works with tags assigned to members.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok, would that not work here?

I can't figure out how it would work in this situation Drew. Unless I'm missing something about how the member tags work.

I'm swapping between two links in the global (site-wide) navigation menu that's included on every page in the site. The links are swapping between Login/Register and Account across the whole site based on whether the user is logged in as a member or not. I'm not restricting access to any pages (yet) or displaying different navigation on different pages.

Drew McLellan

Drew McLellan 2638 points
Perch Support

I think probably the best thing to do is add a page attribute to indicate the link requires a login, and then test for that in your template.

Thanks Drew. I'll check that out.