Previewing Page Templates
Intro
It is always nice to see the effects of changes immediately and without much effort. Page Templates within Plone usually need a special context to render nicely, for example a content object.
This how to helps you to set up your favorite HTML Editor to show page templates within context as preview. I use HTML Kit for explanation but I bet you can do that with your Editor as well as long as its preview can be set up to use a web server.
The principle here is to set up a script which will automatically select an appropriate context for your template when a preview URL is launched from within a HTML editor.
Steps
- Add the following script to
portal_skins/customand name it 'preview_template':## Script (Python) "preview_template" ##bind container=container ##bind context=context ##bind namespace= ##bind script=script ##bind subpath=traverse_subpath ##parameters=catalog_sequence ##title= ## request = container.REQUEST RESPONSE = request.RESPONSE # dict 'preview' contains the template names as keys # and the URL to the preview context as values. preview = {'member_search_form.pt' : '/Plone/Members/index_html', 'index_html.pt' : '/Plone'} passed_template = request.traverse_subpath[-1] RESPONSE.redirect(preview[passed_template]) - Configure the preview to use a web server and point it to the above script.
In HTML Kit you find the settings under
Edit->Preferences. Open thePreview-Tab, hit theEdit preview rolesButton and enter a preview rule underPath to server mappings. Here you map the path to templates with a server address for the preview. In other editors, look for an option which will let you set the URL to prepend to the name of your script when launching a preview. You must set this to thepreview_templatescript above, so that this can select an appropriate context and redirect your browser to it.The following values would show the standard Plone templates from a Plone site on localhost:
File Path: c:\Plone\data\Products\CMFPlone\skins Server Address: http://localhost:8080/Plone/preview_template/
- Add page templates and their corresponding preview contexts
to the preview dictionary as shown with
member_search_form.pt. - You may want to add content objects for testing to your Plone Site. I recommend creating a dedicated folder for that purpose as you can easily ex- and import your test objects to other instances later on.
Open Questions
I do not yet know how to test search result pages. Comments are welcome.
Ideas
This is just a quick and dirty solution. It works well for me but it could quite easily be enhanced. Here are some ideas:
- Replace the dictionary by properties.
- Catch calls to page_templates that are not yet registered and display a form to add the context information.
- Create a tool that stores the preview infos in objects, handles muliple views for one page template, creates a preview-walk-trough, restricts permissions, etc. etc.
