Code Evaluation

part of a tutorial on the Dynamic Publishing System by Karl Goldstein

This lesson illustrates how the template specification can provide a structured framework for procedural code that generates dynamic data for a page.

Create the specification file

Create a file named contest.data in a test directory of your server and enter the following specification:

In this case, the first data source creates a list of ns_set data structures to hold the contestant data. The code specified by the second data source uses this data to pick a contest winner.

Note that any type of Tcl code may be executed by a data source, provided that the last command in the code returns the data structure stipulated by the data source's structure property. Data sources are processed in the order in which they are listed in the specification file, so the code for a data source may safely refer to variables created by other data sources that are higher in the file.

The inclusion of arbitrary amounts of Tcl code in the specification file itself should be only be done after considering the implications. For complex pages it is clearly convenient to have procedural and declarative data mixed in a single file. However, if a particular data source requires more than a few lines of code to create, then you should consider the possibly that at least some of the code will be useful elsewhere as well, and thus is better suited to encapsulation in a proc that resides in the Tcl private library.

The encapsulation of as much code as possible in procs is also desirable from a performance standpoint. Code in an eval data source must be evaulated every time a page is requested, as opposed to a proc which must only be parsed once and then is held resident in shared memory.

View the data dictionary

Create the template

The template for this lesson does not introduce anything new:

View results

Tutorial Contents
karlg@arsdigita.com