【问题标题】:How to add css files using PHP inside the <head> tag?如何在 <head> 标签内使用 PHP 添加 css 文件?
【发布时间】:2014-02-12 17:17:44
【问题描述】:

好吧,让我解释一下:

我有一些文件,基本如下:

index.php

<html>
      <head>

            <title>Simple page</title>

      </head>

      <body>

            <?php include 'home.php'; ?>

      </body>
</html>

home.php

<div class="thisWillBeBlue">Still not blue</div>

style.css

.thisWillBeBlue {background: blue}

现在的问题是:使用 php 我想在 head 标签内插入 style.css,从文件 home.php 中调用它。好吧,我想出了一个解决方案,但它不是很有效:

index.php

<?php $css = array(); 
      $css[] = 'linktothecss.css'

?>

<html>
      <head>

            <title>Simple page</title>

            <?php

                foreach($css as $item){

                    echo "<link rel='stylesheet' href='".$item."' />";

                }

            ?>

      </head>

      <body>

            <?php include 'home.php'; ?>

      </body>
</html>

但问题是,如果我从 home.php 调用 css,它将稍后添加到数组中,因此它不会在 head 标签内回显。有什么想法吗?

【问题讨论】:

  • 在&lt;link&gt; 标签内,是href 而不是src
  • 我不是 php 专家,但我相信解决方案将涉及您的 php 文件之间的某种协议,以便您的 index.php 一方面收集所需的 css 文件,另一方面,html 进入您的 body 标签,在这种情况下来自其他 php 文件,如 home.php。
  • 你可以使用一些模板系统,比如twig:twig.sensiolabs.org/doc/tags/block.html和twig.sensiolabs.org/doc/tags/extends.html
  • 谢谢@LorenzoMarcon,我没看到:/
  • 是的,我可以@FabrizioCalderan,但我真的很想知道一个不使用它的解决方案。

标签: php html css


【解决方案1】:

您可以使用 ob_start() 和 ob_end_flush() 函数来实现 例如

index.php

<?php

$csspage = "default.css";
function loadCSS($buffer) {
  global $csspage;  
  return (str_replace('{{ css }}', $csspage, $buffer));
}

ob_start("loadCSS"); ?>

<html>
   <head>
      <!-- the string {{ css }} is just a placeholder that will be replaced 
           with the new value of $csspage defined later in the code, otherwise
           it will replaced with its initial value (default.css)
      -->
      <link href="{{ css }}" /> 
   </head>

<body>
     <?php include 'home.php'; ?>
</body>

</html>

<?php ob_end_flush(); ?>

home.php

 <?php $csspage = "custom_style.css";  ?> 
 <div class="thisWillBeBlue">blue</div>

进一步参考:http://it1.php.net/ob_start

【讨论】:

    【解决方案2】:

    我认为您正在寻找类似的东西..(在他们的头文件中包含一段代码,以便您添加更多样式表)

    这将允许您在每个页面上添加更多样式表。 (将此添加到&lt;head&gt;)

    <?php
    if (!empty($styles) && is_array($styles)) {
        foreach ($styles AS $style) {
            echo '<link rel="stylesheet" href="/assets/css/'. $style .'">';
        }
    }
    ?>
    

    如果您需要特定的样式表,您可以将变量放在单个脚本的顶部:

    <?php
    $styles = array('custom_style.css');
    ?>
    

    【讨论】:

      【解决方案3】:

      如果需要,可以将 CSS 文件引用放在代码主体中。

      <body>
        <link href="linktothecss.css" rel="stylesheet" type="text/css" />
        <div class="thisWillBeBlue">
          I'll be blue as soon as linktothecss.css finishes loading!
        </div>
      </body>
      

      唯一的区别是,当在 HEAD 中时,它们保证在页面呈现之前被加载。当它们在 BODY 中时,可能会有一瞬间它们仍在加载并且尚未应用样式。

      如果您确实希望它们在 HEAD 中,您可以在具有相同文件名的单独文件夹中定义 css 要求,如下所示:

      index.php:

      <html>
        <head>
          <?php
          include('css-requirements/home.php');
          ?>
        </head>
      
        <body>
          <?php include('home.php'); ?>
        </body>
      </html>
      

      和

      css-requirements/home.php:

      <link href="mycss.css" rel="stylesheet" type="text/css" />
      <link href="myothercss.css" rel="stylesheet" type="text/css" />
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-27
        • 1970-01-01
        • 1970-01-01
        • 2019-11-25
        • 1970-01-01
        • 2011-03-14
        相关资源
        最近更新 更多