Page 1 of 1

template_path parsing

Posted: Sun Oct 04, 2009 2:30 pm
by Timbalu
Hi

Is there any solution to get smarty parsing plugin css files in your template folder using

Code: Select all

background: url('{TEMPLATE_PATH}img/foo.png')
which works in the admin panel but does not work for a plugin css file inside your template folder

Code: Select all

$tfile = serendipity_getTemplateFile('style_myplugin_backend.css', 'serendipityPath');

if (!$tfile || $tfile == 'style_myplugin_backend.css') {
     $tfile = dirname(__FILE__) . '/style_myplugin_backend.css';
}

echo file_get_contents($tfile);
If you put it in template user.css you can just do

Code: Select all

background: url('img/foo.png')
Is this behavior by design or did I understand something wrong?

Ian

Re: template_path parsing

Posted: Sun Oct 04, 2009 2:33 pm
by garvinhicking
Hi!

You only need TEMPLATE_PATH in the style.css, because it is loaded through serendipity.css.php and its relative URL is inside the main root directory. Unlike all other CSS files which are loaded through the real path they live in, so you can reference any paths relatively to that CSS file (img/...). So if you like to load a template dependant file, you would need to put the CSS file inside the template directory.

The CSS files are loaded directly, so there is no way for PHP to kick in. If you need any replacement, you would need to use an event plugin with "external_plugin" hook to create a custom URL like index.pph?/plugin/mycss which uses PHP to output/read any CSS files.

Regards,
Garvin

Re: template_path parsing

Posted: Sun Oct 04, 2009 3:08 pm
by Timbalu
Hi Garvin

I don't really understand...
I use a event plugin hook inside the event plugin I am working on with case 'css'.
The file style_myplugin_backend.css is located in my plugin folder and in the bulletproof template folder where it is loaded correctly and I can't use url('img/foo.png') nor url('{TEMPLATE_PATH}img/foo.png').

If I add the external plugin hook

Code: Select all

case 'external_plugin':
       switch($eventData) {
             ???
       }
       return true;
       break;
what do I need to put in there to let smarty parse the template/style_myplugin_backend.css?

Ian

Re: template_path parsing

Posted: Sun Oct 04, 2009 5:05 pm
by garvinhicking
Hi!

In your 'css' event hook you would need to replace the variables yourself; have a look at how serendipty.css.php parses the path.

There's no smarty parsing, it's only a str_replace() on two fixed variables.

HTH,
Garvin

Re: template_path parsing

Posted: Sun Oct 04, 2009 6:33 pm
by Timbalu
Hi Garvin

Thats it! No use for "external_plugin". Thanks for the help!
Ian

Code: Select all

case 'css':
       ...
       $tfile = serendipity_getTemplateFile('style_myplugin_backend.css', 'serendipityPath');
       ...
       echo str_replace('{TEMPLATE_PATH}', 'templates/' . $serendipity['defaultTemplate'] . '/', @file_get_contents($tfile));
       ....