Expressions

Expressions are user or site-administration through-the-web-written conditions which can be evaluated. You might want to use expressions in your own add-on product to provide user written conditions for viewlet visibility, portlets, dynamic text, etc.

Plone has built-in support for TALES expression. They are internally used at least in Javascript and CSS machinery to pick the files, actions to determine whether the action is visible or not.

Expression variables are defined in CMFCore/Expressions.py:

data = {
    'object_url':   object_url,
    'folder_url':   folder.absolute_url(),
    'portal_url':   portal.absolute_url(),
    'object':       object,
    'folder':       folder,
    'portal':       portal,
    'nothing':      None,
    'request':      getattr(portal, 'REQUEST', None),
    'modules':      SecureModuleImporter,
    'member':       member,
    'here':         object,
    }

Examples

Some sample expressions:

python:if object.getSomething() == True

string:object/Title

object/Title

object/getSomething

Getting the current language

We traverse to plone_portal_status view (documented at plone.app.layout.globals.interfaces.IPortalStatus interface) and call its language() method to get the current language.

The language is returned as two letter language code.

Example:

object/@plone_portal_status/language

See chapter View helpers to get more information how to use utility views in expressions.

Using expression in your own code

Expressions are persistent objects. You usually want to attach them to something, but this is not necessary.

Example:

from Products.CMFCore.Expression import Expression, getExprContext

# Create a sample expression - usually this is taken from
# the user input
expression = Expression("python:context.Title() == 'foo')

expression_context = getExprContext(self.context)

# Evaluate expression by calling
# Expression.__call__(). This
# will return whatever value expression evaluation gives
value = expression(expression_context)

if value.strip() == "":
        # Usually empty expression field means that
        # expression should be True
        value = True

if value:
        # Expression succeeded
        pass
else:
        pass

Custom expression code

If you need to add complex Python code to your expression conditions it is best to put this code to BrowserView class and call the view method in the expression:

object/@@my_view_name/my_method

And:

class MyViewName(BrowserView):
    """ Exposes methods for expression conditions """

    def my_method(self):
        """ Funky condition

        self.context = object for which this view was traversed
        """
        if self.context.Title().startswith("a"):
            return True
        else:
            return False

Table Of Contents

Previous topic

Calendar related objects

Next topic

History and versioning

This Page