Skip to content

Commit 31ba95e

Browse files
committed
Add simple first portlets section.
1 parent 612ffcd commit 31ba95e

File tree

1 file changed

+163
-1
lines changed

1 file changed

+163
-1
lines changed

docs/classic-ui/portlets.md

Lines changed: 163 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
myst:
33
html_meta:
4-
"description": ""
4+
"description": "Basic information about portlets in classic UI"
55
"property=og:description": ""
66
"property=og:title": ""
77
"keywords": ""
@@ -11,3 +11,165 @@ myst:
1111

1212
# Portlets
1313

14+
## What is a Portlet?
15+
16+
In Plone, a portlet is a small, modular piece of content that can be displayed in a specific area of a web page. Portlets are typically used to display information that is relevant to the current context, such as the latest news, upcoming events, or a list of related documents.
17+
18+
Portlets are highly customizable and can be used to display a wide variety of information. They can be added, removed, or rearranged on a web page by users with the appropriate permissions, allowing for a high degree of flexibility in the layout and content of a Plone site.
19+
20+
Plone comes with a number of built-in portlets, such as the news portlet, the events portlet, and the login portlet. In addition, developers can create custom portlets to display specific types of information or to provide specific functionality.
21+
22+
## Adding a portlet to a page
23+
24+
As a user, you can add a portlet to a web page in a Plone site by following these steps:
25+
26+
1. Navigate to the web page where you want to add the portlet.
27+
28+
2. Click on the "Manage portlets" link in the toolbar of the page and select the region on the page to mofify.
29+
This will open the "Manage portlets" screen.
30+
31+
3. In the "Add portlets" menu, select the portlet that you want to add and click the "Add" button.
32+
This will add the portlet to the list of "Portlets assigned here" on the screen.
33+
34+
4. Click the "Save" button to save your changes and add the portlet to the web page.
35+
Note that you must have the appropriate permissions in order to add portlets to a web page. If you do not see the "Manage portlets" link, you may need to contact the site administrator to request access.
36+
37+
5. Use the "Up" and "Down" arrows in the "Assigned portlets" section to change the order in which the portlets will be displayed on the web page.
38+
39+
40+
## Writing a custom Portlet
41+
42+
To create a portlet, you will need to write a Python class that defines the portlet and its behavior.
43+
This class should subclass the Portlet class from the `plone.portlets` package.
44+
45+
Here is an example of a very simple portlet class ``my_portlet.py``:
46+
47+
```python
48+
from plone.app.portlets.portlets import base
49+
from plone.portlets.interfaces import IPortletDataProvider
50+
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
51+
from zope import schema
52+
from zope.interface import implementer
53+
54+
55+
class IExamplePortlet(IPortletDataProvider):
56+
"""A portlet that displays a greeting message."""
57+
58+
message = schema.TextLine(
59+
title="Greeting message",
60+
description="The message to display in the portlet.",
61+
required=True,
62+
)
63+
64+
65+
@implementer(IExamplePortlet)
66+
class Assignment(base.Assignment):
67+
"""Portlet assignment."""
68+
69+
def __init__(self, message="Hello World!"):
70+
self.message = message
71+
72+
@property
73+
def title(self):
74+
"""This property is used to give the title of the portlet in the
75+
"manage portlets" screen. Heres, we use the message as a part of the title.
76+
"""
77+
return f"Greeting: {self.message}"
78+
79+
80+
class Renderer(base.Renderer):
81+
"""Portlet renderer."""
82+
83+
# Define the page template that will be used to render the portlet
84+
template = ViewPageTemplateFile("my_portlet.pt")
85+
86+
def message(self):
87+
"""This method is called by the page template to render the portlet."""
88+
return self.data.message
89+
90+
def render(self):
91+
"""This method is called whenever the portlet is rendered."""
92+
return self.template()
93+
94+
95+
class AddForm(base.AddForm):
96+
"""Portlet add form."""
97+
98+
schema = IExamplePortlet
99+
label = "Add Greeting Portlet"
100+
description = "This portlet displays a greeting."
101+
102+
def create(self, data):
103+
return Assignment(**data)
104+
105+
106+
class EditForm(base.EditForm):
107+
"""Portlet edit form."""
108+
109+
schema = IExamplePortlet
110+
label = "Edit Greeting Portlet"
111+
description = "This portlet displays a greeting."
112+
113+
```
114+
115+
Here is the example for a simple page template ``my_portlet.pt``:
116+
117+
```HTML
118+
<html
119+
xmlns="http://www.w3.org/1999/xhtml"
120+
xmlns:tal="http://xml.zope.org/namespaces/tal"
121+
xmlns:i18n="http://xml.zope.org/namespaces/i18n"
122+
tal:omit-tag=""
123+
>
124+
<div class="card portlet" i18n:domain="my-example">
125+
<div class="card-header">Greeter</div>
126+
127+
<div class="card-body">
128+
<p>${python:view.message()}</p>
129+
</div>
130+
131+
<div class="card-footer portletFooter"></div>
132+
</div>
133+
</html>
134+
```
135+
136+
This portlet class defines a portlet with the title "My Portlet" and the name "my-portlet". When the portlet is rendered, it will use the template file `my_portlet.pt` to generate its HTML output that displays a greeting message..
137+
138+
The message is set when the portlet is added, and can be edited in the portlet's edit form.
139+
140+
To register this portlet with Plone, you will need to create a configure.zcml file that tells Plone about the portlet, and - after a restart - you can add it to a Plone page using the "manage portlets" screen. Here is an example configure.zcml file that registers the MyPortlet class defined above:
141+
142+
```XML
143+
<configure xmlns="http://namespaces.zope.org/zope"
144+
xmlns:browser="http://namespaces.zope.org/browser"
145+
xmlns:plone="http://namespaces.plone.org/plone"
146+
i18n_domain="example.portlet">
147+
148+
<plone:portlet
149+
title="Example Portlet"
150+
description="A portlet that displays a greeting message"
151+
addview="example.portlet.add"
152+
editview="example.portlet.edit"
153+
assignment="example.portlet.Assignment"
154+
renderer="example.portlet.Renderer"
155+
schema="example.portlet.IExamplePortlet"
156+
/>
157+
158+
</configure>
159+
```
160+
161+
This file registers a portlet with the following properties:
162+
163+
- *Title:* Example" Portlet"
164+
- *Description:* "A portlet that displays a greeting message"
165+
- *Add form:* "example.portlet.add"
166+
- *Edit form:* "example.portlet.edit"
167+
- *Assignment:* "example.portlet.Assignment"
168+
- *Renderer:* "example.portlet.Renderer"
169+
- *Schema:* "example.portlet.IExamplePortlet"
170+
171+
These values should match the corresponding classes and interfaces defined in the example code from the previous answer.
172+
173+
This file registers the MyPortlet class as a portlet with Plone. It also specifies the portlet's name, title, description, and category.
174+
175+
To get more example you can also look at the source code of the Plone core package `plone.app.porlets`, or of other Plone add-ons that include portlets for examples of how to write and register portlets.

0 commit comments

Comments
 (0)