IMO,最好的方法通常是将 HTML 单独存储在模板文件中。这是一个通常包含 HTML 的文件,其中包含一些需要填写的字段。然后,您可以使用一些模板框架根据需要安全地填写 html 文档中的字段。
Smarty是一个流行的框架,这里有一个如何工作的例子(取自 Smarty 的速成课程)。
模板文件
<html>
<head>
<title>User Info</title>
</head>
<body>
User Information:<p>
Name: {$name}<br>
Address: {$address}<br>
</body>
</html>
将名称和地址插入模板文件的 PHP 代码:
include('Smarty.class.php');
// create object
$smarty = new Smarty;
// assign some content. This would typically come from
// a database or other source, but we'll use static
// values for the purpose of this example.
$smarty->assign('name', 'george smith');
$smarty->assign('address', '45th & Harris');
// display it
$smarty->display('index.tpl');
除了 Smarty 之外,还有许多合理的模板框架选择可以满足您的口味。有些很简单,许多具有一些相当复杂的功能。