简体   繁体   中英

PHP template and external CSS

So I have this PHP website that I am working on that using templates. Any page is divided into three parts: header, content and footer. Each part is represented by a .php file.

Header:

<html>
<head><link_to_some_css /></head>
<body>
<div id="menuBar">
    xxx
</div>

Content:

<?php include("header.php") ?>
    <div id="someHTMLContent">xxx</div>
    <!-- 
        How do I link an external CSS stylesheet from within here?
    -->
<?php include("footer.php") ?>

Footer:

</body></html>

The <link> tag should resides within the <head> , but being within content.php I have no access to the head of the html.

Sorry if this has been asked, but I am a little new to implementing a framework-less PHP project in an MVC architecture (hence trying to separate CSS JavaScript PHP files for a better structure)

The simple answer is - no way.

The way it should be done is, that there is an object, which represents all stylesheets, which is initialized upon page creation, then you add your stylesheet, and only then display the page. It should look something like this:

Controller:

$this->view->script="viewSomePage.php";
$this->view->stylesheets->add('AddSomeStylesheet.css');
$this->view->render();

On the inside, the render() function would then take the stylesheets, and pass them on to a view.

View:

foreach($this->stylesheets as $stylesheet){
//do some outputing;
}

Doing this however requires, that each view is an instance of some sort of a view class. Or has some sort of an array passed down to it in an array. The choice is your's.

This comes from the MVC idea itself, that a view should do nothing more, than display the content that is given to it. Therefore, it should not think, which stylesheet to load, or which template/layout to use. If a particular template/layout already has some stylesheets - fine, but additional ones should be defined in a controller and then passed on to the view.

Hope this helps.

Edit As I sat thinking, I though of a way you might achieve this. And with a might I mean, that I've not tested it, and I do not know if it would work as expected.

You could try creating a simple javascript, that upon page load would take all <link> tags and put them in the header.

This is however, a very hackish way to do this, and should not be used unless you are desperate.

If you want something included in the header then make it a global variable before including.

$header = 'Add something to the head tag';
include('header.php');

In the header.php:

<head>
<?php
if (isset($header)) {
    echo $header;
}
?>
<!-- Add global headers -->
</head>

You could add a more robust solution by defining functions to create CSS tags given a link and then add them into the global $header string. These would need to be included from a core.php file included before the header include.

Create a css_main.php file which store the CSS links.

Examples are...

<link rel="stylesheet" href="(THE PATH OF CSS FILE)" type="text/css" />

after that, just need to require "css_main.php" on your content .php file

You either have the link in the header or inside the content. If you have it in the header it'll load whenever you include the header. Otherwise just put the link inside the content.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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