I have a conf.inc.php
file which has the following few lines.
// enable css using convention below:
// $css["pageName"] = ["cssName1", "cssName2", ...]
$css["all"] = ["style"];
$css["myPage"] = ["someStyle"];
$css["myOtherPage"] = ["otherStyle", "anotherStyle"];
// disable css
$exCss["myOtherPage"] = ["style"];
What this will do is give all of my pages the style.css
. Then all the other keys of the index will give their respective page (e.g. "myPage.php" etc.) the styles listed in the array after the equals (e.g. "someStyle.css").
All of the styles are stored in a folder called "css" and there is no other place to store them as I have hard coded it in - this meant I didn't have to write relative paths in the multidimensional array.
The second array will exclude some styles from their respective page. In this instance the style called style.css
is being excluded from the page named myOtherPage.php
.
Note: The page names are from a function here. So they only read the file name and ignore extensions etc. This means that "myOtherPage" could also be "myOtherPage.php" or "myOtherPage.html". Although if you look at the following code they will have to be PHP see I use an include
in my header of the PHP files.
// print css that are present in all pages
$a = (isset($css["all"])) ? count($css["all"]) : 0;
for ($i = 0; $i < $a; $i++)
if ((isset($exCss["all"]) && !in_array($css["all"][$i], $exCss["all"])) || (isset($exCss["$page"]) && !in_array($css["all"][$i], $exCss["$page"])) || (!isset($exCss["all"]) || !isset($exCss["$page"])))
echo (file_exists("{$css["all"][$i]}.css")) ? "<link rel=\"stylesheet\" type=\"text/css\" href=\"{$css["all"][$i]}.css\" />" : "";
This code is part of header.inc.php
and this gets placed inside the <head>
tags. This will add all the styles from the array providing that style is not listed in the $exCss
array.
It is common for me to make things more difficult than they need to be...
This could be another case of that. I also tend to make functions or processes that already exist.
Anyway, I thought I would share this little snippet. If anyone has anyway of improving it then I would love to hear it. Thanks as usual!