How to include an external HTML-Head?

為{幸葍}努か 提交于 2020-01-24 20:37:06

问题


I'm back to coding a small business website and it seems silly to have like 40% duplicate code (head and menu) across 10 HTML-pages. On the other hand, it also seems overkill to throw something like Next.js at such a small project.

Is there a simple way to mitigate the duplicate code by including a head.html within every page either server- or client-side?


回答1:


I prefer PHP. Change your .html (static) pages into .php (dynamic) pages. Just 'save as'. You can separate pretty much anything into templates.

You have the following simplified file structure:

index.php

header.php

home.php

footer.php

style.css

The index.php contains + calls the header.php + home.php + footer.php

The header.php has your menus, etc.

the home.php can have your slider, and more sections, etc.

the footer.php has your menus + copyright, etc.

the style.css has your styles and you can have many .css files depending what you want styled.

You can expand this to your other pages: about.php, services.php, contact.php, etc.

Into index.php file you add something like

<?php require ('your-folder-for-content/header.php'); ?>

<?php require ('your-folder-for-content/home.php'); ?>

<?php require ('your-folder-for-content/footer.php'); ?>

Close </html> bracket at the end of your footer.

One page or little repeated sections call the other and redundancy is prevented.




回答2:


You can't do this with client JavaScript coz it running after header parsing. You should use server side technologies for this. From SSI to PHP :)




回答3:


Jekyll is pretty good and easy to use. You can strip off all the features you don't need. Has template filling available as well. It outputs static html for the whole site, so you don't need your server to be running any daemons.



来源:https://stackoverflow.com/questions/58318334/how-to-include-an-external-html-head

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!