Personal tools
You are here: Home Zope & Plone tips Zope 3 for beginners
Navigation
 
Document Actions
  • Send this page to somebody
  • Print this page
  • Add Bookmarklet

Zope 3 for beginners

by tibi last modified 2007-08-27 20:39

The following tutorial is a simple document meant at introducing Zope 3 to beginner web developers. Explanations are kept simple and some things are taken "idiomatically". It is also a work in progress which I'll update and improve when time will allow me to.

Why Zope 3?

This is a hard question. If you feel the need to learn a serious Python web framework that will allow you to develop advanced websites, in an extensible matter, with the help of packages created by a thriving community of developers, then Zope is for you. If you're happy with the SQL or procedural driven development styles of, let's say PHP, then perhaps Zope 3 will feel very heavy to you. Zope 3 also tries to keep the development process "pythonic" (which means that it tries to follow the python standards in terms for code development and general philosophy), so it may be the right choice if you're trying to learn (advanced) Python, too.

One of the unique characteristics of Zope 3 is its extensibility. The model promoted in this document will make it easy to create "components" which can be interconnected and swapped with other 'plugins'. To understand Zope's Component Architecture you need a good understanding of the OOP model.

Hello world!

I'm assuming you have followed the instructions on the zope.org site and have installed the Zope 3 application server, on a Linux system and have created a zope instance somewhere in your home directory, by following the installation instructions. Take your time to start zope and play with its administration interface (often called the ZMI - Zope Management Interface).

As I have mentioned, Zope 3 makes development "pythonic", which means we'll have to organize our work in a Python package, a folder located in the <zopeinstance>/lib/python, which contains an empty __init__.py file. (This package can also be in the site-packages folder from python, or any other folder located in python's sys.path, but it's easier to develop and keep things organized in the lib/python folder from the zope instance. Let's call this folder "learning".

Inside this folder let's create a page using a template file named "hello.pt". Unlike (let's say) php, where the templating language is the same as the programming language, Zope uses a special template language for templating, the Zope Page Templates. The templates produced for this engine are simple xhtml files, with two added namespace extensions: tal and metal; for a general introduction there is the ZPT chapter from the Zope Book, which is targeting the Zope 2 server, so don't be surprised if some of the path expressions used on that page will fail on z3.

For the file content, add a simple and classic:

<h1>Hello world!</h1>

and save it. Again, unlike php or other web-frameworks that value convention over configuration and implicit over explicit, zope 3 is quite verbose when it comes to configuring stuff: it won't assume much and any mistake done in configuration will result in a failure to start the server. So, we need to tell Zope how to use our template to publish a page: create inside the "learning" folder a file called "configure.zcml". Inside this file add the following content:

<configure xmlns:browser="http://namespaces.zope.org/browser"
  xmlns="http://namespaces.zope.org/zope">
  <browser:page name="hello.html" template="hello.pt" permission="zope.View" for="*" />
</configure>

This tells Zope that we want a page named "hello.html", using the template "hello.pt" for whoever has the permission "zope.View" (which is everybody, by default), page which will be available for all objects published through Zope (the meaning of the star for="*").

Next, we need to tell Zope to load this configure.zcml file, and this is done by placing a file named "learning-configure.zcml" in <zopeinstance>/etc/package-includes/ (the file can be named anything as long as it ends with -configure.zcml). This file should contain:

<include package="learning" />

Restart Zope (this is important for any code change that isn't limited to template code) and navigate to http://localhost:8080/hello.html. You should see the rendered headline.

Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 License.
 

Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: