0

我已经为 php 网站构建了我的简单模板。问题是我不知道包含我的 css 和 javascript 文件的好方法。

这是我的index.php文件

<?php
include'core/template.php';
$temp=new Template();
$settings=array("title","page_title","welcome_word");
$values=array("RIS - Home","Results Information System","Welcome to result 
               Information      system");
$temp->header($settings,$values);
$temp->footer();


?>

这是我的template.php文件

<?php
class Template {
public function header($setting,$values){
    $file=file_get_contents("http://localhost/RIS/src/header.php");
    $count=0;
    foreach ($setting as $setting) {
        $file=str_replace('{'.$setting.'}',$values[$count],$file);
        $count++;
    }
    echo $file;

}

public function footer(){
    $file=file_get_contents("http://localhost/RIS/src/footer.php");
    echo $file;
}
}




?>

这是我的header.php文件

<!DOCTYPE html>
<html>
<head>
    <title>{title}</title>
    </head>
<body>
    <header>
        <h1>{page_title}</h1>
            {welcome_word}
    </header>
    My contents goes here...

这是我的footer.php文件

<footer>
Copyright &copy; RIS 2013.
</footer>
</body>
</html>

我有我的 css 文件和 js 文件在assets 文件夹中 ,那么我如何将它们包含在我的模板中?

4

3 回答 3

1

对于 CSS 文件:

<!DOCTYPE html>
<html>
<head>
    <title>{title}</title>
    <link rel="stylesheet" href="path/to/assets/yourcss.css" type="text/css">
</head>
<body>
    <header>
        <h1>{page_title}</h1>
            {welcome_word}
    </header>
    My contents goes here...

对于 javascripts:

<footer>
Copyright &copy; RIS 2013.
</footer>
<script src="path/to/assets/yourjs.js"></script>
</body>
</html>
于 2013-11-11T09:26:46.290 回答
1

我会使用输出缓冲区和包含而不是 file_get_contents,include'core/template.php';这里缺少一个空格。

在您的情况下,为什么不将 CSS 和 JS 放入header.php

或者,如果您想要非阻塞,请将 CSS 放在 header.php 中,将 JS 放在 footer.php 中

于 2013-11-11T09:30:34.243 回答
0

为什么你使用“file_get_contents”,你可以使用“include_once”来包含你的页脚文件。

对于您的模板,为 css 放置一个标记,并在 php 中构建您的 css 数组,然后将其注入您的标记。

希望能帮助到你。

于 2013-11-11T09:30:50.097 回答