如何将css模板包含在网站上的所有页面上

时间:2014-01-15 02:14:16

标签: php html css templates

我创建了一个包含标题,导航和页脚的template.html,我希望将其包含在我的所有其他页面中:网站上的html / php文件,而不必将所有代码复制到所有其他文件。 是否有捷径可寻?或者有更合适的方法来处理这个问题吗? 我已经尝试过要求使用php的页面,但它没有采用任何样式。

更新:我最终切换到使用include功能。 我需要在我的HTML中使用php,如果你回复出PHP脚本,代码就会被评论。 我发现最好的是使用包括head,header(和nav)和页脚。 但是为了避免多次打开和关闭html标签,我将其格式化为:

head: <html><head>
-content for head
header: </head><body><header></header>
-content between header and footer
footer: <footer></footer></body></html>

2 个答案:

答案 0 :(得分:2)

嗯,你把你的:

标题在一个文件中,例如header.php
footer在一个文件中,如footer.php
等等。

然后在你的其他.php文件中,你只需调用include函数给出你的页眉/页脚路径。

答案 1 :(得分:1)

我将组织不同的脚本,以便从index.php中调用它们(如果尚未完成)。然后,在构建页面的逻辑之前,我将包括标题和导航,以及之后的页脚。

这是一个模板:

<!DOCTYPE html>
<html>
<head><!-- here your header --></head>
<body>
~
<!-- here your navigation? -->
~
</body>
</html>

我把'〜'放在能够爆炸的地方:

$template = explode('~',file_get_contents('template.html'))
// that way we have our code in an array we can easily use for header, navig and footer
$html = $template[0]; // header
$html.= $template[1]; // nav

switch($action) {
case 'foo':
  $html.= file_get_contents('foo.html');// just add directly html content
  break;                                // careful that it does not have <body> tags
case 'bar':
  require_once('bar.php');
  $html.= showBar(); // if you have a function in your script, you can add it to your page that way
  break;
}

$html.= $template[2]; // footer

如果您的其他脚本直接回显内容,请使用$html.=

更改echo
相关问题