• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Embed PHP in css and js files

#1
[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).

#2
[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.

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

#4
[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')

#5
[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.

#6
[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).

#7
[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?

#8
[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?


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.