Skip to main content

WSGI middleware to manipulate css and javascript in the HTML header.

Project description

See ‘src/zif/headincludes/README.txt’ for more information.


0.2 (2007/04/13)

Correct releases README.

0.1 (2007/04/13)

Initial release.

Detailed Documentation


This is a wsgi middleware application intended for use with paste.deploy, zope.paste, and zope3.

It serves as a wsgi filter to create on-the-fly < script> and <style> tags inside the <head> of HTML documents. It was designed for output from a Zope3 application, but the wsgi filter itself, in, has no Zope3 dependencies.

The idea is that subobjects of a document may separately need special resources, but it is difficult to know whether a resource is asked for multiple times when documents are dynamically generated. For Zope3, headincludes replaces the functionality of zc.resourcelibrary, which also implements this idea. In fact, mostly because headincludes “borrows” code from zc.resourcelibrary, they may be installed side-by-side, but only one can be used at a time, because they both implement <zope:resourceLibrary> tags for zcml and the <tal:resource_library> statement for PageTemplates.

headincludes works by creating a key in the wsgi environment, ‘wsgi.html.head.includes’ that is a list of urls that need to be referenced in the head of the HTML document for the current request. The application registers a need for the reference by appending the url for the reference to the list. Urls that end in “.css” and “.js” in that list trigger the middleware to insert <style> or <script> tags into the <head> of the document after the application is done creating the HTML.

headincludes tries to maintain as much compatibility as possible with zc.resourcelibrary. The need() function has been rewritten, and using the headincludes version will be a simple matter of changing the import statement. The <tal:resource_library> statement is also still functional.

One new thing headincludes allows is arbitrary includes without needing to register the resource. urls can be placed in the includes list at any time that request.environ can be accessed. Just append any desired url to the list, e.g.,

except KeyError:
    (handle case when the filter is not available)

Alternatively, headincludes has a utility that provides IHeadIncludeRegistration:

from zope.component import getUtility
from zif.headincludes.interfaces import IHeadIncludeRegistration
registrar = getUtility(IHeadIncludeRegistration)
if registrar:


For zope3, headincludes requires Sidnei da Silva’s zope.paste

zope.paste is available at

cd []/zope
svn co paste

Instructions for zope.paste are at

zope.paste requires paste.deploy. paste.deploy can be obtained from the cheese shop. Presuming you have setuptools installed,

sudo PasteDeploy

This (headincludes) package can be unzipped and installed anywhere on the Python path.


Follow Sidnei’s instructions for setting up zope.paste. It involves putting the usual zope.paste-configure.zcml file in [zope3 instance]/etc/site-packages. There is also a parameter to change in [zope3 instance]/etc/zope.conf. The new twist is a paste.ini file in [zope3 instance]/etc

For Zope3, copy the headincludes-configure.zcml and headincludes-meta.zcml files into [zope3 instance]/etc/package-includes directory.

An example paste.ini file looks like:

pipeline = gzipper headincludes main

paste.app_factory = zope.paste.application:zope_publisher_app_factory

nocompress=jp gz zip



The paste.ini file above shows an example of the configuration option for headincludes

  • location is where in the <head> you want the new tags. “top” is the default, and places the new script and/or style tags just after the <head> element. Any other value will place the tags just before the </head> tag.


Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

zif.headincludes-0.2.tar.gz (10.9 kB view hashes)

Uploaded Source

Built Distribution

zif.headincludes-0.2-py2.4.egg (24.0 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page