internally1
internally1

Reputation: 97

how to include css and js files to php template?

i have built my simple template for php site. the problem is that i don knw the nice way to include my css and javascript files .

here is my index.php file

<?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

<?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

<!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>
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?

Upvotes: 0

Views: 4758

Answers (3)

David Ansermot
David Ansermot

Reputation: 6112

Why do you use "file_get_contents", you can use "include_once" to include your footer file.

For your template, put a marker for the css, and build your css array in php then inject it to your marker.

Hope it helps.

Upvotes: 0

Daniel W.
Daniel W.

Reputation: 32290

I would use output buffers & include instead of 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 ?

Or if you want non-blocking, put CSS in header.php and JS in footer.php

Upvotes: 1

TiMESPLiNTER
TiMESPLiNTER

Reputation: 5889

For CSS files:

<!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:

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

Upvotes: 1

Related Questions