Extending Chameleon/five.pt

I'd love to extend Plone's templates as easily as I can in Django. At Chameleon level it seems easy, but the code below failed in a Plone's browser view, even though I made sure five.pt was a dependency in my build:

import ast
def uppercase_expression(string):
    def compiler(target, engine):
        uppercased = self.string.uppercase()
        value = ast.Str(uppercased)
        return [ast.Assign(targets=[target], value=value)]
    return compiler

  from chameleon import PageTemplate
  PageTemplate.expression_types['upper'] = uppercase_expression

In my template I have:

<div tal:content="upper:'mystring';" />

Which fails with:

Compilation failed
exceptions.LookupError: Unknown expression type: u'upper'

Is extending possible/should I look somewhere else? I'm running Plone 4 but the same happens in 5.

PS: I know this simple example can be implemented with a view method, allow_module or even a simple python: expression but template extendibility is a nice selling point and may be useful for other usecases.

I've got no idea for the answer, but did you try sticking PDB in your template as mentioned here?

Actually, that is no longer possible when you have the April hotfix installed.
See https://plone.org/security/hotfix/20160419/bypass-restricted-python

1 Like

What a pity. I agree this should be avoided for Restricted Python but not in filesystem code.

Oh, right, there is a difference. I forgot. In file system templates it should actually still work.
Or rather: in Zope 3 templates it should work, in Zope 2 templates not.

This means: in browser views, viewlets, and portlets you can use this. In skin templates, templates in portal_skins/custom, portal_view_customizations and templates in for example PloneFormGen overrides you cannot use this.