A FoxWeb script is a text file with the extension .fwx that contains HTML content as well as FoxWeb code.
The simplest form of a FoxWeb script is a regular static HTML file, whose extension has been changed to .fwx. If the script does not contain any FoxWeb code then its content is simply sent back to the browser without any processing. Although FoxWeb scripts are not required to contain any FoxWeb code, most scripts that you will be creating will be a combination of standard HTML content and FoxWeb code.
To publish a FoxWeb script on the Web, simply save it in a directory in the FoxWeb program tree and call it by typing its URL in your browser (FoxWeb scripts must be served by the FoxWeb application server, so you can not view a script by simply typing its physical path).
You can use any text editor to create FoxWeb scripts. Some programmers find it easier to use editors that were designed for use with Web-based server-side scripting and specifically Active Server Pages, such as Microsoft Visual Interdev. Others prefer to use Visual HTML editors. Some of these editors are more suitable than others for creating FoxWeb scripts. Certain editors tend to corrupt FoxWeb code by wrapping lines or replacing characters using a process called HTML-Encoding. In most cases you can work around the problem by using special techniques to enter your program code.
FoxWeb code is simply a Visual FoxPro code block, able to utilize virtually any command in the FoxPro language. Among other things, it can be used to search tables, manipulate data, perform calculations and create HTML content to be returned to the browser. In .fwx files, scripts are differentiated from text and HTML tags by delimiters. A delimiter is a sequence of characters that marks the beginning or end of a block. FoxWeb by default uses the delimiters <% and %> to enclose FoxWeb code. If the FoxWeb code block starts with an equal sign then FoxWeb treats the block includes a single expression, which is evaluated and inserted in the HTML output. Blocks not starting with an equal sign are treated as regular VFP code. The following example shows a simple FoxWeb script that contains a FoxWeb expression:
If you have experience with VFP you will recognize the function DATETIME(). When FoxWeb processes this page, it replaces <%=DATETIME()%> with the current date and time and returns the page to the browser:
|This page was served on 12/18/99 8:43:10 PM.|
A FoxWeb script can include unlimited FoxWeb code blocks intermingled with HTML content. VFP code can determine whether and how many times HTML sections are inserted in the output sent to the browser. Consider the following portion of a FoxWeb script:
In the above script the HTML section surrounded by the FOR NEXT loop is executed 9 times to return the following output:
Multiplication Table (multiples of 7)|
1 times 7 = 7
Similarly, HTML content surrounded by conditional statements such as IF and CASE commands is affected by the conditional expression. The following code displays a different greeting based on the time of the year:
A FoxWeb script may contain multiple procedures and functions. These procedures can be called by other procedures, or directly by a browser by including the procedure name in the URL. The following code is a FoxWeb script called SysDate.fwx with three procedures and one function. The procedures are meant to be called directly by the browser. The function, called PageHeader is used by all three procedures to produce an HTML header:
The first procedure, which has no PROCEDURE statement at the top displays the main menu for this sample application, and can be called by simply calling the fwx file itself. In order to call the other two procedures in the file (ShowDate and ShowTime), we need to include their names in the URL, as seen in the code of the main menu (<a href="ShowTime@SysDate.fwx">).
FoxWeb scripts can contain two types of comments, HTML and VFP.
HTML comments must start with <!-- and end with -->. They can span several lines and may not be located in a FoxWeb code block. HTML comments are sent to the browser and can be seen by the user, using the browser's View Source function.
VFP comments are the regular comments of the FoxPro language (both * and && comments are supported). VFP comments must be located in FoxWeb code blocks and, contrary to HTML comments, they are not sent to the browser. The following example contains both kinds of comments:
FoxWeb has the ability to call both FWX and PRG files. PRG files are regular FoxPro programs, which may only contain FoxWeb (VFP) code. You may not insert delimiters or mix free-flow HTML content with your FoxWeb code in PRG files. All output must be generated programmatically, using Response.Write or other FoxWeb functions.
There is no difference in terms of performance between PRG and FWX files. In fact, FWX files are converted into temporary PRG files before they are compiled into FXP files the first time they are called (and whenever they are modified). Other than backward compatibility and to hold function and class libraries, there is almost no reason to use PRG files in your FoxWeb applications. An FWX file with a single code block enclosed by delimiters is exactly the same as a PRG file, containing the same code without the delimiters.
As you will see in the Locating and Addressing Scripts topic, you must never use the PRG extension in URLs when calling PRG files. Depending on whether you utilize conventional or script-mapped URLs, you should either use no extension or the FWX extension (for uniformity you can make it a rule to always use the FWX extension, even when you are using conventional URLs). You may not mix PRG and FWX files with the same name in the same directory. FoxWeb will only run the newest of the two files, based on the file modification date. If you are not careful, you can overwrite an existing PRG file by creating and calling an FWX file with the same name in the same directory.
FoxWeb uses the following algorithm in deciding which file to run, regardless of whether the extension specified in the URL was FWX or nothing: