DelpWebScript

HTML Embeding

Homepage
DWS
HTML Embeding
 

Embeding DWS Scripts in HTML Documents

To use this feature of DWS you need a ISAPI or NSAPI module capable to process DWS scripts.

If a websurfer requests a link like this:
http://www.example.com/scripts/mymodule.dll/myscript.dws?value1=0
your module executes the script "myscript.dws" and returns the output (TDelphiWebScript.Result) to the websurfer. Of course you could generate all HTML tags in your DWS script:

sendln ('<html>');
sendln ('<body>');
for x := 1 to 5 do sendln ('<p>Hello World</p>');
sendln ('</body>');
sendln ('</html>');

This procedure is very uncomfortable and hard to change. It's easier to create HTML pages in a HTML editor and to embed the script in comment tags. In DWS this looks like this:

<html>
<body>
<% for x := 1 to 5 do begin %>
<p>Hello World</p>
<% end; %>
</body>
</html>

Most actual HTML editors accept <% %> as comment tag and leave them unchanged. But in DWS it's also possible to use <!-- /--> as script tag.

If Statements and Loops

It's possible to use if-statements and loops.

<% if true then %>
<p>This appears in the output</p>
<% else %>
<p>This doesn't appear in the output</p>

<% for x := 1 to 5 do %>
<p>This appears five times in the output</p>

<%
x := 5;
while x > 0 do begin
%>
<p>This appears in the output</p>
<% dec (x);
end;
%>

Important: The HTML code between two script tags (%> ... <%) is seen as a instruction in DWS. If you use a if-statement or a loop without a begin-end block everything after "then" or "do" is seen as instruction. Example:

<% if x = 0 then %>
<p>X is zero!</p>
<% else %>
<p>X isn't zero!</p>
....
</body>
</html>

If x is zero only "<p>X is zero!</p>" appears in the output string. Change such code like this:

<% if x = 0 then %>
<p>X is zero!</p>
<% else begin {use a begin - end block!!! } %>
<p>X isn't zero!</p>
<% end %>
....
</body>
</html>

Expressions

A special form of script tag allows you to shortcut the output of expressions of any kind:

<p> <% send (text) %> </p>

<p><%= text %></p>

It's also possible to return more complicated expressions:

<p> <%= formatdatetime ('%s: %d', text, value) %> </p>
<p> <%= sin (cos (pi)) + power (2.0, 4.623) %> </p>