PHP Adding stylesheets to header

泄露秘密 提交于 2021-01-27 11:52:38

问题


Is there a way to add stylesheets to the header after including the header file? Say we have this code:

class content {
    public $stylesheets = array();

    public function addStylesheets($stylesheets)
    {
        if(!empty($stylesheets))
        {
            if(!is_array($stylesheets))
                $stylesheets = array($stylesheets);

            $this->stylesheets = array_merge($this->stylesheets, $stylesheets);
        }
    }

    public function getStylesheets()
    {
        $response = '';

        foreach($this->stylesheets as $stylesheet)
            $response .= '<link rel="stylesheet" type="text/css" href="' . $stylesheet . '" />' . Chr(13);

        return $response;
    }
}

And this header:

<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" type="text/css" href="default.css" />
    <?php
        print($content->getStylesheets());
    ?>
</head>

<bod...

And then later in the file I want to add stylesheets like this:

$content->addStylesheets(array('home.css', 'slider.css'));

Can someone give me an example of how to do this?


回答1:


The final html code is always generated at the end. Here is an example how to proceed with pure php/html. Your index.php could look something like this:

<?php

// ...

// Define the stylesheets to use
$content = new content();
$content->addStylesheets(array('home.css', 'slider.css'));

// ...

// Print template at the end
ob_start();
include 'your_html_template.php';
print ob_get_clean();

The your_html_template.php is your template file as shown in the question.


You can define variables before executing the template to have a more clear separation of php code and html. Then the index.php could look like this:

<?php

// ...

// Define the stylesheets to use
$content = new content();
$content->addStylesheets(array('home.css', 'slider.css'));
$htmlStyleHeaderTags = $content->getStylesheets();  // Define variable with html header tags

// ...

// Print template at the end
ob_start();
include 'your_html_template.php';
print ob_get_clean();

And then you can use the variable $htmlStyleHeaderTags in your template:

<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" type="text/css" href="default.css" />
    <?php print $htmlStyleHeaderTags; ?>
</head>

<bod...



回答2:


As @szapio already sad, it makes more sense to build the HTML at the very end.

Anyway, a "dirty" solution would be to hack your DOM with jQuery by putting the following before the </body>-tag (having jQuery included in the <head>):

<script>
$(document).ready(function() {
    $('head').append('<link rel="stylesheet" href="test.css" />');
});
</script>

You really shouldn't consider doing this, because the CSS file will be loaded after everything else, meaning you will see the unstyled elements as long as the CSS file is not loaded yet…



来源:https://stackoverflow.com/questions/28500848/php-adding-stylesheets-to-header

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!