[英]How to add css files using PHP inside the <head> tag?
Ok, let me explain: 好,让我解释一下:
I have a some files, something basic like this: 我有一些文件,像这样的基本内容:
index.php 的index.php
<html>
<head>
<title>Simple page</title>
</head>
<body>
<?php include 'home.php'; ?>
</body>
</html>
home.php home.php
<div class="thisWillBeBlue">Still not blue</div>
style.css style.css文件
.thisWillBeBlue {background: blue}
Now the question: Using php I want to insert the style.css inside the head tag, calling it from the file home.php . 现在的问题是:使用php我想将style.css插入head标记内,从文件home.php调用它。 Well, I came out with a solution, but it was not very effective:
好吧,我提出了一个解决方案,但是它不是很有效:
index.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>
But the problem it is, If I call the css from home.php it will be added to the array later, therefore it will not be echoed inside the head tag. 但是问题是,如果我从home.php调用css,它将在以后添加到数组中,因此不会在head标签内回显。 Any ideas?
有任何想法吗?
You could do it using ob_start()
and ob_end_flush()
functions eg 您可以使用
ob_start()
和ob_end_flush()
函数来实现,例如
index.php 的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 home.php
<?php $csspage = "custom_style.css"; ?>
<div class="thisWillBeBlue">blue</div>
Further reference: http://it1.php.net/ob_start 进一步参考: http : //it1.php.net/ob_start
I think you are looking for something like this ..(include a piece of code in their header files, so that it will allow you to add more stylesheets ) 我认为您正在寻找这样的东西..(在其头文件中包含一段代码,以便您可以添加更多样式表)
This will allow you to add more stylesheets to it on each page. 这将允许您在每个页面上添加更多样式表。 (add this to
<head>
) (将此添加到
<head>
)
<?php
if (!empty($styles) && is_array($styles)) {
foreach ($styles AS $style) {
echo '<link rel="stylesheet" href="/assets/css/'. $style .'">';
}
}
?>
You can put a variable at the top of an individual script if you need a specific stylesheet: 如果需要特定的样式表,可以将变量放在单个脚本的顶部:
<?php
$styles = array('custom_style.css');
?>
CSS file references can be placed in the body of your code, if needed. 如果需要,可以将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>
The only difference is that when in the HEAD, they are guaranteed to be loaded before the page is rendered. 唯一的区别是,在HEAD中时,保证在呈现页面之前已加载它们。 When they are in the BODY, there may be a split-second where they are still loading and the styles haven't been applied yet.
当它们在BODY中时,可能会在瞬间加载它们,而样式尚未应用。
If you definitely want them in the HEAD, you could define the css requirements in a separate folder with the same file name, like so: 如果您确实希望它们在HEAD中,则可以在具有相同文件名的单独文件夹中定义css要求,如下所示:
index.php: index.php文件:
<html>
<head>
<?php
include('css-requirements/home.php');
?>
</head>
<body>
<?php include('home.php'); ?>
</body>
</html>
and 和
css-requirements/home.php: CSS-要求/ home.php:
<link href="mycss.css" rel="stylesheet" type="text/css" />
<link href="myothercss.css" rel="stylesheet" type="text/css" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.