Using FoxPro commands to generate HTML

HTML is just text and FoxPro has always been very good at manipulating text. Generating the HTML on your own PC gives you a static document on the web but that's sometimes all that the user needs. There are four easy of generating HTML from FoxPro:

  • The Web Page Wizard
  • The GenHTML utility
  • TextMerge commands
  • The StrToFile() function
  • The FoxPro Editor

Web Page Wizard

This is more useful than most wizards and creates a table of data on a web page from a VFP table or cursor. The wizard generates an HTML file but can also generate a prg file that will recreate that web page from a later version of the table. This generated program uses GenHTML and creates and uses a new entry in the GenHTML table.

GenHTML

The 'Save as HTML' option on the File menu calls the generator program specified by system variable _GENHTML. This is usually GenHTML.Prg but you can write your own utility if you want to improve it. GenHTML.Prg can be added to your project and distributed in an executable.

The simple syntax is:

DO GenHTML With lcHTMLFileName, lcTableName

You can extend the power of GenHTML by referring to two FoxPro Foundation Classes and a table of styles:

  • _HTML.vcx
  • _HTMLSty.vcx
  • GenHTML.dbf

These too can be distributed in your application.

TextMerge

TextMerge is a tool that dates from FoxPro version 2.00 where we used it to create reports. TextMerge produces a pure ASCII output to a text file and the only way to get special effects in these reports was to embed printer control codes in the text. The Report Writer made life so much easier that TextMerge is little used now.

The output from TextMerge can be a mixture of text literals, the contents of fields or memory variables, and the result of VFP functions. This flexibility means that the TextMerge commands have become useful again as powerful tools for generating a web page from FoxPro data.

More details on TextMerge here.

StrToFile

StrToFile is a new function introduced with VFP 5. It takes a string variable and writes it into a text file. You have the option of creating a new file, overwriting an existing file, or adding text to the end of an existing file.

The StrToFile function returns the number of bytes written so you can check this value to ensure that your file has actually been created. The VFP 7 version of StrToFile (and its partner FileToStr) opens the file in Shared mode to reduce the risk of conflict

Use Customer

*-- Start the string with a heading for the page
lcHTML = '<h1>Customer List</h1>'

*-- Now append 'Company' and the company name for each record
SCAN
   lcHTML = lcHTML + '<br>Company  '
   lcHTML = lcHTML + ALLTRIM (Company)
ENDSCAN

*-- And send it to a file
=StrToFile (lcHTML, 'Customer.htm')

FoxPro Editor

Don't forget that FoxPro has a good text editor built into it that's much better than Notepad as a basic tool for writing HTML. Think of all these advantages:

  • Multiple levels of Undo.
  • Drag and Drop.
  • Automatic indenting.
  • Intellisense which can be extended so that typing PP gives you the opening and closing paragraph tags and puts the cursor between them:

    <P>
       |
    </P>
  • Global search and replace across all pages using Code References on the Tools menu.
  • Write simple FoxPro programs to write and manipulate your HTML files.