有没有办法在包含头文件后将样式表添加到标题中?假设我们有这样的代码:
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;
}
}
还有这个标题:
<html>
<head>
<title>Title</title>
<link rel="stylesheet" type="text/css" href="default.css" />
<?php
print($content->getStylesheets());
?>
</head>
<bod...
然后在文件中我想添加如下样式表:
$content->addStylesheets(array('home.css', 'slider.css'));
有人可以给我一个如何做到这一点的例子吗?
最终的 html 代码总是在最后生成。这是一个如何继续使用纯 php/html 的示例。你的index.php可能看起来像这样:
<?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
是您的模板文件,如问题所示。
You can 定义变量在执行模板之前有一个更清晰的php代码和html分离。那么index.php 可能如下所示:
<?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();
然后你可以使用变量$htmlStyleHeaderTags
在你的模板中:
<html>
<head>
<title>Title</title>
<link rel="stylesheet" type="text/css" href="default.css" />
<?php print $htmlStyleHeaderTags; ?>
</head>
<bod...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)