Even though the content components are registered now, nothing interesting will happen, because there exists only a programmatic way of adding and editing the new components. Thus we are going to define some very basic browser views to make the content components accessible via the browser-based user interface.

即使内容组件现在被注册了, 可没什么有趣的事发生, 因为这里仅存在通过程序方式增加和编辑新组件。因此我们将定义通过以浏览器界面方式接入的基本视图

First create a package called browser (do not forget the init.py file) inside the messageboard package. Add a new configuration file, configure.zcml, inside browser and insert the following content:

首先在messageboard包里创建一个browser的包(不要忘记了init.py文件),在该包下面新增一个配置文件configure.zcml,在该文件中插入如下内容:

1  <configure
2      xmlns="http://namespaces.zope.org/browser">
3  
4    <addform
5        label="Add Message Board"
6        name="AddMessageBoard.html"
7        schema="book.messageboard.interfaces.IMessageBoard"
8        content_factory="book.messageboard.messageboard.MessageBoard"
9        fields="description"
10        permission="zope.ManageContent"
11        />
12  
13    <addMenuItem
14        class="book.messageboard.messageboard.MessageBoard"
15        title="Message Board"
16        description="A Message Board"
17        permission="zope.ManageContent"
18        view="AddMessageBoard.html"
19        />
20  
21    <editform
22        schema="book.messageboard.interfaces.IMessageBoard"
23        for="book.messageboard.interfaces.IMessageBoard"
24        label="Change Message Board"
25        name="edit.html"
26        permission="zope.ManageContent"
27        menu="zmi_views" title="Edit"
28        />
29  
30    <containerViews
31        for="book.messageboard.interfaces.IMessageBoard"
32        index="zope.View"
33        contents="zope.View"
34        add="zope.ManageContent"
35        />
36  
37    <addform
38        label="Add Message"
39        name="AddMessage.html"
40        schema="book.messageboard.interfaces.IMessage"
41        content_factory="book.messageboard.message.Message"
42        fields="title body"
43        permission="zope.ManageContent"
44        />
45  
46    <addMenuItem
47        class="book.messageboard.message.Message"
48        title="Message"
49        description="A Message"
50        permission="zope.ManageContent"
51        view="AddMessage.html"
52        />
53  
54    <editform
55        schema="book.messageboard.interfaces.IMessage"
56        for="book.messageboard.interfaces.IMessage"
57        label="Change Message"
58        fields="title body"
59        name="edit.html"
60        permission="zope.ManageContent"
61        menu="zmi_views" title="Edit"
62        />
63  
64    <containerViews
65        for="book.messageboard.interfaces.IMessage"
66        index="zope.View"
67        contents="zope.View"
68        add="zope.ManageContent"
69        />
70  
71  </configure>
  • Line 2: In this configuration file we do not use the zope, but the browser namespace, since we want to configure browser-specific functionality. Also note that browser is the default namespace, so that our directives do not need the namespace prefix. Namespaces for ZCML start commonly with http://namespaces.zope.org/ followed by the short name of the namespace, which is commonly used in this book to refer to namespaces.

    在这个配置文件我们不使用zope,, 因为我们想要配置browser包具体功能。 并且注意browser包是缺省的命名空间, 我们的指令不需要命名空间前缀。 http://namespaces.zope.org/是常常在这本书里提到命名空间。

  • Line 4-11: Register an auto-generated “Add” form for the Message Board.

为Message Board注册一个自动生成“Add” form。

Line 5: The label is a small text that is shown on top of the screen.

这个标签是显示在屏幕顶部的小文本

Line 6: The name of the view. The name is the string that is actually part of the URL.

这个视图的名称,这个字符串名称实际上是URL的一部分

Line 7: This defines the schema that will be used to generate the form. The fields of the schema will be used to provide all the necessary meta data to create meaningful form elements.

Schema定义将被用于产生表单,Schema中的字段将被用于提供所有为创建有意义的表单元素所必需的meta data

Line 8: The content factory is the class/factory used to create the new content component.

The content factory用于创建新的内容组件

Line 9: The fields are a list of field names that are displayed in the form. This allows you to create forms for a subset of fields in the schema and to change the order of the fields in the form.

The fields是被显示在表单里的字段名称列表,它允许你创建基于schema的字段子集的表单并且可以改变字段在表单的顺序。

Line 10: Specifies the permission required to be able to create and add the new content component.

指定能创建和新增内容组件的权限。

  • Line 13-19: After creating a view for adding the message board, we now have to register it with the add menu, which is done with the browser:addMenuItem directive. The title is used to display the item in the menu. The important attribute is the view, which must match the name of the add form.

当创建了一个新增message board的视图后,我们现在必须以browse:addMenuItem指令注册新增菜单。标题显示在菜单的项目上,重要的是视图必须与新增表单的名称相匹配

  • Line 21-28: Declaring an edit form is very similar to defining an add form and several of the options/attributes are the same. The main difference is that we do not need to specify a content factory, since the content component already exists.

申明一个编辑表单的工作与定义一个新增表单非常相似,最主要的不同之处在于我们不需要指定专门的content factory,因为相关的内容组件已经存在。

The for attribute specifies the interface for which type of component the edit form is for. All view directives (except the browser:addform) require the for attribute. If you would like to register a view for a specific implementation, you can also specify the class in the for attribute.

For属性为组件的编辑表单指定接口。所有的视图指令(除开browser:addform)都要求for属性。如果你想为一个特定的应用注册一个视图,你能为for属性指定类

We also commonly specify the menu for edit views directly in the directive using the menu and title attribute as seen on line 27. The zmi_views menu is the menu that creates the tabs on the default Web UI. It contains all views that are specific to the object.

在27行我们可以看见我们用菜单和标题属性直接为编辑视图指定菜单,zmi_views菜单是在缺省的web界面创建tabs。它包含指定对象的所有视图。

  • Line 30-35: The message board is a container and a quick way to register all necessary container-specific views is to use the browser:containerViews directive. Note though that this directive is not very flexible and you should later replace it by writing regular views.

Message board是容器并且将使用browser:containerViews指令以一个快的方式注册所有必要的容器视图,这个指令非常灵活并且稍后你可以写规则视图代替它。

  • Line 37-69: These are exactly the same directives over again, this time just for the IMessage interface.

这些确切地说是同样的指令, 不过是针对IMessage 接口

In order for the system to know about the view configuration, we need to reference the configuration file in messageboard/configure.zcml. To include the view configuration, add the following line:

为了能让系统知道关于视图的配置, 我们需要参考配置文件messageboard/configure.zcml。 也包括视图配置, 增加如下信息:

1 <include package=".browser" />

Even though the content components are registered now, nothing interesting will happen, because there exists only a programmatic way of adding and editing the new components. Thus we are going to define some very basic browser views to make the content components accessible via the browser-based user interface.

First create a package called browser (do not forget the init.py file) inside the messageboard package. Add a new configuration file, configure.zcml, inside browser and insert the following content:

1  <configure
2      xmlns="http://namespaces.zope.org/browser">
3  
4    <addform
5        label="Add Message Board"
6        name="AddMessageBoard.html"
7        schema="book.messageboard.interfaces.IMessageBoard"
8        content_factory="book.messageboard.messageboard.MessageBoard"
9        fields="description"
10        permission="zope.ManageContent"
11        />
12  
13    <addMenuItem
14        class="book.messageboard.messageboard.MessageBoard"
15        title="Message Board"
16        description="A Message Board"
17        permission="zope.ManageContent"
18        view="AddMessageBoard.html"
19        />
20  
21    <editform
22        schema="book.messageboard.interfaces.IMessageBoard"
23        for="book.messageboard.interfaces.IMessageBoard"
24        label="Change Message Board"
25        name="edit.html"
26        permission="zope.ManageContent"
27        menu="zmi_views" title="Edit"
28        />
29  
30    <containerViews
31        for="book.messageboard.interfaces.IMessageBoard"
32        index="zope.View"
33        contents="zope.View"
34        add="zope.ManageContent"
35        />
36  
37    <addform
38        label="Add Message"
39        name="AddMessage.html"
40        schema="book.messageboard.interfaces.IMessage"
41        content_factory="book.messageboard.message.Message"
42        fields="title body"
43        permission="zope.ManageContent"
44        />
45  
46    <addMenuItem
47        class="book.messageboard.message.Message"
48        title="Message"
49        description="A Message"
50        permission="zope.ManageContent"
51        view="AddMessage.html"
52        />
53  
54    <editform
55        schema="book.messageboard.interfaces.IMessage"
56        for="book.messageboard.interfaces.IMessage"
57        label="Change Message"
58        fields="title body"
59        name="edit.html"
60        permission="zope.ManageContent"
61        menu="zmi_views" title="Edit"
62        />
63  
64    <containerViews
65        for="book.messageboard.interfaces.IMessage"
66        index="zope.View"
67        contents="zope.View"
68        add="zope.ManageContent"
69        />
70  
71  </configure>
  • Line 2: In this configuration file we do not use the zope, but the browser namespace, since we want to configure browser-specific functionality. Also note that browser is the default namespace, so that our directives do not need the namespace prefix. Namespaces for ZCML start commonly with http://namespaces.zope.org/ followed by the short name of the namespace, which is commonly used in this book to refer to namespaces.

    在这个配置文件我们不使用zope,, 因为我们想要配置浏览具体功能。并且注意浏览器是缺省的命名空间, 我们的指令不需要命名空间前缀。 http://namespaces.zope.org/是常常在这本书里提到命名空间。

  • Line 4-11: Register an auto-generated “Add” form for the Message Board.

为Message Board注册一个自动生成“Add” form。

Line 5: The label is a small text that is shown on top of the screen.

这个标签是显示在屏幕顶部的小文本

Line 6: The name of the view. The name is the string that is actually part of the URL.

这个视图的名称,这个字符串名称实际上是URL的一部分

Line 7: This defines the schema that will be used to generate the form. The fields of the schema will be used to provide all the necessary meta data to create meaningful form elements.

Schema定义将被用于产生表单,Schema中的字段将被用于提供所有为创建有意义的表单元素所必需的meta data

Line 8: The content factory is the class/factory used to create the new content component.

The content factory用于创建新的内容组件

Line 9: The fields are a list of field names that are displayed in the form. This allows you to create forms for a subset of fields in the schema and to change the order of the fields in the form.

The fields是被显示在表单里的字段名称列表,它允许你创建基于schema的字段子集的表单并且可以改变字段在表单的顺序。

Line 10: Specifies the permission required to be able to create and add the new content component.

指定能创建和新增内容组件的权限。

  • Line 13-19: After creating a view for adding the message board, we now have to register it with the add menu, which is done with the browser:addMenuItem directive. The title is used to display the item in the menu. The important attribute is the view, which must match the name of the add form.

当创建了一个新增message board的视图后,我们现在必须以browse:addMenuItem指令注册新增菜单。标题显示在菜单的项目上,重要的是视图必须与新增表单的名称相匹配

  • Line 21-28: Declaring an edit form is very similar to defining an add form and several of the options/attributes are the same. The main difference is that we do not need to specify a content factory, since the content component already exists.

申明一个编辑表单的工作与定义一个新增表单非常相似,最主要的不同之处在于我们不需要指定专门的content factory,因为相关的内容组件已经存在。

The for attribute specifies the interface for which type of component the edit form is for. All view directives (except the browser:addform) require the for attribute. If you would like to register a view for a specific implementation, you can also specify the class in the for attribute.

For属性为组件的编辑表单指定接口。所有的视图指令(除开browser:addform)都要求for属性。如果你想为一个特定的应用注册一个视图,你能为for属性指定类

We also commonly specify the menu for edit views directly in the directive using the menu and title attribute as seen on line 27. The zmi_views menu is the menu that creates the tabs on the default Web UI. It contains all views that are specific to the object.

在27行我们可以看见我们用菜单和标题属性直接为编辑视图指定菜单,zmi_views菜单是在缺省的web界面创建tabs。它包含指定对象的所有视图。

  • Line 30-35: The message board is a container and a quick way to register all necessary container-specific views is to use the browser:containerViews directive. Note though that this directive is not very flexible and you should later replace it by writing regular views.

Message board是容器并且将使用browser:containerViews指令以一个快的方式注册所有必要的容器视图,这个指令非常灵活并且稍后你可以写规则视图代替它。

  • Line 37-69: These are exactly the same directives over again, this time just for the IMessage interface.

这些确切地说是同样的指令, 不过是针对IMessage 接口

In order for the system to know about the view configuration, we need to reference the configuration file in messageboard/configure.zcml. To include the view configuration, add the following line:

为了能让系统知道关于视图的配置, 我们需要参考配置文件messageboard/configure.zcml。也包括视图配置, 增加如下信息:

1  <include package=".browser" />