简体   繁体   English

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

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

i have built my simple template for php site. 我为php网站构建了我的简单模板。 the problem is that i don knw the nice way to include my css and javascript files . 问题是我不知道如何包含我的CSS和JavaScript文件。

here is my index.php file 这是我的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();


?>

here is my template.php file 这是我的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;
}
}




?>

here is my header.php file 这是我的header.php文件

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

here is my footer.php file 这是我的footer.php文件

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

i have my css files and js files are in assets folder so how do i include them in my template? 我有我的CSS文件和js文件在资产文件夹中,所以我如何将它们包含在我的模板中?

For CSS files: 对于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...

and for javascripts: 对于javascripts:

<footer>
Copyright &copy; RIS 2013.
</footer>
<script src="path/to/assets/yourjs.js"></script>
</body>
</html>

I would use output buffers & include instead of file_get_contents, include'core/template.php'; 我会使用输出缓冲区和include而不是file_get_contents, include'core/template.php'; here is a blankspace missing. 这里缺少空白。

In your case, why not just put the CSS and JS into header.php ? 在你的情况下,为什么不把CSS和JS放入header.php

Or if you want non-blocking, put CSS in header.php and JS in footer.php 或者如果你想要非阻塞,可以在footer.php中将header放在header.php和JS中

Why do you use "file_get_contents", you can use "include_once" to include your footer file. 为什么使用“file_get_contents”,可以使用“include_once”来包含页脚文件。

For your template, put a marker for the css, and build your css array in php then inject it to your marker. 对于你的模板,为css放置一个标记,然后在php中构建你的css数组,然后将它注入你的标记。

Hope it helps. 希望能帮助到你。

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

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