Question

Seedword

Are there any best practices for managing duplicate HTML code on static websites?

Asked by Seedword 3 months ago code html static

Answers

Hyperlexic
1

Best Answer

Best Answer

 
This is just for anyone who is completely unfamiliar with PHP. Anyone who isn't familiar with backend programming might be intimidated or confused by using PHP to include the HTML on all your pages, but it's really easier than most people think. For practice (assuming your host supports PHP) create three files  - header.php, content.php and footer.php.

Create the header using the same HTML you're used to. I start with the opening HTML / doctype I usually end it at the closing </head> tag. I include my scripts and stylesheets here too.

Content.php is whatever the site's content is - usually pumped in from a database, but it can be anything. This is usually all your stuff from the opening and closing body tags.

Footer is just like the header. Anything i want attached to the bottom of all my pages. Close this with the closing html tag.

Put all three files in the same folder for now. Then - within the content.php file, use this code

<?phpinclude 'header.php';?>

(edit: Should be a space between 'php' and 'include'. How do we post code snippets here?)

//put your main content here (can be included as well)

<?phpinclude 'footer.php';?>

This way, if you end up having 100s of pages, and you want to edit your header or footer (or any other part of your site you included) you will only have to update one file and not 100s. This is extremely powerful for including scripts like jQuery or Google Analytics as well.

by Hyperlexic 2 months ago

unregistered
0
 
Agree with using php includes for shared resources, you can the use variables for minor visual modifiers rather than serving multiple copies of virtually identically content.

by unregistered 3 months ago

Zhuoshi Xie
0
 
There are also CMS's that are aimed at static sites. For example, Frog and GetSimple.

by Zhuoshi Xie 3 months ago

Andrew Miller
0
 
If you want to have a piece of HTML repeated on multiple pages (without cut&paste) try using a server side include #include commonmenu="commonmenu.html"

do a web search for HTML include (I can't seem to type & preserve or cut & paste code)

On some servers you may have to change the extension to shtml

or in php use the include('commonmenu.php');

by Andrew Miller 3 months ago

Devone
0
 
You can run a simple PHP based solution. It's easy to handle, will clean your code up, and more. Running a script on every page load so that it servers a static file will slow down your responses and will ultimately stress the CPU. You can also take a look at various PHP frameworks that'll take your PHP generated pages and create static HTML's for you. You will in return get static HTML pages that take up minimal CPU, and load pages at a faster rate.

This static site generator also works well (Jekyll).

by Devone 3 months ago

Answer this question

Are there any best practices for managing duplicate HTML code on static websites?

0 errors found:

 
0