daveyah2002 Posted October 24, 2014 Share Posted October 24, 2014 Hi, just joined today. I am using a variable to chose a template dependent on when the user is logged in it shows a search box or not if they haven't. As a php beginner I just want to know if this is the correct method or should I be declaring 'template' somewhere specific. It is only used in this one file. I hope I've used the code tags correctly... <php? include_once("login_status.php") If(user_login==true){ $template="template_top.php" ...web page with search box }else{ ...web page without search box $template="template_top_NoSearch.php" } ?> <html> <?php include_once($template); ?> </html> Quote Link to comment https://forums.phpfreaks.com/topic/292027-correct-variable-use/ Share on other sites More sharing options...
Ch0cu3r Posted October 24, 2014 Share Posted October 24, 2014 Rather than have two templates one with a searchbox and the other without. Instead have one template for your sites over all layout. You then extract elements from that template into smaller template files which you only include when you need them. Ie in your templare file you have something like this <html> <head> <title>Site Title</title> ... etc </head> <body> ... <?php // if user logged in include the searchbar template if($user_login) include 'searchbar.php'; ?> ... <body> </html> Searchbar.php will only contain the HTML for your searchbar. The searchbar will only be shown if $user_login is true. Quote Link to comment https://forums.phpfreaks.com/topic/292027-correct-variable-use/#findComment-1494573 Share on other sites More sharing options...
daveyah2002 Posted October 24, 2014 Author Share Posted October 24, 2014 Hi Thanks for the reply, I did think about that but I got the impression that as it's a form search box and part of the template for the top of the page then I would need to exclude individual form elements. I could be wrong. To me that was more hassle than it's worth and easier to reproduce the top template with and without the form. I will have another look at it though. Many thanks. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/292027-correct-variable-use/#findComment-1494615 Share on other sites More sharing options...
jcbones Posted October 24, 2014 Share Posted October 24, 2014 One of the reasons we include individual parts into a main template, is so that when we redesign the page, we only have one page to do, not multiples. Therefore, it is less prone for errors. Quote Link to comment https://forums.phpfreaks.com/topic/292027-correct-variable-use/#findComment-1494620 Share on other sites More sharing options...
Solution daveyah2002 Posted October 27, 2014 Author Solution Share Posted October 27, 2014 Hi, thanks for the replies I managed to stick the form in a separate .php template in the end, which only displays when users logged in. Appreciate your time. Quote Link to comment https://forums.phpfreaks.com/topic/292027-correct-variable-use/#findComment-1494933 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.