Customizing AT Templates
Plone Theme Reference
1. Introduction
Goals, Pre-Requisites, and Tools
So you think Archetypes' way of automagically generating HTML to view your object isn't pretty enough? Well, you've come to the right place! I'm going to teach you how to dress up those boring, drab views and make them shine!
Goals: What will I learn?
- How Archetypes generates views for content objects
- How much control Archetypes gives you
- How to change the HTML output for a particular field by creating a custom widget template
- How to use the Archetypes template framework to make minor changes to the default AT-generated view
- How to customize the HTML output for the entire view of an Archetypes object by using the
title,body,folderlisting, andfootermacros
Pre-Requisites: What do I need to know?
- How to read and write Python code
- How to read and write Zope Page Templates (ZPT)
- How to create Archetypes-based products (ArchGenXML is acceptable)
Tools: What do I need to have installed?
- Plone 2.0 or 2.1
- Archetypes (included by default in Plone 2.1)
- The ATViewTutorial product - this product has examples of the concepts in this tutorial
2. What Makes It Tick?
This page describes how Archetypes uses different templates to generate HTML, and how AT-based template customization can be applied.
Archetypes has a very clever system for generating HTML pages for AT-based objects. The same set of templates generates all of the content areas for all AT-based objects. This buys you the following benefit, useful for site consistency:
- All the pages look the same.
However, it also has the following drawback:
- All the pages look the same.
Different types of content need to be displayed differently. Let's dig into how Archetypes does things, so we can figure out how to make your content types shine!
The base_view Template
The base_view template (found in the archetypes skin) handles selecting the appropriate
macros from the appropriate templates, and using those macros to display content objects.
If you look at this piece of code from 'base_view':
<tal:block define="portal_type python:here.getPortalTypeName().lower().replace(' ', '_');
view_template python:'%s_view' % portal_type;
view_macros python:path('here/%s/macros|nothing' % view_template);
macro view_macros/css | nothing"
condition="macro">
You can see that it defines the variable view_template as the object's Portal Type Name converted to lowercase, with spaces replaced with underscores (_), followed by _view. So, MyType's view template, for instance, would be called mytype_view.
Now, before we move on, I must warn you: don't edit base_view. Seriously. Don't.[1]
No, really. Don't customize base_view. If you think you need to customize base_view, first, well... don't. Keep reading the tutorial. If you're certain, after reading the tutorial, that you need to customize base_view, again, don't! Write a clear, concise example indicating why, after reading this tutorial, you believe you should customize base_view, and send it to the archetypes-users mailing list. If you really do need to customize base_view, you've found a shortcoming in Archetypes, and the people on the list will inform you if that's the case. So, repeat after me: "Don't customize base_view." Good!
Now, there are six important macros to be aware of. These six macros give you the power to insert template code that is customized for your class. These macros are:
js- A macro to insert javascript into the
<head>tag of the generated HTML page css- A macro to insert CSS includes and style code into the
<head>tag of the page header- The macro that defines the topmost portion of the content area. By default, this macro has an
<h1>tag that contains the title, and links for printing, emailing, etc. on the right. body- The macro that defines the "body" area of the content. This is where the fields and their values are displayed.
folderlisting- This macro shows a list of the child content for the object. Don't confuse this with
folder_contents, this is what theviewtab shows for folderish objects. Folderish objects use both thebodymacro and thefolderlistingmacro. footer- This is where AT puts the byline.

As you can see, the header macro generates what's outlined in the area marked ''header'' in red, the body macro generates the content just beneath it, and the folderlisting macro generates the listing of the objects within the folderish object.
The base_view template automatically pulls the appropriate macro from the custom view template (mytype_view, from our earlier ad-hoc example), or from the next template that we are going to explore: base.
The base Template
The base template contains four of the six macros that base_view looks for:
headerbodyfolderlistingfooter
The only reason why I mention base is so that you know where AT's default behavior comes from. This is important if you only want to change a little bit of a type's view. It's often helpful to copy the macro from base into your custom view template, and then start tweaking and customizing.
Widgets
Widgets are what Archetypes uses to display fields. Widgets have two parts:
- The widget class
- This class defines data and behavior for the widget. In most cases, you'll never need to create a custom 'Widget'-derived class. See Archetypes/Widget.py for examples.
- The widget template
- This is a ZPT that provides three macros:
view,edit, andsearch. These macros display the field. Some of the macros depend on certain variables being defined in the calling template, so pay close attention. Most often, you'll only need to provide a custom widget template, and not a custom widget class.
There are all kinds of widgets out there to do all kinds of things. The Archetypes Quick Reference Manual covers the details for the various widgets in Archetypes.
[1] Unless you're wiggy.
3. Customizing Widgets
This page shows you how to customize widgets, and gives some examples of what kinds of neat tricks can be done with widget customization.
4. Total Control: The View Template
This page describes how you can control every portion of the HTML output in the content area by creating a custom view template.
Customizing Labels
5. Conclusion
Some final notes about customizing Archetypes view templates
So, now you should know all of the following information:
- How to identify which parts of an Archetypes view template are generated by the
header,body,folderlisting, andfootermacros - How to create a custom view template that overrides one or more of the
header,body,folderlisting, andfootermacros - How to create a custom widget template that works in the Archetypes framework
- How to create a custom
bodytemplate that uses Archetypes' widget rendering templates - How to inject custom CSS code and links to custom CSS files into your view template
Some Final Notes
I want to cover a few details about how to apply all of these tools. Some wise guy somewhere said something like, "To a man whose only tool is a hammer, every problem tends to look like a nail." Your success with Archetypes is very contingent upon selecting the appropriate tool for your specific problem. So, use the following outline of the basic AT page layout as a guideline to determine what should be customized:
headermacro- Title (or id if no title is present)
- Document actions (e.g. print, send to)
bodymacro- List of fields
- Field label (from the
labelmacro in the view template, if one exists) - Field value (from the widget template's
viewmacro)
- Field label (from the
- List of fields
folderlistingmacro- List of links to each sub-object
footermacro- byline
So, based on which parts of this standard layout you need to customize, use the appropriate macro. Keep the infamous "Foo" template around to help you with debugging. See the next page for a reference on customizing Archetypes view templates.
6. Reference
A reference for customizing Archetypes view templates
View Templates
View templates are named according to the portal_type of the class. To create the name for a view template, follow these steps to create the name of the template:
- Change the
portal_typeto lowercase. - Replace all spaces with underscores (
_). - Append
_viewto the end of the name.
Archetypes will automatically locate templates with names created according to the above steps, and will make use of the macros defined within the template. View templates can define one or more of the following macros:
css- A macro for inserting type-specific CSS code, including
<link>tags pointing to custom CSS files. There is no default macro for this within Archetypes; Archetypes uses the existing CSS styles in Plone to render AT-based objects. header- This macro, by default, generates the
<h1>tag containing the object's title and the document actions (print, rss, e.g.) body- The location where the fields and values are displayed by default. When rendering fields using the existing widget mechanism, be sure to
tal:definethe variablefieldas the current field; the widget templates depend on this variable being set. folderlisting- This is the folder listing display when viewing the
viewtab of a folderish object. This is not the same as thecontentsview. footer- By default, this is where Archetypes puts the byline.
label- This template generates field labels.
For any of these macros that is not defined in the custom view template, Archetypes will use the default behavior in its place, taken from base or widgets/field.
Widget Templates
Use custom widget templates by naming them in the schema - insert a macro parameter into the widget's constructor in the schema, and set the value to the name of the template. For example, macro="my_widget_template". Widget templates must have the following three macros:
vieweditsearch
Widget templates have the following local variables available within TALES expressions:
accessor- The accessor method for the current field. The code
<p tal:content="accessor" />will cause the field's value to be written within the<p>tag. fieldName- The name of the field.
widget- The widget object for the field.
mode- Will always be
viewfor view templates, but is useful for, say, error checking.





Author: