简体   繁体   中英

Dynamic navbar in bootstrap

Is it a good programming practice to write code for navigation bar in each file? or write it once and use forever using any server side scripting language like php.

There's a principle that us programmers use called DRY (Don't Repeat Yourself). If you can help it, it's best to write things only once and then use functions / methods to refer to the code, as opposed to writing the same thing over and over again.

The most common way of doing it with navbars is to put them in a header file, along with the scripts / stylesheets for your website, and then include them at the top of every page (or if your website is MVC then you could do it dynamically and include it from the Servlet).

The way of doing this in PHP is simple, and like so:

header.php

<link rel="stylesheet" href="style.css" />
// etc. etc. - this will include your navbar code

Then in your other pages, at the top, you simply write

<?php
include 'path/to/header.php';
//Other code specific to that page

Easy peasy :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM