Current

This document is valid for the current version of Plone.

Example Conditions

by Anne Bowtell last modified Jul 31, 2009 06:36 PM
Contributors: Yuri Carrer
Some examples of limiting Style Sheets by the condition parameter, to help you get started.

In these examples we've given the condition as it would appear in profiles/default/jsregistry.xml and cssregistry.xml. If you are working through the web, then the text within the inverted commas "...." can simply be dropped into the condition box for the relevant CSS or JavaScript file in portal_css or portal_javascripts.

Content Type

If your CSS or JavaScript is only relevant to a particular content type:

expression = "python:object.meta_type == 'ATFolder'" 

View

You can check a specific browser view for a property (this example is from Products.Maps):

<javascript 
    cacheable="True" 
    ...
    expression="object/@@maps_googlemaps_enabled_view/enabled 
                                                | nothing" />

Here's how to look up one of the global views (see the Using Other Information about your Site Section for more about these views). This example delivers the Plone RTL Style Sheet:

<stylesheet 
    ...
    expression="python:portal.restrictedTraverse
                             ('@@plone_portal_state').is_rtl()"
    id="RTL.css" 
    .../>

Role

The following will check whether the visitor is logged in or not - in this case it delivers the standard member.css if the visitor is not anonymous:

<stylesheet 
    ...
    expression="not: portal/portal_membership/isAnonymousUser"
    id="member.css" 
    .../>

This example does the same, but uses the plone_portal_state view

expression="python: not here.restrictedTraverse
                        ('@@plone_portal_state').anonymous()"

Section and/or place in the site

Note that the body tag of every page has a section and template CSS class. So you may not need to use the resource registries at all.

However, if you want to change styles for a folder that isn't at the top level of your site, or, if you want to supply some JavaScript at a specific point in the site, then you will need to write a script and call this via the resource-registry expression. The section class on the body tag in main_template is generated by a Python script getSectionFromURL. This is a good starting point for your own script.

Here's an example expression and script. This will return True for a folder of your site with the shortname 'news' and any object within it. In this case - unlike the default Plone setup - the news folder is one level down from the top level:

expression="python: here.getSectionInTree(object,1)=='news'"
## Script (Python) "getSectionInTree"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=object, position
##title=Returns a section name
##
contentPath = object.portal_url.getRelativeContentPath(context)
if not contentPath:
    return ''
else:
    if len(contentPath)-1 >= position:
         return contentPath[position]
    else:
         return ''