繁体   English   中英

如何将css和js文件包含到php模板中?

[英]how to include css and js files to php template?

我为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文件在资产文件夹中,所以我如何将它们包含在我的模板中?

对于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>

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

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

或者如果你想要非阻塞,可以在footer.php中将header放在header.php和JS中

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

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

希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM