在多个页面上包含相同的 html

Include one and the same html on many pages

我在很多页面上都有这个 html 文本

<head>
<meta charset="utf-8" name="viewport" content="width=device-width, height=device-height minimum-scale=1, maximum-scale=1">
<title>Account</title>
<link href="css/Master.css" rel="stylesheet"/>
    <link rel='stylesheet' media='screen and (max-width: 450px)' href='css/narrow.css' />
    <link rel='stylesheet' media='screen and (min-width: 451px) and (max-width: 800)' href='css/medium.css' />
    <link rel='stylesheet' media='screen and (min-width: 801px)' href='css/wide.css' />
</head>

问题是我想对许多页面使用相同的代码,而不必复制和粘贴比方说 5 页。有没有一种聪明的方法可以为此使用 "include" 命令?

如果您使用的是 php,您可以创建一个新文件页面-element.php 并将您的内容粘贴到该文件中。

然后在每个你想使用这个元素的页面中你可以使用

<?php
include_once "page-element.php";

你可以用这个写一个文件:

File with libs

 <meta charset='utf-8' name='viewport' content='width=device-width, height=device-height minimum-scale=1, maximum-scale=1'>
 <title>Account</title>
 <link href='css/Master.css' rel='stylesheet'/>
 <link rel='stylesheet' media='screen and (max-width: 450px)' href='css/narrow.css' />
 <link rel='stylesheet' media='screen and (min-width: 451px) and (max-width: 800)' href='css/medium.css' />
 <link rel='stylesheet' media='screen and (min-width: 801px)' href='css/wide.css' />

HTML

<head>
  <?php include("file_with_libs.php"); ?>
</head>

但我建议你使用像 Twig 这样的模板引擎。您拥有所有信息:http://twig.sensiolabs.org/

或者你可以看看我写的其他回复,解释如何使用Twig:

检查您的服务器是否配置为使用服务器端包含。这可能是一种无需 PHP.

即可实现所需内容的简单方法

我假设您使用的是 Apache,我承认这可能不正确,但 IIS 和 Ngnix 都提供 SSI 功能,尽管我不确定它们与 Apache 的相似程度。

https://httpd.apache.org/docs/current/howto/ssi.html