Add a custom view


A simple tutorial introducing the basics of Plone development.

In this tutorial we add a browser view. Plone uses views to display dynamic content.

For more information about views, see the Views and viewlets section.

There are 3 parts to our view; a Python class, a page template, and a ZCML registration. We will add the two files and edit configure.zcml in the browser directory.

Register the view

  • Edit the configure.zcml file in the browser directory. Open configure.zcml in your editor and add this just before the closing </configure> tag.:



  • We use the name attribute to access the view.
  • The for attribute allows us to limit context to a particular interface. With the asterisk, we are not limiting access.
  • The permission attribute allows us to limit access with a permission. In this case, we have a wide open permission.
  • The class attribute points to our Python class. Here, we have a HelloWorldView class in a file named
  • If you have not already done so, you need to define the browser namespace in configure.zcml by adding this to the configure tag.:


If you also went through the simple form tutorial, then configure.zcml should look something like this when you are done.:


  <include package="" />

  <!-- -*- extra stuff goes here -*- -->

    <!-- this is our simple form -->

    <!-- this is our custom view -->


Create Python class

  • We need to create our Python class. In the browser directory, create a file named and add the following code.:

    from Products.Five import BrowserView
    from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
    class HelloWorldView(BrowserView):
        template = ViewPageTemplateFile('')
        def __call__(self):
            self.hello_name = getattr(self.context, 'hello_name', 'World')
            return self.template()

Our BrowserView class is a Python callable. The __call__() method is the entry point. In it, we add a hello_name attribute to the view. Our page template will use the value of hello_name when the page is rendered.

The logic in our class is pretty simple. If our context has a hello_name attribute, then we use its value in our view. If not, we use the string 'World'.

In the class, we also define our page template and save it in the template attribute.

Create page template

The third part of our view is the page template.

  • In the browser directory, create a file named and add the following:

    <html xmlns=""
        <metal:block fill-slot="content-core">
                    <span tal:content="view/hello_name">this gets replaced</span>

Plone uses Zope Page Templates (ZPT). For great documentation on ZPT and the TAL, TALES, and METAL languages, see the TAL page templates section of the developer manual, and the TAL/TALES & METAL Reference Guide

In the above code, we define some xml namespaces; metal and tal that we use in our template.:


The metal:use-macro statement tells ZPT to use main_template to draw our page.:


The master template main_template contains predefined slots, or areas on the page, that we can fill with content from our template.

Anything in our code, between the:

<metal:block fill-slot="content-core">



gets put in the content-core area of main_template and is displayed on our page.

We also have access to our view class using the view namespace. In our case, we are interested in view/hello_name.:

<span tal:content="view/hello_name">this gets replaced</span>

In our Python class, we defined a hello_name attribute. This code says take the value of the view/hello_name attribute and use it as the content for the span tag. So, the string "this gets replaced" gets replaced with the value of view/hello_name.

Access the view

To access the view, restart your instance to have access to the new view

Then, add @@hello_world_view to the end of an object url in your plone site.:


Since our object has a hello_name attribute, the value is displayed along with the word Hello.


We can also call our view on the root of the site.:


The root of the site does not have a hello_name attribute, so Hello World is displayed.


For a more in depth explanation of views, see the Views and viewlets section of this manual.