如何包含外部HTML-Head? [英] How to include an external HTML-Head?

查看:277
本文介绍了如何包含外部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.phpservices.phpcontact.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆