April 20, 201214 yr Hi, I have been making some web pages as a hobby. I have learnt the basics of HTML, CSS. PHP and so on. So far i have been able to realize most of the things i have tried but i know that my code is far from best practice. I have a couple of basic questions. Regarding headers and footers. Since they look the same and appear on all pages what is the best way to add them to all of my pages? I have done this with PHP before. Is this the way to do it? Regarding a main menu. This is also something that i want to appear on all of my pages. However i want the link to the current page to be highlighted. How do i do this dynamically? Previously i have been adding the menu code manually to all of my pages and just changing the class for the link that corresponds to the current page. But it is just so much work every time i want to make a change in the menu having to do it on every page. I guess this is it for now. Regards Rafal
April 20, 201214 yr If you've got this far it might be a good idea to get into a CMS, which have these kind of things out of the box for you. The way I did this when I was making raw php websites was to set a variable at the top of each page, and use it thusly: <?php $page = 'home'; ?> <?php include 'header.php'; ?> Then in your header file it would look like: <ul> <li><a href="#" <?php if($page == 'home'): ?> class="active" <?php endif; ?>>Home</a></li> <li><a href="#" <?php if($page == 'about'): ?> class="active" <?php endif; ?>>About</a></li> <li><a href="#" <?php if($page == 'contact'): ?> class="active" <?php endif; ?>>Contact</a></li> </ul> May not be the most efficient way, but it worked for me. But, like I say start using a CMS and you don't need to worry about it! (I love the word 'thusly') Edited April 20, 201214 yr by mrchristoph
April 20, 201214 yr Author If you've got this far it might be a good idea to get into a CMS, which have these kind of things out of the box for you. The way I did this when I was making raw php websites was to set a variable at the top of each page, and use it thusly: <?php $page = 'home'; ?> <?php include 'header.php'; ?> Then in your header file it would look like: <ul> <li><a href="#" <?php if($page == 'home'): ?> class="active" <?php endif; ?>>Home</a></li> <li><a href="#" <?php if($page == 'about'): ?> class="active" <?php endif; ?>>About</a></li> <li><a href="#" <?php if($page == 'contact'): ?> class="active" <?php endif; ?>>Contact</a></li> </ul> May not be the most efficient way, but it worked for me. But, like I say start using a CMS and you don't need to worry about it! (I love the word 'thusly') Thanks for you quick reply. I have tried doing it in a similiar way in PHP. I was just not sure if PHP is the way to go or if there are better solutions.
April 20, 201214 yr You can look into templating too - but I could only suggest PHP examples, like Twig. http://twig.sensiolabs.org/
Create an account or sign in to comment