Using chtml as a CGI Postprocessor

If you wish to use chtml markup in your CGI programs, but cannot make use of template files, you'll need to save your output to a temporary file and then have chtml postprocess that file. The postprocessor will output all the necessary CGI headers, so you just need to use exec to call it.

The chtml postprocessor is available at /www/lib/chtml-postproc, and takes a single argument of a file to run through chtml. If you use the filename of - then the postprocessor will read the contents of the file from standard input (but note in some cases it's more efficient to write to a temporary file to avoid the extra process required to write to standard input). Note you must not pass any HTTP headers; they will be treated as HTML.

You can give optional arguments, also:

Remove temporary file after postprocessing is finished
-h header
Add header to the headers emitted by the postprocessor. Make sure that header is quoted so it appears as one argument on the command line. Multiple -h arguments may be specified.
-d dir
Force chtml to process as if the file being filtered is in dir.

The chtml postprocessor will show a short error message if it is incorrectly invoked, and the HTML generated by your CGI will not be displayed, nor the original file removed.

chtml Postprocessing Example

As an example this shell CGI (source and raw output) will call the chtml postprocessor after creating a temporary file. Another version (with source and raw output) of the example writes to the postprocessor using standard input. Note that the file has many chtml directives in it. In addition, this script will set extra headers which, in this case, will be ignored by the browser.

Other things to note about the example:

Using Postprocessing with chtml Datestamps

Since chtml date statements use the modify time of the html file for datestamps, you can control what date chtml thinks the original file was changed by setting the modify time on the file.