Current

This document is valid for the current version of Plone.

Using Resource Registries programmatically

by Geir Baekholt last modified Dec 30, 2008 03:06 PM
Instructions for controlling the CSS and Javascript registries programmatically instead of through the user-interface. This is especially relevant for product authors or for crating file-system-based site-configuration products.

The by far most common need for using the ResourceRegistries programmatically is to register resources with the installation of products.
A very common use-case is 'site-configuration'-products and 'themes' that usually supply at least one CSS-file. (The equivalent of what we used ploneCustom.css for back in 2004). Other common uses are supplying custom javascript files, either with site-configurations or as client-side supplemements to custom products.

There are currently two main approaches to supplying registry configuration through filesystem code

  1. Python API calls, typically called from a product's Install.py module. Very flexible, but somewhat cumbersome and opaque to non-programmers. The old-style way of doing it. Has access to more (rarely needed) functionality than (2)
  2. GenericSetup XML configuration ( the new shiny way of supplying Plone configuration settings )

Configuring the ResourceRegistries from the Python APIs

ResourceRegistries/Interfaces/registires.py has the authorative list, but here are some examples. These examples are for the CSS-registry (chosen because it is more commonly used, and also more often used by non-programmers)

To work with the tool, you must first get your hands on the tool itself…
from Products.CMFCore.utils import getToolByName
CSSRegistry = getToolByName(self, 'portal_css')
To register a new resource ( the same as using the add-form in the UI ) , you can simply do
CSSRegistry.registerStylesheet(self, id)
…and stick with the defaults. They are usually just what you need anyway.

if you feel you need more detailed control, there are many options you can supply if you need
CSSRegistry.registerStylesheet(self, id, expression='', media='', rel='stylesheet',title='',
rendering='import',  enabled=1,
cookable=True, compression='safe', cacheable=True)
Using python calls for installation stuff is, however, rapidly becoming obsolete. GenericSetup is the new way, and luckily the ResourceRegistries also supports this scheme.

Configuring the ResourceRegistries with GenericSetup XML

Note that this approach is instead of the Python API approach listed above. You don't need both.
If you have not worked with GenericSetup yet, there is a nice tutorial from Rob Miller right here on plone.org. If you do know the basic of it, all you have to do is add a file called cssregistry.xml to your profile and let it contain something like this
<?xml version="1.0"?>
<object name="portal_css" meta_type="Stylesheets Registry">
<stylesheet title="" cacheable="True" compression="safe" cookable="True"
    enabled="1" expression="" id="css-test.css" media="screen"
    rel="stylesheet" rendering="import"/>
</object>
As you can see, the parameters are the same as through Python or through the user-interface-forms.

Contribute

Something wrong or out of date? Anybody can edit or create a new article in the knowledge base. Simply create an account on this site, log in, and click the Edit button to contribute.