Plone, creating a viewlet from a form
I have a snippet of HTML content tha开发者_Python百科t I constantly have to update on my homepage. I ideally want to go to a page, fill in a form, and have that form data populate the HTML snippet (viewlet?)
Open to any suggestions. (creating a custom content type, using mysql db to populate it. anything!)
You can do this elegantly with a marker interface and archetypes.schemaextender. Assuming your homepage is set up like the front-page on a fresh Plone site - a Document made the default view of the Plone site - add to your custom product's interfaces.py
:
class IHomepage(Interface):
"""Marker interface flagging a document as the homepage.
"""
And to configure.zcml
:
<class class="Products.ATContentTypes.interfaces.document.IATDocument">
<allow interface=".interfaces.IHomepage" />
</class>
In the ZMI, on the Interfaces tab for the front-page, set IHomepage
as a provided interface.
Now you can register a schemaextender that applies only to your front-page object.
Create a new file schemaextender.py
.
from zope.component import adapts
from zope.interface import implements
from Products.Archetypes import atapi
from archetypes.schemaextender.field import ExtensionField
from archetypes.schemaextender.interfaces import ISchemaExtender
from interfaces import IHomepage
class HomepageBlurbTextField(ExtensionField, atapi.TextField):
"""Extra text for the homepage.
"""
class HomepageExtender(object):
adapts(IHomepage)
implements(ISchemaExtender)
fields = [
HomepageBlurbTextField('blurb',
required=True,
searchable=True,
storage=atapi.AnnotationStorage(),
validators=('isTidyHtmlWithCleanup',),
default_output_type='text/x-html-safe',
widget=atapi.RichWidget(
label=u"Blurb",
description=u"Make the site sound cool.",
rows=25,
allow_file_upload=False),
),
]
def __init__(self, context):
self.context = context
def getFields(self):
return self.fields
Add to configure.zcml
:
<adapter factory=".schemaextender.HomepageExtender" />
(This is a simple example; see archetypes.schemaextender's docs for more advanced features like field reordering, registering only to a specific browser layer, etc.)
Restart the site, edit the front-page and - hey presto - a new field for your blurb.
Now all you have to do is display it. You can do what you like here, just remember to register for your IHomepage
interface. Let's say you wanted the blurb to appear in the page's header:
In browser/viewlets.py
:
from Acquisition import aq_inner
from plone.app.layout.viewlets import common
class HomepageHeaderViewlet(common.ViewletBase):
@property
def blurb(self):
context = aq_inner(self.context)
return context.getField('blurb').get(context)
In browser/homepageheader.pt
:
<div id="homepage-blurb" tal:content="structure view/blurb|nothing" />
In browser/configure.zcml
:
<browser:viewlet
name="example.homepageheader"
for="..interfaces.IHomepage"
manager="plone.app.layout.viewlets.interfaces.IPortalTop"
class=".viewlets.HomepageHeaderViewlet"
template="homepageheader.pt"
permission="zope.Public"
/>
It depends on your usecase but often you can get by without coding using something like collective.portletpage with several static text portlets to create the page you want. With plone.app.theming you have the ability to rearrage the html for your needs.
for example our new homepage http://www.pretaweb.com was done using plone.app.theming, collective.portletpage and Products.ContentWellPortlets.
Products.Collage is another alternative to portletpage we've also used.
精彩评论