Warning

This document hasn't been checked for compatibility with current versions of Plone. Use at your own risk.

Previewing Page Templates

by Alan Runyan last modified Dec 30, 2008 03:01 PM
Shows how to make it possible to quickly preview page templates when editing with an offline editor.

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

  1. Add the following script to portal_skins/custom and 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])
    
  2. 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 the Preview-Tab, hit the Edit preview roles Button and enter a preview rule under Path 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 the preview_template script 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/
    
  3. Add page templates and their corresponding preview contexts to the preview dictionary as shown with member_search_form.pt.
  4. 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:

  1. Replace the dictionary by properties.
  2. Catch calls to page_templates that are not yet registered and display a form to add the context information.
  3. 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.

Contribute

Something wrong or out of date? Anybody can edit or create a new article in the knowledge base. Simply create an account on this site, log in, and click the Edit button to contribute.