Plone browser view template




















It pertains to Plone 2. They support the "display" menu with per-instance selectable views, including the ability to select a default-page for folders via the GUI. These use CMF 1. The "display" menu is not used. The previous behavior of Plone still holds.

This is used to look up the default-page e. The rules are slightly simplified :. If a folder contains items with any of these magic ids, the first one found will be used as a default-page. If the object has a 'folderlisting' action, use this.

This is a funny fallback which is necessary for old-style folders to work see below. In CMF 1. The fallback on the 'folderlisting' action in PloneTool. If you think that sounds messy, you're right. With CMF 1. Enter CMF 1. CMF 1. An instance of the class is also available in the template, under the name view.

As a rule of thumb, try to keep the page template free from complex expressions. Python code is much easier to debug and test. Here is an example of a view class which registers a view and provides some helper methods and attributes. It also prepares some variables for the view in the update method, which is called just before the view is rendered. This class could go in any Python module. For generic views, browser. The automatically associated template is shown below.

If the Python module was browser. See the Dexterity Developer Manual for more information about how to register default and alternative views for content items.

Sometimes, we do not need a template. In this case, we can override the render method of the grok. View base class to return a string, which is then returned to the browser as the response body. Below is an example that builds a CSV file of the recipients of the message representation of the context.

By setting appropriate response headers, this view ensures that the browser will attempt to download that generated file, rather than display a plain text response. Dexterity uses the powerful z3c. Sometimes, though, we just want a simple HTML form and a bit of logic to process request parameters. One common way to implement this is with a view that defines a form, which submits back to itself. The form is processed in the update method of the view class. The example below shows a simple form which allows users to subscribe to a content object with an email address.

The list of subscribers is stored in an annotation as described earlier in this manual. Here is the form template. Assuming the view was put in a module subscription. The most important thing in the add-on is that your registers itself to grok which allows Plone to scan all Python files for grok directives and furter automatically pick up your views as opposite using old Zope 3 method where you manually register views by typing them in to ZCML in ZCML. First make sure the file configure.

These lines are needed only once, in the root configuration ZCML file:. Either you need to have five. If you didn't add it in this point and run buildout again to download and install five. Add the file yourcompany. The grok. In the first case, the incoming self. Alternatively, you could use the content interface docs to make the view available only for certain content types. Example grok.

Then create a page template for your view. Create yourcompany. Now when you restart to Plone or use auto-restart add-on the view should be available through your browser. After enabled, grok will scan all Python files for available files, so it doesn't matter what.

You can also use the notation at the front of the view name to make sure that you are looking up a view , and not a content item that happens to have the same id as a view:. Use grok. Use available permissions in Zope 3 style strings. If this code fails and an exception is raised, the zope. Additionally, view class may be instantiated in other places than where you intended to render the view.

For example, plone. Usually, the configuration file, where the registration done, is called yourapp. You need to declare the browser namespace in your configure. Below is a sample code snippet which allows you to override an already constructed ViewPageTemplateFile with a chosen file at run-time:.

You can bind several templates to one view and render them individually. This is very useful for reusable templating, or when you subclass your functional views. Example using five. You should never try to put your code there. Instead, use helper method or lazy construction design pattern if you need to set-up view variables. This information is set after the constructor have been run. Views can be registered against a specific layer interface.

This means that views are only looked up if the specified layer is in use. Since one Zope application server can contain multiple Plone sites, layers are used to determine which Python code is in effect for a given Plone site. There are two different classes that share the same ViewPageTemplateFile name.

Most of the code in this section is copied from a tutorial by Martin Aspeli on slideshare. The main change is that, at least for Plone 4, the interface should subclass plone. IDefaultPloneLayer instead of zope. In this example we override the register form from the plone. Create an interface in interfaces. Not all views need to return HTML output, or output at all.

Views can be used as helpers in the code to provide APIs to objects. Since views can be overridden using layers, a view is a natural plug-in point which an add-on product can customize or override in a conflict-free manner. View methods are exposed to page templates and such, so you can also call view methods directly from a page template, not only from Python code. Often, the point of using helper views is that you can have reusable functionality which can be plugged in as one-line code around the system.



0コメント

  • 1000 / 1000