0

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?

0

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

thenx for advise, i think this will help
1

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>

1 Comment

Additional Information Css files are called between head tags. But the reason of calling js files at the end of body is to ensure all DOM elements are instatiated.
0

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.

1 Comment

include_once('path/to/footer.php'); For the loops, build an array for exemple with all your css files, then loop on it and build the css includes (<link href=""....>), then inject it like the rest in a marker like {css_head}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.