Summary: | WebTemplate allows C programs to easily make web pages through the use of HTML templates. |
---|---|
Version: | 1.15 |
Download: | Current version: webtpl-1.15 |
In the latter case the value of the macro becomes text of the template, with all internal macros and dynamic blocks expanded.
<!-- BEGIN DYNAMIC BLOCK: name -->
HTML code
<!-- END DYNAMIC BLOCK: name -->
where name consists entirely of alphanumeric characters or underscores, and is used to identify the block within the program. The 'BEGIN' and 'END' tags must be on lines by themselves. They cannot be mixed with other text on the same line.
Each time a dynamic block is parsed, the resulting text, including all macros or nested dynamic blocks, are added to the value of the enclosing template.
WebTemplate allows an abbreviation of the block identifier. So the above pattern could look like this:
<!-- BDB: name -->
HTML code
<!-- EDB: name -->
The BDB and EDB still have to be by themselves on the line, however.
start_comment_marker ...
Comments
end_comment_marerk ...
or
single_comment_line_marker Comments
where all of the markers must appear at the start of a line.
The API WebTemplate_set_comments activates comments.
The common procedure for the template system is something like this:
See the example.
Description: | Get the text message for an error |
---|---|
Syntax: | char* WebTemplate_get_error_string(WebTemplate W) |
Arguments: |
|
Return: | Text of the error. |
Errors: |
|
Notes: |
|
Description: | Initialize a template structure |
---|---|
Syntax: | WebTemplate WebTemplate_new() |
Arguments: |
|
Return: | |
Errors: |
|
Notes: |
|
Description: | Free a template structure | ||
---|---|---|---|
Syntax: | void WebTemplate_free(WebTemplate W) | ||
Arguments: |
| ||
Return: | |||
Errors: |
| ||
Notes: |
|
Description: | Set the comments delimiters | ||||||
---|---|---|---|---|---|---|---|
Syntax: | void WebTemplate_set_comments(WebTemplate W, char* start, char* end) | ||||||
Arguments: |
| ||||||
Return: | |||||||
Errors: |
| ||||||
Notes: |
|
Description: | Read a template from a named file | ||||||
---|---|---|---|---|---|---|---|
Syntax: | int WebTemplate_get_by_name(WebTemplate W, char* name, char* filename) | ||||||
Arguments: |
| ||||||
Return: | 0 if OK; else the unix errno or -1. | ||||||
Errors: |
| ||||||
Notes: |
|
Description: | Read a template from a file descriptor | ||||||
---|---|---|---|---|---|---|---|
Syntax: | int WebTemplate_get_by_fd(WebTemplate W, char* name, int fd) | ||||||
Arguments: |
| ||||||
Return: | 0 if OK; else the unix errno or -1. | ||||||
Errors: |
| ||||||
Notes: |
|
Description: | Read a template from a file pointer | ||||||
---|---|---|---|---|---|---|---|
Syntax: | int WebTemplate_get_by_fp(WebTemplate W, char* name, FILE* fp) | ||||||
Arguments: |
| ||||||
Return: | 0 if OK; else the unix errno or -1. | ||||||
Errors: |
| ||||||
Notes: |
|
Description: | Assign a value to a macro | ||||||
---|---|---|---|---|---|---|---|
Syntax: | void WebTemplate_assign(WebTemplate W, char* name, char* value) | ||||||
Arguments: |
| ||||||
Return: | |||||||
Errors: |
| ||||||
Notes: |
|
Description: | Assign an intever value to a macro | ||||||
---|---|---|---|---|---|---|---|
Syntax: | void WebTemplate_assign_int(WebTemplate W, char* name, int value) | ||||||
Arguments: |
| ||||||
Return: | |||||||
Errors: |
| ||||||
Notes: |
|
Description: | Parse a dynamic block | ||||
---|---|---|---|---|---|
Syntax: | int WebTemplate_parse_dynamic(WebTemplate W, char* block_name) | ||||
Arguments: |
| ||||
Return: | 0 if OK; 1 if template not found. | ||||
Errors: |
| ||||
Notes: |
|
Description: | Parse a template | ||||||
---|---|---|---|---|---|---|---|
Syntax: | int WebTemplate_parse(WebTemplate W, char* macro_name, char* template_name) | ||||||
Arguments: |
| ||||||
Return: | 0 if OK; 1 if template not found. | ||||||
Errors: |
| ||||||
Notes: |
|
Description: | Set output file descriptor | ||||
---|---|---|---|---|---|
Syntax: | void WebTemplate_set_output(WebTemplate W, int fd) | ||||
Arguments: |
| ||||
Return: | |||||
Errors: |
| ||||
Notes: |
|
Description: | Add a header name and value | ||||||
---|---|---|---|---|---|---|---|
Syntax: | void WebTemplate_add_header(WebTemplate W, char* name, char* value) | ||||||
Arguments: |
| ||||||
Return: | |||||||
Errors: |
| ||||||
Notes: |
|
Description: | Retrieve a macro value | ||||
---|---|---|---|---|---|
Syntax: | char* WebTemplate_macro_value(WebTemplate W, char* name) | ||||
Arguments: |
| ||||
Return: | A string containing the macros's value, NULL if not found | ||||
Errors: |
| ||||
Notes: |
|
Description: | Output a macro value | ||||
---|---|---|---|---|---|
Syntax: | int WebTemplate_write(WebTemplate W, char* name) | ||||
Arguments: |
| ||||
Return: | 0 if OK; else the unix errno or -1. | ||||
Errors: |
| ||||
Notes: |
|
Description: | Output headers | ||
---|---|---|---|
Syntax: | int WebTemplate_header(WebTemplate W) | ||
Arguments: |
| ||
Return: | 0 if OK; else the unix errno. | ||
Errors: |
| ||
Notes: |
|
Description: | Set no header option | ||
---|---|---|---|
Syntax: | void WebTemplate_set_noheader(WebTemplate W) | ||
Arguments: |
| ||
Return: | |||
Errors: |
| ||
Notes: |
|
Description: | Reset output flags and values | ||
---|---|---|---|
Syntax: | int WebTemplate_reset_output(WebTemplate W) | ||
Arguments: |
| ||
Return: | |||
Errors: |
| ||
Notes: |
|
The template package includes some convenience routines for managing cookies and parsing form data.
In addition, the template system stores the REMOTE_USER environment variable, set by the web server on authenticated requests, in the global variable remote_user.
Description: | Load the form and request arguments (parameters) | ||
---|---|---|---|
Syntax: | void WebTemplate_get_args(WebTemplate W) | ||
Arguments: |
| ||
Return: | |||
Errors: |
| ||
Notes: |
|
Description: | Get a parameter by name | ||||
---|---|---|---|---|---|
Syntax: | char* WebTemplate_get_arg(WebTemplate W, char* name) | ||||
Arguments: |
| ||||
Return: | A string containing the parameter's value. | ||||
Errors: |
| ||||
Notes: |
|
Description: | Get a multivalued parameter by name | ||||
---|---|---|---|---|---|
Syntax: | char** WebTemplate_get_arg_list(WebTemplate W, char* name) | ||||
Arguments: |
| ||||
Return: | A null-terminated list of strings containing the parameter's values. | ||||
Errors: |
| ||||
Notes: |
|
Description: | Free a parameter list | ||
---|---|---|---|
Syntax: | void WebTemplate_free_arg_list(char** list) | ||
Arguments: |
| ||
Return: | |||
Errors: |
| ||
Notes: |
|
Description: | Get a parameter's name and value by number | ||||||
---|---|---|---|---|---|---|---|
Syntax: | char* WebTemplate_get_next_arg(WebTemplate W, int* n, char** value) | ||||||
Arguments: |
| ||||||
Return: | A string containing the n'th parameter's name | ||||||
Errors: |
| ||||||
Notes: |
|
Description: | Get a multi-part form value | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Syntax: | int WebTemplate_get_octet_arg(WebTemplate W, char* name, void** value, size_t* len, char** type, char** Vfilename') | ||||||||||||
Arguments: |
| ||||||||||||
Return: | non-zero if the parameter exists; zero if not | ||||||||||||
Errors: |
| ||||||||||||
Notes: |
|
Description: | Convert html to text | ||
---|---|---|---|
Syntax: | char* WebTemplate_html2text(char* string) | ||
Arguments: |
| ||
Return: | A string containing the decoded input | ||
Errors: |
| ||
Notes: |
|
Description: | Convert text to html | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Syntax: | char* WebTemplate_text2html(char* string) | ||||||||||
Arguments: |
| ||||||||||
Return: | The encoded string | ||||||||||
Errors: |
| ||||||||||
Notes: |
|
Description: | Get a cookie's value | ||||
---|---|---|---|---|---|
Syntax: | char* WebTemplate_get_cookie(WebTemplate W, char* name) | ||||
Arguments: |
| ||||
Return: | A string containing the parameter's value. | ||||
Errors: |
| ||||
Notes: |
|
Description: | Set a cookie's value | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Syntax: | void WebTemplate_set_cookie(WebTemplate W, char* name, char* value, time_t V'expires, char* domain, char* path, int secure) | ||||||||||||||
Arguments: |
| ||||||||||||||
Return: | |||||||||||||||
Errors: |
| ||||||||||||||
Notes: |
|
page.tpl
NOTE
Comments in this file are delimited by 'NOTE' and 'ENDNOTE'
ENDNOTE
<html>
<head>
<title>{TITLE}</title>
</head>
<body>
{BODY}
</body>
</html>
list.tpl
# comments in this file are identified by leading '#'
<h1>{TITLE}</h1>
<p>
<table colsep=3>
<tr><td align=right>n</td><td align=right>n!</td></tr>
<!-- BEGIN DYNAMIC BLOCK: num -->
<tr><td align=right>{N}</td><td align=right>{NFACT}</td></tr>
<!-- END DYNAMIC BLOCK: num -->
</table>
This program
/* Show factorials */
main()
{
WebTemplate W;
int n;
int f = 1;
char txt[12];
/* Initialize */
W = newWebTemplate();
/* Write the headers */
WebTemplate_header(W);
/* Load the two templates */
WebTemplate_set_comments(W, "NOTE", "ENDNOTE");
WebTemplate_get_by_name(W, "page", "page.tpl");
WebTemplate_set_comments(W, "#", NULL);
WebTemplate_get_by_name(W, "list", "list.tpl");
/* Set the title */
WebTemplate_assign(W, "TITLE", "10 factorials");
/* Make a line for each number */
for (n=1;n<11;n++) {
f = n * f;
sprintf(txt,"%d",n);
WebTemplate_assign(W, "N", txt);
sprintf(txt,"%d",f);
WebTemplate_assign(W, "NFACT", txt);
WebTemplate_parse_dynamic(W, "list.num");
}
/* Make the body of the page */
WebTemplate_parse(W, "BODY", "list");
/* Make the page */
WebTemplate_parse(W, "PAGE", "page");
/* Write the page */
WebTemplate_write(W, "PAGE");
exit (0);
}
produces this page:
n | n! |
1 | 1 |
2 | 2 |
3 | 6 |
4 | 24 |
5 | 120 |
6 | 720 |
7 | 5040 |
8 | 40320 |
9 | 362880 |
10 | 3628800 |