Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (
Help us improve Python packaging - Donate today!

Grok components to include resources.

Project Description

megrok.resourceviewlet is a package meant to include resources using layer, context and view discriminations.


Let’s import and init the necessary work environment:

>>> import grokcore.component as grok
>>> from grokcore import view, viewlet
>>> from import Browser

>>> browser = Browser()
>>> browser.handleErrors = False


We first declare a resource. We’ll include it in our page:

>>> from fanstatic import Resource, Library
>>> myLibrary = Library('test_library', 'ftests/resources')
>>> Thing = Resource(myLibrary, 'thing.js')

This step is done by an entry point. For the testing, we do it by hand:

>>> from zope.fanstatic.zcml import create_factory
>>> from zope.component import getGlobalSiteManager
>>> from zope.publisher.interfaces.browser import IBrowserRequest
>>> from zope.interface import Interface

>>> resource_factory = create_factory(myLibrary)
>>> getGlobalSiteManager().registerAdapter(
...      resource_factory, (IBrowserRequest,), Interface,


To demonstrate our resource viewlet, we first need a page to render. This page contains a content provider named ‘resources’:

>>> from zope.interface import Interface

>>> class Index(view.View):
...   view.require("zope.Public")
...   view.context(Interface)
...   template = view.PageTemplate("""<html><head>
...     <tal:resources replace='provider:resources' />
...   </head></html>""")

>>> grok.testing.grok_component('index', Index)


We now register a content provider named ‘resources’. It will be a ResourcesManager. An ResourcesManager is a component dedicated in rendering ResourceViewlets:

>>> from megrok.resourceviewlet import ResourcesManager

>>> class Resources(ResourcesManager):
...   viewlet.context(Interface)

>>> grok.testing.grok_component('resources', Resources)


Now, we register a ResourceViewlet, including our resource. The declaration is very straightforward:

>>> from megrok.resourceviewlet import ResourceViewlet

>>> class SomeViewlet(ResourceViewlet):
...   viewlet.context(Interface)
...   resources = [Thing]

>>> grok.testing.grok_component('viewlet', SomeViewlet)

By default, a ResourceViewlet is registered for an instance of ResourcesManager. Most of the time, a page contains only one of these content providers. If it’s not the case, make sure to provide your own viewletmanager directive value.


Rendering our page should render the ResourcesManager and therefore, include our resource:

>>> print browser.contents
  <script type="text/javascript"

It works ! Enjoy.


0.2 (2011-01-18)

  • megrok.resourceviewlet now works and depends on fanstatic.
  • Dependencies has been greatly reduced.

0.1 (2009-12-24)

  • Initial release
Release History

Release History

This version
History Node


History Node


Download Files

Download Files

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

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
megrok.resourceviewlet-0.2.tar.gz (7.2 kB) Copy SHA256 Checksum SHA256 Source Jan 18, 2011

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting