If you use C to write CGI programs, the general code is like this:
# Include <stdio. h>
Main ()
{
Printf ("Contenttype: text/html/n ″);
Printf ("Printf ("Printf ("<body> <br>/n ″);
Printf ("Printf ("</body>/n ″);
Printf ("Fflush (stdout );
}
The above code writes html statements to the c program, which is ugly. In the future, we need to modify the html. Not only can the artist do it, but the programmer has to rewrite the program and recompile it.
This html template solves this problem.
How to Use
How to use it is very simple. For example, to display a Color table, the first line is the title Color, and the following lines Generate several colors. The task of html editing is to provide the following html file.
Please refer to the following link for more information:
<HTML>
<HEAD>
<TITLE> </TITLE>
</HEAD>
<BODY>
<Table>
<Tr>
<Td> {ColumnName} </td>
</Tr>
<! -- BeginBlockName -->
<Tr>
<Td> {ColumnValue} </td>
</Tr>
<! -- EndBlockName -->
</Table>
</Body>
</Html>
The programmer must write the following code:
================== Test. cgi ==========================
Htloadfile ("test.htm ");
Htsetvar ("columnname", "color ");
Htsetvar ("columnvalue", "Red ");
Htparse ("blockname", 0 );
Htsetvar ("columnvalue", "green ");
Htparse ("blockname", 0 );
Htsetvar ("columnvalue", "blue ");
Htparse ("blockname", 0 );
Htfinish ();
The output of the program is as follows:
=================== Output ========================
<HTML>
<HEAD>
<TITLE> </TITLE>
</HEAD>
<BODY>
<Table>
<Tr>
<Td> Color </td>
</Tr>
<Tr>
<Td> Red </td>
</Tr>
<Tr>
<Td> Green </td>
</Tr>
<Tr>
<Td> Blue </td>
</Tr>
</Table>
</Body>
</Html>
Keywords
Variable
The syntax for variable definition is
{Variable}
Use {} to enclose the variable name.
Note: The variables are case-sensitive. The name must not exceed 32 characters long and contain spaces.
Valid variable definitions include:
{Name}, {My name}, {My name}, {$ my name ##}, and so on. Note that {My name} and {My name} are two different variable names, because there are spaces before and after the second variable.
Invalid variable definition:
{Na {me}, {Nam} e}, {n <! -- Ame}
The recommended naming format is
{Name}, {name}, {myname}, and so on. Be case sensitive. Try not to have spaces or other special characters.
Variable usage
For example, the variable {columnvalue} exists in the preceding example. The value of this variable is null at first, and the value of this variable is red when htsetvar ("columnvalue", "Red") is called, when output, the variable is replaced with red.
Then htsetvar ("columnvalue", "green"); change the value of this variable to green again, and replace it with green in the output.
Note: The variable value does not limit the length.
Block
The block definition syntax is
<! -- Beginblockname -->
.........
<! -- Endblockname -->
BlockName is the block name, and the first character of Begin and End must be in upper case. We recommend that you do not have spaces or other special characters.
Note: Unlike variables, a block can only be defined once in the template.
Block usage
The template library regards html files as a top-level unknown block, which can be nested. Each block is only defined in the form in html. It is instantiated only when HTParse is called.
For example
<! -- BeginBlockName -->
<Tr>
<Td> {ColumnValue} </td>
</Tr>
<! -- EndBlockName -->
This block definition. If the HTParse ("BlockName", 0) function has never been called, the block definition output will be blank.
The following call
HTSetVar ("ColumnValue", "Red ");
HTParse ("BlockName", 0 );
The two functions. The second parameter of HTParse indicates that if the block is output multiple times, 0 indicates that the block is called first and output first. 1 indicates that the block is called first and then output.
The block will be instantiated
<Tr>
<Td> Red </td>
</Tr>
Call again
HTSetVar ("ColumnValue", "Green ");
HTParse ("BlockName", 0 );
It is instantiated once again
<Tr>
<Td> Green </td>
</Tr>
In the preceding example, Red, Green, and Blue are displayed in sequence. If the second parameter is 1, Blue, Gree, and Red are displayed.
Interface
Int HTLoadFile (char * File );
The function loads the html template File and points to the File name. The file size cannot exceed 32 KB.
Void HTSetVar (char * Name, char * Value );
The function changes the Value of all variables named Name in the template to "Value" regardless of the original Value of the variable.
Void HTParse (char * Name, int ReverseFlag );
The function instantiates a block named Name, And the ReverseFlag controls the sequential output or reverse output.
Void HTFinish ();
Output files and release resources.
By the way, CGL
CGL is a lightweight cgi c library that provides some interfaces that allow users to easily operate dynamic web pages, such as reading Session, Get, Post, and other variables.