Pre and Post Parse Scripts

Pre and Post Parse Scripts


The pre-parse and post-parse scripts exist as two 'special' WebDNA files in the WebCatalog engine folder, or WebDNA Sandbox system folder. These are named, as you might guess, 'PreParseScript' and 'PostParseScript'. When enabled, any WebDNA that is contained in those scripts is process before, or after, every WebDNA page request.


The Pre-Parse Script


The pre-parse script is an ideal place to put 'global' function or variable definitions. It is also a great place to put HTML comment blocks that should appear at the top of WebDNA page results. When combined with a WebDNA Sandbox, this level of functionality can be considered as 'application' level functionality, as each WebDNA Sandbox will use a separate set of pre-parse and post-parse scripts.

Here is an example of a pre-parse script you could use to initialize a series of function definitions. In this example the function definitions exist as separate include files in a sub-folder called, 'FunctionDefs'.

Contents of the 'PreParseScript' file...

[!] An example Pre-Parse Script that loads function definitions [/!]
[!]
Include My Function Definitions.
[/!]

[text]results=
[listfiles path=^FunctionDefs]
[showif [isfile]=T]
[include ^FunctionDefs/[filename]]
[/showif]
[/listfiles]
[/text]

If this example pre-parse script was 'live', then every WebDNA page request would have access to any function definitions that were processed as a result of the script execution.


The Post-Parse Script


The post-parse script is executed at the end of every WebDNA page request. This enables the WebDNA programmer to 'wrap' WebDNA code around the results of every WebDNA template that is processed. So this is an ideal place to insert code that generates 'global' (or 'application' when inside WebDNA Sandbox) header or footer messages. Or as a place to put server, or Sandbox, 'logging' code. It can even be used to insert WebDNA code that will 'strip' or modify the page results, perhaps to remove unwanted white-space from the HTML before it is return to the client.

There is a new, very important, WebDNA tag that you will almost always use inside the post-parse script. This tag is: [WEBDNA_TEMPLATE_RESULTS]. This tag represents the results of the requested WebDNA template. This tag allows you to 'wrap' you own WebDNA code around the page results. So, if you enable the post-parse script feature, you will need to include the [WEBDNA_TEMPLATE_RESULTS] tag, somewhere in the WebDNA code.

Here is an example of a post-parse script you could use to insert an HTML footer message at the bottom of every WebDNA page request.

Contents of the 'PostParseScript' file...


[!] Example code to place a 'footer' message at the bottom of every page request [/!]

[showif [WEBDNA_TEMPLATE_RESULTS]^

 

How to use the scripts

Both the Pre-Parse and Post-Parse scripts are disabled by default. Even if the scripts are 'enabled', the stock scripts contain little to no 'active' WebDNA code. The shipping version of WebDNA 5.0 may include pre-defined functions that will be loaded by the pre-parse script.

You enable the scripts via the WebDNA admin, preferences page. The scripts can be independently enabled or disabled from that page. There is also a 'link', for each script, that will lead to a page where the script can be edited and saved.

IMPORTANT:

You should have ftp access to the folder containing the scripts, before trying to enable and edit them. This is in case you insert bad HTML or WebDNA code that renders all pages unreadable (which would render the admin pages unreadable as well). Ftp access to the script files will ensure that you have a way to 'correct' fatal errors in the script files. For WebDNA Sandbox sites, malformed scripts would only effect pages served from that Sandbox.

To enable, disable, view, or edit the pre_parse or post-parse scripts, select the 'Prefs/...Admin...' menu item (in the lab 'source' view). When the admin page is displayed, clink on the 'Preferences' link in the left pane. You will see the pre-parse and post-parse script options at the bottom of the 'General' section.

User Contributed Notes
ParseScripts  

Bill DeVaul  
05/23/2003 at 08:17:22
Does anyone have an example of removing the whitespace in a post-script?  I take it that this would eliminate the [!][/!] line breaks that make the HTML output look insane.  

Copyright © 2008 WebDNA Software Corporation, Inc.