On Oct 7, 2009, at 11:49 AM, mdipierro wrote: > > Yes, this should be done in main using using gluon/contenttype after > creating the Response object. I would take a patch for this.
Here's my cut at a patch to return proper headers for css files. This does not address Eric's desire for a single controller solution for CSS files, which is a different issue, but it does eliminate the need for the __cssheaders() function. http://lobitos.net/web2py-patches/gluon/contenttype.py http://lobitos.net/web2py-patches/gluon/main.py > > Massimo > > On Oct 7, 10:56 am, Jonathan Lundell <jlund...@pobox.com> wrote: >> On Oct 7, 2009, at 8:44 AM, Julio wrote: >> >> >> >>> This is good, specially if on your website you implement some sort >>> of >>> "skinning" allowing the users to dynamically switch "layouts" using >>> the same css file, thanks for sharing this. >> >> You're welcome. >> >> I think it's a generally useful capability, and I hope that there's a >> more elegant way of incorporating it into web2py. As I was saying >> somewhere else, a generic.css file doesn't really work (unless you're >> generating all your CSS content in controllers). At the very least, >> perhaps the logic (where?) that creates the default html headers >> could >> recognize that it's got a .css file and create css headers instead. >> >> >> >>> On Oct 7, 8:19 am, Jonathan Lundell <jlund...@pobox.com> wrote: >>>> On Oct 6, 2009, at 2:54 PM, Eric Vicenti wrote: >> >>>>> Thanks, mdi and Jonathan. I will borrow your __cssheaders() >>>>> function, >>>>> if you don't mind. >> >>>> I should have commented it a bit first, but it's not all that >>>> subtle. >> >>>> By the time the controller runs, the response has been set up as a >>>> standard html page. So we delete cookies and the default caching, >>>> and >>>> use Expires caching (an hour as shown) so that, in theory anyway (I >>>> haven't tested this against browsers yet) the performance should be >>>> fairly close to static. >> >>>> When I'm actually working on the CSS, I reduce the Expires >>>> timeout to >>>> 3 seconds (or whatever you like) so that my changes appear >>>> promptly. >> >>>> Here's a slightly cleaner version: >> >>>> def __cssheaders(): >>>> ''' >>>> edit default response headers for CSS >>>> serve text/css with no cookies, limited caching >>>> ''' >>>> response.cookies = Storage() # no cookies >>>> for header in ('Last-Modified', 'Expires', 'Pragma'): >>>> if header in response.headers: del response.headers[header] >>>> response.headers['Content-Type']='text/css' >>>> response.headers['Cache-Control'] = 'max-age=3600, must- >>>> revalidate' >>>> response.headers['Cache-Control'] = 'max-age=3, must- >>>> revalidate' >> >>>>> Is there any way to have some code in the css.py controller which >>>>> will >>>>> automatically define functions for all my css files in /views/ >>>>> css/ ? >>>>> All it would need to do is run the __cssheaders() function and >>>>> return >>>>> dict(), but it feels unnecessary to maintain a list of functions >>>>> when >>>>> there are files. >> >>>>> -Eric >> >>>>> On Oct 6, 2:29 pm, Jonathan Lundell <jlund...@pobox.com> wrote: >>>>>> On Oct 6, 2009, at 1:35 PM, mdipierro wrote: >> >>>>>>> It is good practice to put in the CSS only relative urls to >>>>>>> other >>>>>>> static files. That is not a problem with web2py. >> >>>>>>> If you need to include in the CSS urls generated by {{=URL >>>>>>> (....)}} >>>>>>> then you should promote the css from a static file to a dynamic >>>>>>> page: >>>>>>> 1) make a controller for it >>>>>>> 2) make a view ending in .css >>>>>>> 3) call the action from the layout ending in .css >>>>>>> 4) in the .css view use absolute paths to include images >> >>>>>> When I tried serving dynamic CSS (I wanted to dynamically control >>>>>> colors, fonts, etc), I had to do some work to get the result >>>>>> served >>>>>> as >>>>>> text/css (which is necessary). I ended up hacking that (and some >>>>>> other >>>>>> header stuff, like caching) in the controller, but I suppose it >>>>>> could >>>>>> happen in the view. >> >>>>>> I notice that there's no generic.css. >> >>>>>> I have a controller, css.py, that looks like this: >> >>>>>> import time >>>>>> from gluon.storage import Storage >> >>>>>> def __cssheaders(): >>>>>> ''' >>>>>> edit default response headers for CSS >>>>>> serve text/css with limited caching >>>>>> ''' >>>>>> response.cookies = Storage() >>>>>> response.headers['Content-Type']='text/css' >>>>>> response.headers['Cache-Control'] = 'max-age=3600, must- >>>>>> revalidate' >>>>>> if 'Last-Modified' in response.headers: del response.headers >>>>>> ['Last-Modified'] >>>>>> if 'Expires' in response.headers: del response.headers >>>>>> ['Expires'] >>>>>> if 'Pragma' in response.headers: del response.headers >>>>>> ['Pragma'] >> >>>>>> def xxx(): >>>>>> ''' >>>>>> app/css/xxx.css >>>>>> set fonts and colors to be substituted via xxx.css view >>>>>> ''' >>>>>> __cssheaders() >>>>>> font_serif = 'cambria, georgia, "times new roman", serif' >>>>>> font_sans = 'corbel, "Helvetica Neue", Helvetica, Arial, >>>>>> sans- >>>>>> serif' >>>>>> return dict(dark_color="#8e1650", second_color="#635e54", >>>>>> light_color="#e9e7de", >>>>>> accent_color="#edab05", flash_color="white", >>>>>> flash_background="#8e1650", >>>>>> body_background_color="#e8e8e8", >>>>>> content_background_color="white", >>>>>> font_body=font_serif, >>>>>> font_logo=font_sans, >>>>>> font_head=font_sans,) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "web2py-users" group. To post to this group, send email to web2py@googlegroups.com To unsubscribe from this group, send email to web2py+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/web2py?hl=en -~----------~----~----~----~------~----~------~--~---