I've just committed yet another experimental way to inject resources,
this time without using lxml.
The alternative seems to be too good to be true, I'm afraid it might be
even naive :). Resources are injected with a simple regexp substitution
on the rendered html string after widgets have been displayed and
registered their resources, example:
from toscawidgets import framework
from toscawidgets.api import inject_resources
html = render_template()
# At this point widgets have been displayed and registered their
# resources at a request-local storage area so can pop them out.
resources = framework.pop_resources()
# Now we inject it into the html string
html_with_resources = inject_resources(resources, html)
# return it to the client...
For convenience, TGWidgetsMiddleware can take care of injecting them
automatically when the response's content-type is html. This behavior is
disabled by default in order not to double-include them in current
projects which use a base template to include them (with "match"
directives when using Genshi or by inheriting a base template when using
Mako). To enable it just flip the "inject_resources" flag when
configuring the middleware:
app = TGWidgetsMiddleware(app, host_framework, inject_resources=True)
Note that that it is exactly the same it was when using the old lxml
method, no need to change anything.
When using this method make sure you don't use a master/base template.
The advantages:
* No dependency on lxml, one C dependency less. :)
* supports both html and xhtml outut.
* Much, much easier to set up and less intrusive. This makes integration
into a wsgi framework/app as easy as configuring and stacking the
middleware.
* Resources are registered when a widget is displayed. This means
widgets can be imported, instantiated or even declared inside the
template, no need to pass them in an awkward pylons.c.w object or sent
through the output dict in TG [1].
* Better separation of view and controller layers. Widgets now only need
to be imported into the controller if used to validate input. Pure "view
widgets", such as a menu, crumbs, etc... don't need to be imported in
the controllers)
Disadvantages:
* Cannot be used in TG 1.x ATM. It can probably be adapted quite easily
after this method gets more eyeballs.
I can't see anymore (this is probably bad...). All tests pass and I'm
trying it out in a project and works like a charm. :)
I'd really appreciate some testing and feedback so I can make this
method the default and deprecate the old one.
Alberto