Techniques for Generating HTML

General

There are several ways to send HTML to the client:

These are related topics:

Use the Client object

A simple way to emit text to the client is to use the Client object's << operator.  Here's an example:

Client << "Hello, world.<br>";

If the HTML you want exists in a file, simply use the GetFile function.

Client << GetFile("helloworld.htm";

Embed HTML in the CloudScript program

A CloudScript program may contain embedded HTML surrounded by the delimiters "<$" and "$>.  All the text between the delimiters is treated as if it were a single "Client <<" statement.  Thus,

<$Hello, world.<br>$>

would put the line "Hello, world." on the client's browser.

<$
Line 1.<br>
Line 2.<br>
Line 3.<br>
Line 4.<br>
Line 5.<br>
$>

As part of a CloudScript program, the entire sequence above starting with the <$ and going to the $> is treated as a single statement.  Thus, we could write:

if (name == "Smith")
  <$
  Hello, Smith.<br>
  $>
else
  <$
  Hello, Jones.<br>
  $>

Using the GetFile Function

Use the GetFile function to read in the contents of a file and store it as a string.

string GetFile (filename);

So ..

s = GetFile("x.html");

would read the contents of the file "x.html" into the string s.

Replacing Text

Use the Replace function to scan text and replace CloudScript expressions with their values.  

string Replace (text);

will result in the generation of a string by scanning text and looking for the delimiters <? and ?>.  It will replace the CloudScript expression between the delimiters with the value of that expression.

Imagine that the file named "x.html" contains the following:

Hello, <?name?>.<br>

Then, the CloudScript program:

name = "Jones";
Client << Replace(GetFile("x.html"));

will result in the string "Hello, Jones" being sent to the client browser.

NOTE: HTML editors will take the character "<" and store it as "&lt;";  they will take the character ">" and change it to "&gt;".  Therefore, the Include function will actually search for these special strings when doing replacement.  You can change the BeginHTML, EndHTML, BeginExpression, and EndExpression delimiters if you wish.

The expression can be any valid CloudScript expression.  Thus,

name = "Smith";

<$Hello, <?reverse(name)?>.$>

would cause "Hello, htimS." to be displayed on the client's browser.

Hello, htimS.

An alternate form for the Replace function is:

string Replace (text, arg1 ...);

with a variable number of arguments in the function call.

Within the text contained in the file you may refer to the arguments as arg[0], arg[1], arg[2] and so on.  

Assume the file x.html contains

<?arg[0]?>, <?arg[1]?>, <?arg[2]?>

and, the CloudScript program:

Client << Replace(GetFile("x.html"), "Henry", "George", "Charles");

would result in the following:

Henry, George Charles

Special Considerations

CloudScript has a special set of variables that are used to control, among other things, how included HTML text is scanned for replacement.  These variables are stored in a CloudScript table called SpecialVariables.  Here are the initial values and meanings of the members of SpecialVariables:

BeginExpression "&lt;?" In text included from a file, this signifies the beginning of a CloudScript expression.  If you are using an HTML editor, it looks like "<?" but the "<" gets transformed by the editor into "&lt;". 
EndExpression "?&gt;" In text included from a file, this signifies the end of a CloudScript expression.  If you are using an HTML editor, it looks like "?>" but the ">" gets transformed by the editor into "&gt;".

You may change these values if you wish.  For example, to change the special sequence that starts an expression to "**", your CloudScript program would have:

SpecialVariables["BeginExpression"] = "**";

Replace1

A special variant of the Replace function is Replace1.  It uses the standard values "<?" and "?>" as the delimiters -- without URL-encoding.  Thus:

x = "The value is '<?a?>'<br>";
a = "abc";
y = Replace1(x);
Client << y;

.. would result in

The value is 'abc'.