Page 1 of 1

Sidebar plugins emit css via event hook?

Posted: Fri Jan 08, 2010 10:05 pm
by Don Chambers
I've seen a number of event plugins that emit css via the event hook css. Is something like this possible in sidebar plugins?

Re: Sidebar plugins emit css via event hook?

Posted: Fri Jan 08, 2010 10:41 pm
by garvinhicking
Hi!

Only event plugins can hook into events. So you need to create a bundled event plugin for that, or emit inline CSS, or use javascript to inject CSS into the DOM.

Regards,
Garvin

Re: Sidebar plugins emit css via event hook?

Posted: Fri Jan 08, 2010 10:46 pm
by Don Chambers
I thought it would be a good idea NOT to use inline css, but do you think a bundled event plugin exclusively to emit css is ridiculous? If not, wow exactly do I "bundle" it with the sidebar plugin?

Re: Sidebar plugins emit css via event hook?

Posted: Sat Jan 09, 2010 12:04 am
by garvinhicking
Hi!

If you're simply wanting to "get it done", I'd go for the inline CSS, since all browsers parse that. You could make the plugin read a serendipity_getTemplateFile('bla.css') file that users could customize. Creating a bundled event plugi would be an overhead that for me would not be the most beneficial solution. Going that route would be somewhat along the lines of the serendipity_event/plugin_login plugin. Just create a plugin and in the introspect() method use $this->dependencies...

HTH,
Garvin

Re: Sidebar plugins emit css via event hook?

Posted: Sat Jan 09, 2010 3:31 pm
by Don Chambers
Thanks Garvin. I think serendipity_getTemplateFile('bla.css') makes the most sense... but that assumes the file will be in the template folder. Is there a plugin folder equivalent, or do I need to hard code the path?

Re: Sidebar plugins emit css via event hook?

Posted: Sat Jan 09, 2010 3:49 pm
by garvinhicking
Hi!

Plugins use this as a code to prefer template files, but fallback to the plugin directory:

Code: Select all

        $filename = 'blah.css'; // Set filename
        $tfile = serendipity_getTemplateFile($filename, 'serendipityPath'); // Try to get file path from template directory
        if (!$tfile || $tfile == $filename) { // If not found, use current directory
            $tfile = dirname(__FILE__) . '/' . $filename;
        }
        echo file_get_contents($tfile);
Regards,
Garvin

Re: Sidebar plugins emit css via event hook?

Posted: Sat Jan 09, 2010 6:17 pm
by Don Chambers
Thanks Garvin. But if it just echo's the file's contents, isn't that just going to appear within the sidebar plugin, and not included in serendipity.css? Can you think of a sidebar plugin that does this so I can see the code as an example?

Re: Sidebar plugins emit css via event hook?

Posted: Sat Jan 09, 2010 9:45 pm
by garvinhicking
Hi!

Yes, that's what I call inline CSS! Just echo '<style type="text/css">...</style>';...

As I mentioned, sidebar plugins are not event plugins and thus cannot hook into events.

Regards,
Garvin

Re: Sidebar plugins emit css via event hook?

Posted: Sat Jan 09, 2010 10:01 pm
by Don Chambers
I thought there would still be some way to load it into serendipity.css. I had already done what you suggested:

Code: Select all

        // load plugin stylesheet
        $filename = 'serendipity_plugin_custom.css'; // Set filename
        $tfile = serendipity_getTemplateFile($filename, 'serendipityPath'); // Try to get file path from template directory
        if (!$tfile || $tfile == $filename) { // If not found, use current directory
            $tfile = dirname(__FILE__) . '/' . $filename;
        }
        echo '<style type="text/css">', file_get_contents($tfile), '</style>';
I'm not sure if I gained anything over simply going inline or embedded directly in the plugin as:

Code: Select all

print <<<EOT
<style type="text/css">
/* all my style stuff here */
</style>
EOT;
Any of these methods will still require !important to override via a template or user stylesheet.