| Author |
Message |
Guardian
Site Admin


Joined: Jan 06, 2006
Posts: 4465
Location: Vsetin, Czech Republic
|
Posted:
Mon Mar 26, 2012 9:51 am |
 
|
I needed this for a module I was working on for someone because he wanted to style the modules block file.
It isn't a perfect solution because I would personally prefer to be looking in the module itself for the block stylesheet to ensure that when a module was deleted, so was the stylesheet but it was the best I could come up with in the few seconds I had.
Code:
// filename: includes/addons/head-blockCSS.php
// in case the mainfile function isn't loaded
if(!function_exists('readDIRtoArray')) {
function readDIRtoArray($dir, $filter) {
$files = array();
$handle = opendir($dir);
while (false !== ($file = readdir($handle))) {
if (preg_match($filter, $file)) {
$files[] = $file;
}
}
closedir($handle);
return $files;
}
}
$blockCSSfiles = readDIRtoArray('blocks/css', '/^style\-(.+)\.css/');
foreach ($blockCSSfiles as $blockStyle) {
addCSSToHead('blocks/css/'.$blockStyle.'', 'file');
}
|
This assumes your stylesheet is in webroot/blocks/css/
and your naming convention must be
style-XXXX.css
In my case I'm actually appending the block file name so I know which block it relates to like this;
webroot/blocks/css/style-block-PlayerOfTheMonth.css |
_________________ TPD |
|
|
 |
Guardian
Site Admin


Joined: Jan 06, 2006
Posts: 4465
Location: Vsetin, Czech Republic
|
Posted:
Tue Mar 27, 2012 12:30 am |
 
|
I had some other tweaks I had to do to get the code in the previous post to work correctly because htaccess directiives were blocking the addCSSToHead function from loading the CSS files correctly.
I wasn't entirely happy with the other tweaks I had to do so I actually change my methodology to using a file;includes/addons/head-BlockCSS.php
and just putting a straightforward call to the file there
Code:addCSSToHead('path/to/modules/MY_MODULE/style.css','file');
|
Because the function creates an array, it doesn't matter if you try to load the same CSS file at the module level (it gets ignored). |
_________________ TPD |
|
|
 |
montego
Site Admin


Joined: Jan 06, 2006
Posts: 308
|
Posted:
Wed Mar 28, 2012 5:09 pm |
 
|
|
|
 |
Guardian
Site Admin


Joined: Jan 06, 2006
Posts: 4465
Location: Vsetin, Czech Republic
|
Posted:
Tue Jun 26, 2012 6:11 am |
 
|
Not sure I would agree with that, because if styling is required at the block level, the chances are fairly high that the data in the block belongs to a module. This is not always the case but as a percentage, I think it would be pretty high.
Since one would normally aim for modularity, I think there is even a valid argument for module dependent block files being moved to within the module structure so you could potentially (ideally?) end up with something like;
modules/module_name/
modules/module_name/module_name-config.xml
modules/module_name/styles
modules/module_name/blocks
Example config file so you can see why it appears in this example even though it isn't related to the OP.
xml Code:<?xml version="1.0" ?>
<module>
<name>CA Donations</name>
<date>26/06/2011</date>
<author>Guardian</author>
<email>example@example.com</email>
<url>http://www.code-authors.com</url>
<copyright>GNU/GPL License</copyright>
<version>1.0</version>
<description>Donations Module</description>
<install>
<query>CREATE TABLE....</query>
<query>INSERT INTO...</query>
</install>
<uninstall>
<query>DROP TABLE....</query>
</uninstall>
</module>
|
|
_________________ TPD |
|
|
 |
montego
Site Admin


Joined: Jan 06, 2006
Posts: 308
|
Posted:
Sun Jul 01, 2012 1:24 pm |
 
|
|
|
 |
|
|