Home Documentation Coding Standards View Classes for Zope 2 Developers
Document Actions

View Classes for Zope 2 Developers

by Tom Hoffman last modified 2006-04-08 02:26

Moving logic from ZPT into Python.

This weblog post by Paul Everitt, while focusing on Plone development, does a good job of laying out the difference between the way logic has "traditionally" been embedded in page templates in Zope 2 development compared to the use of view classes to separate logic and presentation in Zope 3 and SchoolTool development:

  1. Complex expressions logic should not be used in a page template... Instead, all complex logic should be put in view class methods. This helps "encapsulate" the logic in one place, and makes for much clearner template design.
  2. A result of this first guideline is that a template should not try to deal with complex, high-level Zope objects, this complex object handling should happen in a view class. As a rule then, view classes' methods should only return simple data objects like strings, numbers, dictionaries, lists, and any other Python built-in, but should never hand a template a content object.

Some further guidelines for deciding whether an expression belongs in a view class or not:

If it is just a simple expression with strings and numbers, for example, one used to build dynamic URLs or paths that are UI only, then it is safe, perhaps even desireable, to leave the expression in the template. But if the expression uses any content objects or tools, or performs complex traversal, or implements any complex semantics at all, then it should be moved into a view class. Almost *all* python TAL expressions will fall into this category.


Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: