| Previous | Contents | Index |
20.2 Defining CODE and SCRIPT Areas
To write web scripting programs in SheerPower, it is important to understand how CODE and SCRIPT areas are defined.
In a SheerPower web scripting program, one is always either in a CODE AREA or a SCRIPT AREA. Stopping one area automatically starts the other. It is implied that when a new area is started (for example, a new CODE area) that the previous area has ended (for example, a SCRIPT area).
A CODE AREA in a web scripting program can be started with either of the following tags alone on a single line:
<<%spcode>> << |
A CODE area can be ended by any one of the following three tags alone on a single line:
<</%spcode>> >> <<%spscript>> ! a beginning Script area tag will also end a code area |
A SCRIPT area can be started with with the following tag by itself on a single line:
<<%spscript>> |
A SCRIPT area can be ended by one of the following tags placed on a single line by itself:
<</%spscript>> ! closing script tag << ! the start of a CODE area <<%spcode>> ! the start of a CODE area |
<<%spscript>> ... ... <</%spscript>> |
| Example 20-4 %SPSCRIPT TAG |
|---|
// The code below illustrates the %SPSCRIPT tag // causing the HTML to be displayed in a browser window <<%spscript>> <html> <head> <title>Greetings!</title> </head> <h2>Welcome to my website!</h2> <</%spscript>> |
The tag <<%spscript>> tells SheerPower that the script area that follows is to be output to the browser. The script area ends when you use the <</%spscript>> tag all by itself on a line.
The <<%spcode>> tag or the short tag "<<" all by itself on a line will also end a script area by starting a code area. It is implied that the Script Area is now complete.
Within SheerPower script areas, the "//" is used for inserting comments. |
When web scripting, each text line inside a SheerPower script area becomes one or more PRINT statements at compile-time. For example:
<<%spscript>> <h1> Your name is <<name$>> </h1> |
At compile-time this generates:
print '<h1> Your name is '; print name$; print ' </h1>' |
Another example:
<<for idx = 1 to 5>> <h1> idx is <<idx>> </h1> <<next idx>> |
Generates at compile-time:
for idx = 1 to 5 print '<h1> idx is '; print idx; print ' </h1>' next idx |
While in a SheerPower script area, text inside of the << >> tags is treated as embedded SheerPower code. For example:
<<%spscript>> <<for idx = 1 to 5>> <h1> idx is <<idx>> </h1> <<next idx>> <</%spscript>> |
The following is output to the browser:
idx is 1 idx is 2 idx is 3 idx is 4 idx is 5 |
If the embedded code consists of an expression, then the expression is evaluated and the results are sent to the browser. For example:
<<%spcode>> a=45 b=10 <<%spscript>> The answer is <<=a+b>> <</%spscript>> <</%spcode>> |
The following is output to the browser:
The answer is 55 |
<<%spcode>> ... ... <</%spcode>> |
| Example 20-5 %SPCODE TAG |
|---|
<<%spcode>>
city$ = getsymbol$("city")
print "<h1>"
select case city$
case "Calgary"
print "Calgary is a great city!"
case else
print "You chose a different city!"
end select
print "</h1>"
<</%spcode>>
|
To run the above example, copy and paste the code into a new program file called spcode_city.spsrc and save it in sheerpower/sphandlers/scripts/sp4gl/. Open a browser window and enter the URL http://localhost/spcode_city.spsrc?city=Calgary. The following is output to the browser:
Calgary is a great city! |
A SheerPower Code Area starts with a <<%spcode>> tag. It designates a section of code within a web scripting program that contains SheerPower code. A code area is ended by a <</%spcode>> tag on a line all by itself.
Code Areas can also be started with the << by itself on a line. This is referred to as a "short tag"... a shortened version of the full <<%spcode> tag. The short tag used to end a Code Area is the >> by itself on a line.
If a <<%spscript>> tag is used after the <<%spcode>> tag it implies that the code area has ended and a script area has started.
Within a SheerPower web scripting program, one is always in either a CODE area or a SCRIPT area. If a script area has ended, the next section of code is a code area.
When in a SheerPower code area, printing to channel zero (a "normal" print statement) causes the printed text to be sent to the browser. For example, to send HTML to the browser that displays a client's city in bold text:
<<%spcode>> print '<b>'; client(city); '</b>' |
Short tags can be used to start and end Code Areas. If a line begins with the << tag all by itself, then all following lines are treated as a Code Area.
An ending line consisting of only the >> tag ends the Code Area.
Within SheerPower Code Areas, the "//" is used to insert comments. |
When you need to display the results of complex expressions contained inside the embedded SheerPower code within a script area (surrounded by << >>), use an "=" at the beginning of the expression. For example:
// start a SheerPower code area <<%spcode>> a=45 b=10 // end the code area and start the SheerPower script area <<%spscript>> // embed the SheerPower code within the script area between << >> // the "=" in front of the expression causes the results to display in the browser <h2><font color=darkgreen>The answer is <<=a+b>>.</font></h2> <</%spscript>> // end the script area and begin the code area |
While in a SheerPower Code Area, you can embed any other code or script, including an .HTM or .HTML file, by using the %INCLUDE directive. SheerPower will recognize the HTML code and output it all to the browser. This makes it trivial to embed large blocks of HTML within your program. To include an HTML file:
%include "@sample_page.html" |
If the file type is .SPSRC, .SPINC or .SPRUN, then the entire include file is assumed to be code. Otherwise it is assumed to be script.
See Section 3.9.4,%INCLUDE for more on the %INCLUDE directive in SheerPower.
20.4.3 CGI Environment Symbols - GETSYMBOL$ Function
variable_name$ = getsymbol$('cgi_symbol')
|
| Example 20-6 GETSYMBOL$ Function |
|---|
// get the CGI symbol value of "password" and store it into
// the variable mypassword$
<<%spcode>>
mypassword$ = getsymbol$('password')
print 'My Password is:'; mypassword$
<</%spcode>>
|
CGI Symbol Names are case sensitive. |
Standard CGI environment symbols must be prefixed with "env:". For example:
<<%spcode>>
myip$ = getsymbol$('env:REMOTE_ADDR')
print 'My IP address is:'; myip$
<</%spcode>>
|
For a list of CGI environment symbols supported, see Section 19.3.8, Summary of CGI Environment Variables.
| Previous | Next | Contents | Index |