简体   繁体   English

动态php以包含不同CSS页面的文件

[英]dynamic php to include file for different CSS pages

A few of my CSS pages will negatively interact with each other if they were all listed on every page of my site. 如果我的CSS页面中的每一个都列在我网站的每个页面上,则它们之间会相互影响。 The goal is to remove entire head section into a header include file to clean up the pages. 目的是将整个头部移到标头包含文件中以清理页面。 I've tried to use a php script to look at SERVER's Request URI variable to detect current page and then use that in an if statement to pick the css link tags needed for that page. 我尝试使用php脚本查看SERVER的Request URI变量以检测当前页面,然后在if语句中使用它来选择该页面所需的css链接标签。 Here's what I tried: 这是我尝试过的:

<?
$currentLocation = $_SERVER['REQUEST_URI'];
if ($currentLocation == '/index.php' || $currentLocation == '/' || $currentLocation == '') 
{
echo '<link href="css/signin.css" rel="stylesheet">';
} elseif ($currentLocation == '/signup.php') {
echo '<link href="css/signin.css" rel="stylesheet">';
} elseif ($currentLocation == '/anotherPage.php') {
echo '<link href="css/anotherPageCSS.css" rel="stylesheet">';
}
?>

Is this a decent approach? 这是一个体面的方法吗? Do you see any errors in it? 您是否看到任何错误? Please advise. 请指教。

I've also considered breaking the CSS pages down into one stylesheet, and use id attributes to target instead of having tag selectors. 我还考虑过将CSS页面分成一个样式表,并使用id属性作为目标,而不是使用标签选择器。 What do you guys recommend? 你们推荐什么?

Here is a basic example of a way to utilize page keying with a small class. 这是利用小类的页面键控方法的基本示例。 Whenever you want to setup output for html, you choose which pagekey it should build from (that way, your urls and php filenames can change without worry): 每当您要为html设置输出时,都可以选择它应该从哪个pagekey构建(这样,您的url和php文件名就可以更改而无需担心):

<?php
// include_handler.php
class IncludeHandler {
    private $pagekey = 'basic';
    private $cssfiles = array();
    private $jsfiles = array();

    function __construct($key) {
        $this->pagekey = $key;
        $this->cssfiles = [ // define all css files to pagekeys
                            'home' => array('home.css'),
                            'basic' => array(),// if none needed
                            'accounts' => array('accounts.css'),
                            'checkout' => array('accounts.css','checkout.css'),
                        ];
        $this->jsfiles = [  // define all js files to pagekeys
                            'home' => array('home.js'),
                            'basic' => array(),
                            'accounts' => array('accounts.js'),
                            'checkout' => array('accounts.js','checkout.js'),
                        ];
    }
    public function headlinks() { // call this in your html output <head> area
        $html = '';
        foreach ($this->cssfiles[$this->pagekey] as $cssfile) {
           $html .= '<link type="text/css" rel="stylesheet" href="/css/'. $cssfile .'" />';
        }
        foreach ($this->jsfiles[$this->pagekey] as $jsfile) {
           $html .= '<script type="text/javascript" src="/js/'. $jsfile .'"></script>';
        }
        return $html;
    }
}
?>

Example usage from like a checkout page: 诸如结帐页面的用法示例:

<?php
// checkout.php
require_once(__DIR__.'/include_handler.php');
$htmlincludes = new IncludeHandler('checkout');

// processing code

// html output area
?><html>
<head>
    <link type="text/css" rel="stylesheet" href="/css/global.css" />
    <?PHP echo $htmlincludes->headlinks();?>
</head>
<body></body>
</html><?PHP
?>

That is one way how to do it. 那是一种方法。 I would suggest using some kind of dictionaries for it (associative arrays in PHP) 我建议为此使用某种字典(PHP中的关联数组)

Something like 就像是

<?php
$currentLocation = $_SERVER['REQUEST_URI'];
$styles = [
  "/index.php"   => "first.css",
  "/"            => "first.css",
  "/another.php" => "second.css",
  //...
];

if (in_array($currentLocation, $styles))
{
    $link = "css/" . $styles[$currentLocation];
    echo "<link rel=\"stylesheet\" href=\"$link\"/>
}

?>

Just my opinion :) 只是我的观点 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM