如何包含外部HTML-Head? [英] How to include an external HTML-Head?
问题描述
我要重新编写一个小型企业网站的代码,在10个HTML页面上有40%的重复代码(标题和菜单)似乎很可笑.另一方面,在如此小的项目中抛出 Next.js 之类的东西似乎也太过分了.
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.
是否有一种简单的方法,通过在服务器或客户端的每个页面中都包含一个head.html来减轻重复的代码?
Is there a simple way to mitigate the duplicate code by including a head.html within every page either server- or client-side?
推荐答案
我更喜欢PHP.将您的.html(静态)页面更改为.php(动态)页面.只是另存为".您可以将几乎所有内容都分成模板.
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
index.php
包含+调用header.php
+ home.php
+ footer.php
The index.php
contains + calls the header.php
+ home.php
+ footer.php
header.php
具有菜单等.
home.php
可以具有您的滑块以及更多部分,等等.
the home.php
can have your slider, and more sections, etc.
footer.php
具有您的菜单+版权等.
the footer.php
has your menus + copyright, etc.
style.css
具有您的样式,并且您可以根据要设置的样式来包含许多.css
文件.
the style.css
has your styles and you can have many .css
files depending what you want styled.
您可以将其扩展到其他页面:about.php
,services.php
,contact.php
等.
You can expand this to your other pages: about.php
, services.php
, contact.php
, etc.
在index.php
文件中添加
<?php require ('your-folder-for-content/header.php'); ?>
<?php require ('your-folder-for-content/home.php'); ?>
<?php require ('your-folder-for-content/footer.php'); ?>
关闭页脚末尾的</html>
括号.
一个页面或很少重复的部分会调用另一个页面,从而避免了冗余.
One page or little repeated sections call the other and redundancy is prevented.
这篇关于如何包含外部HTML-Head?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!