How do I add php support in css file

As I understand it I can make my own injected languages right? So how do i make it that I can have full php support in a css file?
And to prevent anyone from pointing out that css files shouldn't be parsed because of speed and load issues; I only use it during development to automate calculating vertical rhytms and other spacings.

5 comments
Comment actions Permalink

Hi Peter,

I cannot guarantee that this will work (theoretically it should), but you can try it:
1) Remove *.css pattern from "Cascading style sheets" and assign it to "PHP files" in File | Settings | File Types.
2) File | Settings | Template Data Languages -- assign "CSS" to such files (or to the folder where those files are located).

#1 will turn on PHP support for *.css files & #2 should treat those files as CSS instead of the default HTML.

Please note that #1 is IDE-wide setting and will affect all projects.

If you do not like #1 you can go a bit more complex way -- create custom extension (e.g. *.cssphp or *.phpcss etc) and work with such files, but then you may have to configure your webserver (to send proper Content-Type header, unless you do it via PHP) + change the links in your HTML files.

0
Comment actions Permalink

Thanks, it works. Not the most elegant solution, but I can live with it.

I was hoping it could be done by adding a new entry in "language injection". I'll file a request for that in youtrack.

Thanks again.




WI-5662 Add php language injection in css files.

0
Comment actions Permalink

That's an only proper way to do it if these files are valid PHP files.

0
Comment actions Permalink

Here is an improved version (since it is the only proper way):

1) Use custom extension for such css files (e.g *.pcss -- similar idea behind to *.phtml extension) and assign it to "PHP files" in File | Settings | File Types.
Since such settings are IDE-wide, in this way you can still use "normal" *.css files as usual.

2) File | Settings | Template Data Languages -- assign "CSS" to such files (or to the folder where those files are located).
Must have step -- Easy to do and usually you need to do it only once especially if you keep all such files within one folder.

3) In your templates/html files refer to those PHP-powered files as if they would have *.css extension (so no changes in the code here).

4) Create custom URL-rewrite rule to redirect non-existing *.css file to the same name but with *.pcss extension (or whatever extension you choose). Every popular webserver has extensions/modules that allow you to do this (Apache, IIS, nginx, lighttpd for sure).
By creating rewrite rule to check if such *.css file exists you can use both *.css & *.pcss files at the same time with no conflicts (*.css files will be served directly by webserver while *.pcss will go through PHP interpreter).

0
Comment actions Permalink

The problems I have with this solution are:

1) It requires me to set the template data languages for every project.
2) ccs files (.pcss) would get a php-icon and not a css icon
3) Some older browser do not accept style-files with an extension other than css, no matter what the header says.

It's ok for a one time thing, but I wouldn't want to make this part of my regular workflow. I'll see if LESS can solve the same problems for me so I can use that instead of php.

Thanks anyway. :-)

0

Please sign in to leave a comment.