Grok component to include resources.
Project description
megrok.resourceviewlet is a package meant to include resources using layer, context and view discriminations.
Setup
Let’s import and init the necessary work environment:
>>> import grokcore.component as grok >>> from grokcore import view, viewlet >>> from zope.testbrowser.testing import Browser >>> browser = Browser() >>> browser.handleErrors = False
Library
We first declare a resource. We’ll include it in our page:
>>> from megrok import resource >>> class SomeResource(resource.ResourceLibrary): ... resource.path('ftests/resources') ... resource.resource('thing.js') >>> grok.testing.grok_component('library', SomeResource) True
Components
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) True
Manager
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) True
Viewlet
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 = [SomeResource] >>> grok.testing.grok_component('viewlet', SomeViewlet) True
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
Rendering our page should render the ResourcesManager and therefore, include our resource:
>>> browser.open('http://localhost/@@index') >>> print browser.contents <html><head> <script type="text/javascript" src="http://localhost/@@/++noop++.../someresource/thing.js"></script> </head></html>
It works ! Enjoy.
Changelog
0.1 (2009-12-24)
Initial release
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Hashes for megrok.resourceviewlet-0.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2263b79209b2b452770abd894d1c1fac15716e831bed785c1ab418429be2b6fb |
|
MD5 | d6e1af81f79255575b5e11fe0d9d2ad3 |
|
BLAKE2b-256 | eb55eef998ba12d9a82d3f3f2000e68ebb325946dc3dff9e14d5890d7537a962 |