CodeIgniter Forums
Embed PHP in css and js files - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Embed PHP in css and js files (/showthread.php?tid=51166)



Embed PHP in css and js files - El Forum - 04-23-2012

[eluser]coldscooter[/eluser]
Is there and easy way to set this up? It is something that is required a lot (like loading assets such as images in css files).


Embed PHP in css and js files - El Forum - 04-23-2012

[eluser]CroNiX[/eluser]
Never needed to do this. I set all dynamic js vars in the head of my document (which is a php template, so I can use php there), like base_url, so that my separate, pure js files can use them. Same with dynamic css rules.

You can also set your server up to parse js and css files as php and use appropriate headers when sending the data out, although that makes your server have to process more things. There are many guides on the net how to set that up.


Embed PHP in css and js files - El Forum - 04-23-2012

[eluser]coldscooter[/eluser]
So you hardcode the image paths into your css files? Or you declare all image paths in the php view?


Embed PHP in css and js files - El Forum - 04-23-2012

[eluser]CroNiX[/eluser]
For images in css files I just use:
url('/images/image_name.jpg')

which is the same as url('http://www.mysite.com/images/image_name.jpg').

That also lets me use the same image on SSL pages without having to do:
url('https://www.mysite.com/images/image_name.jpg')


Embed PHP in css and js files - El Forum - 04-23-2012

[eluser]Samus[/eluser]
If you want just save the js or css file with a .php extension, do all your processing there and call it how you would js/css.

the extension doesn't matter as everything will be outputted in plain text.

up to you really.


Embed PHP in css and js files - El Forum - 04-23-2012

[eluser]CroNiX[/eluser]
You'd want to set the appropriate headers if you do that. There are browsers that don't process css or js with plaintext headers appropriately. You would also need to take additional steps to get those js/css files that are processed with php to cache in the users browser so it's not sent on every request (mainly last-modified, cache-control and expires IIRC).


Embed PHP in css and js files - El Forum - 04-23-2012

[eluser]Samus[/eluser]
[quote author="CroNiX" date="1335201415"]You'd want to set the appropriate headers if you do that. There are browsers that don't process css or js with plaintext headers appropriately. You would also need to take additional steps to get those js/css files that are processed with php to cache in the users browser so it's not sent on every request (mainly last-modified, cache-control and expires IIRC).[/quote]
Good points.

But if it's dynamic wouldn't it be best not to cache that?


Embed PHP in css and js files - El Forum - 04-23-2012

[eluser]CroNiX[/eluser]
Are you meaning your css/js would actually constantly change output? Then, no, you wouldn't want to. But most of the time it wouldn't change, would it?