我有一个网站,其中大部分内容(如侧边栏,背景等)在网站的大部分页面上都是相似的。
在 ASP.NET 中,对于这种情况,有母版页。html
或php
中易于使用的简单等价物是什么?(从未使用过PHP工具,网站是简单的html,但主机是PHP服务器)
其次,是否有可以避免下载冗余内容并为用户加快速度的东西?
这通常是在 PHP 中通过包含完成的。 查看include()
、include_once()
、require()
和require_once()
。
您可以将页面的各个部分放在它们自己的单独文件中,并以这种方式单独管理它们。
关于缓存,这只是为您要查找的内容设置适当的缓存标头的问题。 最佳做法是将静态资源(JavaScript,CSS等)保存在自己的单独文件中,以便它们可以更轻松地缓存在整个站点中。
就我个人而言,我总是在php网站中使用smarty,因为它为您提供了将代码与标记分开的可能性,就像在dot net中一样。
我通常做这样的事情
class masterpage
{
protected $subpage;
public function output()
{
$smarty = new Smarty();
$smarty->assign('subpage', $this->subpage);
return $smarty->fetch('masterpage.tpl');
}
}
class helloworld extends masterpage
{
public function __construct()
{
this->subpage = 'helloworld.tpl';
}
}
class ciao extends masterpage
{
public function __construct()
{
this->subpage = 'ciao.tpl';
}
}
作为模板文件,我有这样的东西
母版页:
<html>
<body>
<div>This is the menu that has to be on every page!!!!</div>
{include file="$subpage"}
</body>
</html>
你好世界.tpl:
hey there: Hello world!
ciao.tpl:
hey there: ciao!
这样,您可以创建用作页面(asp.net Web窗体)的类和一个用作母版页等效项的类母版页。