Groundwork provides the htmltags class for generating html programmatically. Though it is possible to write applications where 100% of the html comes from the template files, there are cases where you may want to generate it from within the program.
The htmltags class provides methods for generating all tags and endtags valid in html 4.0. The first parameter of each method is a strstream. It can be NULL if you want to write the tag directly to the browser or an instance of a strstream if you want to build a string of tags. The remaining parameters are attribute/value pairs followed by a terminating NULL. The htmltags class provides enums for the attributes of each valid tag. The only exceptions to this pattern are the doctype() and htmlTag() methods which generate DOCTYPE and html tags.
The following code generates an html page with a paragraph of text and a table and writes it directly to the browser.
#include <groundwork/groundwork.h>
#include <groundwork/htmltags.h>
#include <groundwork/cgimodule.h>
class myclass : public groundwork, public htmltags {
public:
myclass(void *apistruct);
int execute();
};
myclass::myclass(void *apistruct) : groundwork(apistruct), htmltags(apistruct) {}
int myclass::execute() {
// send an http header
sendHeader();
// html
htmlTag(NULL);
// paragraph
pTag(NULL,NULL);
inputoutput::output(APISTRUCT,NULL,"This is a paragraph. ");
// bold
bTag(NULL,NULL);
inputoutput::output(APISTRUCT,NULL,"This sentence is bold. ");
endBTag(NULL);
// italics
iTag(NULL,NULL);
inputoutput::output(APISTRUCT,NULL,"This sentence is italicized. ");
endITag(NULL);
endPTag(NULL);
// table
tableTag(NULL,table_border,"1",
table_cellpadding,"0",
table_cellspacing,"0",
NULL);
trTag(NULL,NULL);
tdTag(NULL,NULL);
inputoutput::output(APISTRUCT,NULL,"Here's an image...");
endTdTag(NULL);
tdTag(NULL,td_width,"10",NULL);
// image
imgTag(NULL,img_src,"http://www.mydomain.com/images/myimage.gif",
img_alt,"myimage",
img_height,"10",
img_width,"10",
NULL);
endTdTag(NULL);
endTrTag(NULL);
endTableTag(NULL);
endHtmlTag(NULL);
return 1;
}
MAIN {
myclass mc(APISTRUCT);
mc.execute();
}
The following code generates an html page with a paragraph of text and a table and stores it in a strstream which is later written to the browser.
#include <groundwork/groundwork.h>
#include <groundwork/htmltags.h>
#include <groundwork/cgimodule.h>
class myclass : public groundwork, public htmltags {
public:
myclass(void *apistruct);
int execute();
};
myclass::myclass(void *apistruct) : groundwork(apistruct), htmltags(apistruct) {}
int myclass::execute() {
// send an http header
sendHeader();
// create a strstream to contain the html
strstream *container=new strstream();
// html
htmlTag(container);
// paragraph
pTag(container,NULL);
inputoutput::output(APISTRUCT,container,"This is a paragraph. ");
// bold
bTag(container,NULL);
inputoutput::output(APISTRUCT,container,"This sentence is bold. ");
endBTag(container);
// italics
iTag(container,NULL);
inputoutput::output(APISTRUCT,container,"This sentence is italicised. ");
endITag(container);
endPTag(container);
// table
tableTag(container,table_border,"1",
table_cellpadding,"0",
table_cellspacing,"0",
NULL);
trTag(container,NULL);
tdTag(container,NULL);
inputoutput::output(APISTRUCT,container,"Here's an image...");
endTdTag(container);
tdTag(container,td_width,"10",NULL);
// image
imgTag(container,img_src,"http://www.mydomain.com/images/myimage.gif",
img_alt,"myimage",
img_height,"10",
img_width,"10",
NULL);
endTdTag(container);
endTrTag(container);
endTableTag(container);
endHtmlTag(container);
// write the container to the browser
inputoutput::output(APISTRUCT,NULL,container->str());
// clean up
delete container;
return 1;
}
MAIN {
myclass mc(APISTRUCT);
mc.execute();
}