Writing FoxWeb Scripts

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.

Adding FoxWeb 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:

<HTML>
<BODY>
This page was served on <%=DATETIME()%>.
</BODY>
</HTML>

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.

Mixing FoxWeb Code Blocks with HTML Content

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:

Multiplication Table (multiples of 7)<P>
<%FOR i = 1 TO 9%>
<%=i%> times 7 = <%=i*7%><br>
<%NEXT%>

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
2 times 7 = 14
3 times 7 = 21
4 times 7 = 28
5 times 7 = 35
6 times 7 = 42
7 times 7 = 49
8 times 7 = 56
9 times 7 = 63

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:

<%
CurrentMonth = MONTH(DATE())
IF CurrentMonth = 12
    %>
Happy Holidays!<%
ELSE
    %>
Greetings!<%
ENDIF
%>

<P>
Welcome to the FoxWeb Code Workshop.

Including Multiple Procedures and Functions

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:

<%=PageHeader("Main Menu")%>
<%
CurrentHour = HOUR(DATETIME())
IF CurrentHour < 12
    %>
<b>Good Morning!</b><%
ELSE
    %>
<b>Hello!</b><%
ENDIF
%>

<p>
<ul>
<li><a href="ShowTime@SysDate.fwx">Show System Time</a></li>
<li><a href="ShowDate@SysDate.fwx">Show System Date</a></li>
</ul>
</body>
</html>
<%
RETURN


PROCEDURE ShowDate
%>

<%=PageHeader("System Date")%>
The Date is <%=DATE()%>
<p>
<a href="SysDate.fwx">Back</a>
</body>
</html>
<%
RETURN


PROCEDURE ShowTime
%>

<%=PageHeader("System Time")%>
The Time is <%=TIME()%>
<p>
<a href="SysDate.fwx">Back</a>
</body>
</html>
<%
RETURN


FUNCTION PageHeader
PARAMETERS PageTitle
RETURN "<html><head><title>" + PageTitle + "</title></head>" + ;
"<body><h2>" + PageTitle + "</h2>"
%>

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">).

Adding Comments to Scripts

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:

<HTML>
<!--This is an HTML comment-->
<BODY>
<%
* This is a VFP comment
a=3
b=8 && This is another VFP comment
%>

a + b = <%=a + b%>
</BODY>
</HTML>

PRG vs. FWX Files

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:

If FXP file exists and it is newer than PRG and FWX file
    Run FXP file
Else if PRG file exists and it is newer than FWX file
    Compile PRG file into FXP file
    Run FXP file
Else if FWX file exists
    Convert FWX file into PRG file
    Compile PRG file into FXP file
    If "Keep PRG Files" option is not enabled
        Delete PRG file
    End if
    Run FXP file
End if


© Aegis Group