【发布时间】:2015-04-14 13:38:53
【问题描述】:
有没有办法在包含头文件后将样式表添加到头文件中?假设我们有这个代码:
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 分开,你可以使用 smarty 或 twig 之类的模板引擎。但是如果你不想使用它,那么最后生成你的 html,首先执行你的 php 代码,然后渲染 html。所以首先
if (something) { $content->addStyleSheet($style)} ... (some other code) ... echo $myTemplate;其中 $myTemplate 包含您的网站
标签: php html class stylesheet magic-methods