chameleon.zpt versus standard Zope page templates

Chameleon.zpt is a fast Zope Page Template implementation based on the Chameleon template compiler. It's largely compatible with zope.pagetemplate. Some benchmarks says Chameleon is 16x faster ! Chameleon page templates differ from standard Zope page templates in a few simple aspects which I propose to discover.

chameleon.zpt

The default expression type is Python

With zope.pagetemplate

<div tal:content="python:2+2" />

With chameleon.zpt

<div tal:content="2+2" />

Tuple unpacking is allowed when defining variables

With zope.pagetemplate

<tal:vars define="a python:1; b python:2; c python:3">
...
</tal:vars>

With chameleon.zpt

<tal:vars define="(a, b, c) [1, 2, 3]">
...
</tal:vars>

Generators are allowed in tal:repeat statements

Note that the repeat variable is not available in this case.

With chameleon.zpt

<div tal:repeat="i <some generator>">
...
</some>

Attribute-access to dictionary entries is allowed in Python-expressions

With zope.pagetemplate

<span tal:define="mydict python:{'e1': 'one', 'e2': 'two'}"
      tal:context="python:mydict['e1']"> using dictionary['key'] </span>

With chameleon.zpt

<span tal:define="mydict {'e1': 'one', 'e2': 'two'}"
      tal:content="mydict.e1"> using dictionary.key </span>

Genshi expression interpolation syntax is supported outside tags and inside static attributes

With zope.pagetemplate

<span tal:define="world string:'css'"
      class="" tal:attributes="class string:hello-$world"
      tal:content="string:Hello, $world!">
   Hello, css!
</span>

With chameleon.zpt

<span tal:define="world 'css'"
      class="hello-${'world'}">
   Hello, ${'world'}!
</span>

Notes

Chameleon templates and macros/slots for Grok Views

Posté par Sylvain
The main template: mymacro.cpt: 

<html metal:define-macro="mypage"> 
<head></head> 
<body> 
The content: 
<div metal:define-slot="mycontent"> 
Put your content here... 
</div> 
</body> 
</html> 

Use the "mypage" in a new template: mypage.cpt: 
<html metal:use-macro="path:context/@@mymacros/template/macros/mypage"> 
<!-- slot 'mycontent' was defined in the macro above --> 
<div metal:fill-slot="mycontent"> 
... 
</div> 
</html>

Using Chameleon in debug mode

Posté par Sylvain
$ export CHAMELEON_DEBUG=True 
$ export CHAMELEON_CACHE=False
Add comment

Vous pouvez ajouter un commentaire en complétant le formulaire ci-dessous. Le format doit être plain text. Les commentaires sont modérés.

Question: 1 + one =
Votre réponse: