RAD Studio
ContentsIndex
PreviousUpNext
Server-side Scripting in WebSnap

Page producer templates can include scripting languages such as JScript or VBScript. The page producer executes the script in response to a request for the producer's content. Because the Web server application evaluates the script, it is called server-side script, as opposed to client-side script (which is evaluated by the browser). 

This topic provides a conceptual overview of server-side scripting and how it is used by WebSnap applications. Although server-side scripting is a valuable part of WebSnap, it is not essential that you use scripting in your WebSnap applications. Scripting is used for HTML generation and nothing else. It allows you to insert application data into an HTML page. In fact, almost all of the properties exposed by adapters and other script-enabled objects are read-only. Server-side script isn't used to change application data, which is still managed by components and event handlers written in your application's source code. 

There are other ways to insert application data into an HTML page. You can use Web Broker's transparent tags or some other tag-based solution, if you prefer. For example, several projects in the WebSnap examples directory use XML and XSL instead of scripting. Without scripting, however, you will be forced to write most of your HTML generation logic in source code, which will increase your development time. 

The scripting used in WebSnap is object-oriented and supports conditional logic and looping, which can greatly simplify your page generation tasks. For example, your pages may include a data field that can be edited by some users but not others. With scripting, conditional logic can be placed in your template pages which displays an edit box for authorized users and simple text for others. With a tag-based approach, you must program such decision-making into your HTML generating source code.

WebSnap relies on active scripting to implement server-side script. Active scripting is a technology created by Microsoft to allow a scripting language to be used with application objects through COM interfaces. Microsoft ships two active scripting languages, VBScript and JScript. Support for other languages is available through third parties.

The page producer's ScriptEngine property identifies the active scripting engine that evaluates the script within a template. It is set to support JScript by default, but it can also support other scripting languages (such as VBScript).

Note: WebSnap's adapters are designed to produce JScript. You will need to provide your own script generation logic for other scripting languages.

Script blocks, which appear in HTML templates, are delimited by <% and %>. The script engine evaluates any text inside script blocks. The result becomes part of the page producer's content. The page producer writes text outside of a script block after translating any embedded transparent tags. Script blocks can also enclose text, allowing conditional logic and loops to dictate the output of text. For example, the following JScript block generates a list of five numbered lines:

<ul>
<% for (i=0;i<5;i++) { %>
   <li>Item <%=i %></li>
<% } %>
</ul>

(The <%= delimiter is short for Response.Write.)

Developers can take advantage of WebSnap features to automatically generate script.

When creating a new WebSnap application or page module, WebSnap wizards provide a template field that is used to select the initial content for the page module template. For example, the Default template generates JScript which, in turn, displays the application title, page name, and links to published pages.

The TAdapterPageProducer builds forms and tables by generating HTML and JScript. The generated JScript calls adapter objects to retrieve field values, field image parameters, and action parameters.

When the Web Page module uses TAdapterPageProducer the page module views become available when this component is double-clicked. You can access the page module view with the HTML resulting from the executed script using the HTML Script tab. The HTML Script tab displays the HTML and JScript generated by the TAdapterPageProducer object. Consult this view to see how to write script that builds HTML forms to display adapter fields and execute adapter actions.

A template can include script from a file or from another page. To include script from a file, use the following code statement:

<!-- #include file="filename.html" -->

When the template includes script from another page, the script is evaluated by the including page. Use the following code statement to include the unevaluated content of page1.

<!-- #include page="page1" -- >
Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!