Personal tools
You are here: Home Documentation Tutorials Using the Resource Registries to control CSS and Javascript Using Resource Registries programmatically
Support

Get Help

Join our chat rooms or support forums if you have more specific questions.

Plone Training
Learn how to design, build, and deploy a website in Plone through one of the numerous Plone training sessions around the world.
Find Plone training…
 
Document Actions

Using Resource Registries programmatically

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.

Geir Baekholt

Plone has two neat tools for managing Cascading Stylesheets and Javascript in a handy way. This tutorial explains some whys and hows and even has a minimal practical example of how it works.
Page 5 of 5.

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.
 
by Geir Baekholt last modified December 31, 2006 - 00:15 All content is copyright Plone Foundation and the individual contributors.

For any issues with the web site functionality, please file a ticket.

Please consult the policy on plone.org content if you want your content published on this site.

Servers and hosting by