CGI Framework
Compiling and using staticgen

Basics

Docs

Components

Templates

Tools

Demos


SourceForge.net Logo

Brasil
   

Compiling and using staticgen

After installing CGI Framework components, the next logical step is compile and test the two basic tools supplied: staticgen and TestTool.

Both tools are standalone programs that use all the components provided by CGI Framework package. Compiling and testing them are the shortest way to understand the PageBuilder and TagInterpreter components.

Playing with staticgen

staticgen is a command line tool. It is composed by just 2 units:

staticgen.dpr - the main program
The main program is very simple. It just create a data module (defined on the second unit) and call routines there.
dmmain.pas - a data module
A data module was added to the project because it highly simplify the use of components in design time while keeping our application a console one, without the need of Forms or other "visual" units.

The main program does not deserve comments. The data module "looks" like that:

DtmMain.pas view

Inside the data module we have a IBDatabase and a IBTransaction, to provide access (oops) to our data. However its not mandatory to have a database to use staticgen. IBDatabase and IBTransaction are used in an almost standard way. And maybe you will need only to change the password and character set parameters on the IBDatabase component.

staticgen hard-codes the database password inside the program. It's one of the worst security decisions you can make. If you are using a production server for your tests, please do not redistribute staticgen with your password coded in.

staticgen also includes a TPageBuilder, a TPBTagInterpreter and a TDBIbxTagInterpreter. Selecting PbdMain, and double-clicking its "TagInterpreters" property we have:

PbdMain's TagInterpreters

Above figure show how a PageBuilder is binded to one or more TagInterpreters. In that case both TagInterpreters are linked to our sole PageBuilder. PBTagInt is a instance of TPBTagInterpreter and process tags on the "pb:" namespace. DBIbxTagInt is a instance of TDBIbxTagInterpreter and process "db:" tags.

staticgen code is deceptively simple. More than a half of the code simple deals with command line options. By the way: the program usage is:

staticgen sourceTemplate [ targetFile ] [ -t:templateDir ] [ -d:interbaseDatabase ]

where:

"[" and "]" indicates optional parts, as usual, and must not be typed.
sourceTemplate
is a file name. staticgen will load that file and use it to build the target file. Any name may be used for the source template. You must however avoid the use of .html extension since it is used by default on the targetFile when it is not supplied.
targetFile
is a file name. The generated content goes in that file. Prior file content is lost. When not supplied, this name is generated using the sourceTemplate and changing (or adding) the extension to .html.
-t:templateDir
that option inform the directory to be used when the main template file "pb:include" or "pb:encapsule" another template. If not supplied, its assumed to be equal to sourceTemplate dir.
-d:interbaseDatabase
allows the database server and path to be informed in run time. It follows the standard syntax for specifying Interbase databases

The only "useful" code is the data module is the "Process" method:

procedure TDtmMain.Process;
  var result: Integer;
      content: String;
  begin
    DeleteFile ( targetFile );
    result := FileCreate ( targetFile );
    try
        content := PbdMain.Content;
        FileWrite ( result, content[1], Length ( content ) );
    finally
        FileClose ( result );
    end;
  end;

And inside that routine, the only "useful" line of code is:

content := PbdMain.Content;

It generate the HTML page and stores the whole code in "content" to be subsequently stored in the target file. All the "real" code goes inside the components dropped in the data module. Is something so simple of any use? Yes for sure. All the pages on that site where create with staticgen. It guaranties the uniform looking of that site and makes my live a lot easier.

 


Copyleft (2002) Ivan C. Cruz
Site contents may be reproduced in all or in part since you provide a clear statement citing that site as your source.

That site was built and tested with Mozilla. If some components looks misalligned or not properly rendered, it may be your browser fault. In that case, please, consider an upgrade.