Fanstatic resources¶
Publishing static resources (e.g. javascript, css, images, files) with fanstatic.
Fanstatic¶
From the fanstatic homepage at http://fanstatic.org :
“Fanstatic is a small but powerful framework for the automatic publication of resources on a web page. Think Javascript and CSS. It just serves static content, but it does it really well.”
In conjunction with the zope.fanstatic package it is the default mechanism in grok to publish your static resources. It serves the static resources via WSGI which will off-load this task from Grok, generally improving the application’s performance.
Getting started¶
For a basic example run the grokproject tool. The generated project will have the infrastructure set up for serving static resources. Look at app.py and resources.py.
Setting up a resource library¶
In order to serve static content, you need to register a Fanstatic Library for each of the static directories your project may have.
Registering the ‘static’ directory involves three steps:
Add zope.fanstatic and fanstatic to the install_requires in your project’s setup.py. Include zope.fanstatic in your configure.zcml.
Include the following code in resource.py
from fanstatic import Library, Resource library = Library(PACKAGENAME, 'static') myStyleSheet = Resource(library, 'style.css')
Where the PACKAGENAME should be replaced with the dotted package name in which the ‘static’ directory resides.
In the setup.py of your project, add the following entry point (again, for each ‘static’ directory your project may have)
'fanstatic.libraries': [ 'PACKAGENAME = PACKAGENAME.resource:library' ]
Using the resource library¶
Using the resources
from mypackage import resource
class Index(grok.View):
def update(self):
resource.myStyleSheet.need()
This will include a <link> to your stylesheet in the view’s generated html code.