Ñò Tã·Jc @ s¼ d Z d d k Z d d k Z d d k l Z d d k Z d d k l Z e i e ƒ Z d d g Z d d „ Z d e f d „ ƒ YZ e ƒ i Z e i d e i e i Bƒ Z d „ Z d S( s1 .. testoutput:: :hide: >>> # This is so the doctests start from a clean state >>> from tw.core.util import install_framework; install_framework(True) ToscaWidgets can inject resources that have been registered for injection in the current request. Usually widgets register them when they're displayed and they have instances of :class:`tw.api.Resource` declared at their :attr:`tw.api.Widget.javascript` or :attr:`tw.api.Widget.css` attributes. Resources can also be registered manually from a controller or template by calling their :meth:`tw.api.Resource.inject` method. When a page including widgets is rendered, Resources that are registered for injection arre collected in request-local storage area (this means any thing stored here is only visible to one single thread of execution and that its contents are freed when the request is finished) where they can be rendered and injected in the resulting html. ToscaWidgets' middleware can take care of injecting them automatically (default) but they can also be injected explicitly, example:: >>> from tw.api import JSLink, inject_resources >>> JSLink(link="http://example.com").inject() >>> html = "
" >>> inject_resources(html) '' Once resources have been injected they are popped from request local and cannot be injected again (in the same request). This is useful in case :class:`injector_middleware` is stacked so it doesn't inject them again. Injecting them explicitly is neccesary if the response's body is being cached before the middleware has a chance to inject them because when the cached version is served no widgets are being rendered so they will not have a chance to register their resources. iÿÿÿÿN( t Request( t MultipleReplacert inject_resourcest injector_middlewarec s ‡ ‡ f d † } | S( sE Middleware that injects resources (if any) into the page whenever the output is html (peeks into Content-Type header). Normally you don't have to stack thi yourself because :class:`tw.core.middleware.ToscaWidgetsMiddleware` does it when it is passed the ``inject_resources`` flag as True (default). c s t | ƒ } | i ˆ ƒ } | i i d d ƒ i ƒ } d | j o? t i i ƒ } | o% t | i | | i d ˆ ƒ| _ q‚ n | | | ƒ S( Ns Content-Types text/plaint htmlt render_filter( R t get_responset headerst gett lowert twt frameworkt pop_resourcesR t bodyt charset( t environt start_responset reqt respt content_typet resources( R t app( s= /usr/lib/python2.6/site-packages/tw/core/resource_injector.pyt _injectorB s )( ( R R R ( ( R R s= /usr/lib/python2.6/site-packages/tw/core/resource_injector.pyR 9 s t _ResourceInjectorc B s2 e Z d „ Z e d „ Z d d d d d „ Z RS( c C sc t i | h | i d ƒ d 6| i d t ƒ d 6| i d ƒ d 6| i d t ƒ d 6t i t i Bƒ S( Nt heads