[ZPT] Validation of a ZPT on the file system

Hong Yuan obichina at 21cn.com
Thu Oct 7 11:14:02 EDT 2004


The following is an exerpt on this topic from the great book The 
Definitive Guide to Plone by Andy McKay, available at
http://cvs.sourceforge.net/viewcvs.py/plone-docs/PloneBook/en/ch6.rst?rev=1.2&view=markup

Conducting Syntax Checks
........................

When you edit a page template in the ZMI, Zope performs a syntax check on the document for things such as invalid tags. If a tag is invalid, an error will be shown on the template while you're editing it through the Web. If, like me (and as I demonstrate in Chapter 7), you write most of your page templates on the file system, then a simple syntax check for a page template is really useful. Listing 6-6 is a Python script that resides on your file system and runs independently from Zope.

To run this, you must have a Python interpreter, and the Python module *PageTemplate* must be importable. To make *PageTemplate* importable to your Python interpreter, you must add the *Products* directory of your Zope installation to your Python path. You have several ways to do this (covered in Appendix B).

Listing 6-6. Error Checking Page Templates

::

 #!/usr/bin/python
 from Products.PageTemplates.PageTemplate import PageTemplate
 import sys
  
 def test(file):
     raw_data = open(file, 'r').read()
     pt = PageTemplate()
     pt.write(raw_data)
     if pt._v_errors:
         print "*** Error in:", file
         for error in pt._v_errors[1:]:
             print error
  
 if __name__=='__main__':
     if len(sys.argv) < 2:
         print "python check.py file [files...]"
         sys.exit(1)
     else:
         for arg in sys.argv[1:]:
             test(arg)

For every file passed through to the script, the ZMI will compile the page template and see if there are any TAL errors. Taking the *bad_template.pt* file from Listing 6-4, you'll get an error:

::

 $ python zpt.py /tmp/bad_template.pt
 *** Error in: /tmp/bad_template.pt
 TAL.TALDefs.TALError: bad TAL attribute: 'contents', at line 10, column 39

In this case, it has picked up on the incorrect spelling of *tal:content* as *tal:contents*. This error is something HTML Tidy doesn't catch. Unfortunately, the processing stops at the first syntax error. If there are multiple errors, only the first is picked up, meaning sometimes you have to check the syntax several times.


Petter Kvalvik wrote:

>Hei!
>
>Can someone tell how to validate the tales expressions in a ZPT outside of 
>Zope (on the file system)?
>
>Petter
>
>
>
>
>_______________________________________________
>ZPT mailing list
>ZPT at zope.org
>http://mail.zope.org/mailman/listinfo/zpt
>
>  
>



More information about the ZPT mailing list