Sheerpower®
A Guide to the Sheerpower Language


Previous Contents Index

19.1.3.3 Use of GETSYMBOL$() in MATRIX.SPSRC

At the beginning of the matrix.spsrc program, a Code Area was used to define "highvalue" as a symbol using the GETSYMBOL$() function.


// Change highvalue  to change the table size 
[[
highvalue=val(getsymbol$('highvalue'), false) 
if highvalue = 0 then highvalue = 10 
highvalue = min(25, highvalue)  // limit to 25x25 
]]

This allows the size of the multiplication tables to be changed according to the optional highvalue parameter entered in the browser URL.


http://localhost/matrix.spsrc?highvalue=25

More on GETSYMBOL$() can be found at Section 6.4.16, GETSYMBOL$(str_expr1,[boolean]).

The [[ tag all by itself on a line indicates the start of a Code Area. The end of the Code Area is defined by the ]] tag all by itself on a line.

When the Code Area is completed with the ]] tag by itself on a line, SPINS Webserver processes the next area of code as Script until another Code Area is defined.

The entire matrix.spsrc sample program is shown below:

Example 19-3 MATRIX.SPSRC Sample Scripting Program

<html> 
<head> 
</head> 
<body> 
 
// Change highvalue  to change the table size 
[[ 
highvalue=val(getsymbol$('highvalue'), false) 
if highvalue = 0 then highvalue = 10 
highvalue = min(25, highvalue)  // limit to 25x25 
]] 
 
<table cellpadding=10> 
// let's do the LEFT SIDE first 
<tr> 
<td> 
 
<h1><center>Multiplication Table<br>Written in SCRIPT</center></h1> 
 
<table border=2 cellpadding=5> 
  <tr> 
  <td>&nbsp; 
  [[for j=1 to highvalue]] 
    <td bgcolor=yellow align=right>[[j]] 
  [[next j]] 
  <tr> 
  [[for i= 1 to highvalue]] 
    <tr> 
    <td bgcolor=yellow align=right> [[i]] 
 
    [[for j=1 to highvalue]] 
      <td align=right> [[=i*j]] 
    [[next j]] 
  [[next i]] 
</table> 
 
// And, now the right-side -- but this time in a CODE area 
[[ 
print '<td>' 
 
print '<h1><center>Multiplication Table<br>Written in CODE</center></h1>' 
 
print '<table border=2 cellpadding=5>' 
print '<tr>' 
print '<td>&nbsp;' 
for j=1 to highvalue 
  print '<td bgcolor=yellow align=right>'; j; 
next j 
print '<tr>' 
for i= 1 to highvalue 
  print '<tr>' 
  print '<td bgcolor=yellow align=right>'; i 
  for j=1 to highvalue 
    print '<td align=right>'; i*j 
  next j 
next i 
print '</table>' 
]] 
 
</table> 
 
</body> 
</html> 

19.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 

19.3 Sheerpower Script Areas

FORMAT:


  [[%spscript]]  
  ... 
  ... 
  [[/%spscript]] 

EXAMPLE:

Example 19-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]]

PURPOSE:

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.

Code comments in Sheerpower Script Areas

Within Sheerpower script areas, the "//" is used for inserting comments.

DESCRIPTION:

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 

19.3.1 Embedding Sheerpower Code Inside a Script Area

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 

19.4 Sheerpower Code Areas

FORMAT:


  [[%spcode]]    
  ... 
  ... 
  [[/%spcode]] 

EXAMPLE:

Example 19-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, first make sure that SPINS_webserver is running. 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! 

PURPOSE:

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.

DESCRIPTION:

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

Comments in Sheerpower Code Areas

Within Sheerpower Code Areas, the "//" is used to insert program comments.

19.4.1 Displaying Complex Expression Results

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 


Previous Next Contents Index