NexusDB WebScripts (Part 2) - A Basic Page
Welcome to part 2. Here we will break down the structure of a WebScript page.
NexusDB WebScripts utilise a PascalScript engine to handle server-side operations. All WebScript pages use the .nxscript file extension for the web server to recognise that the file contains PascalScript. |
If you give your web files a .html extension the PascalScript code will not be interpreted.
While .nxScript files are also used in the nxServer for other tasks (e.g. used with nxCommand.exe), for a WebApp, the .nxScript file's purpose is to combine client-side handling and server-side calls to generate a web page.
While .nxScript files are also used in the nxServer for other tasks (e.g. used with nxCommand.exe), for a WebApp, the .nxScript file's purpose is to combine client-side handling and server-side calls to generate a web page.
Like other web-driven languages, your basic .nxscript WebScript file has several elements:
The structure of a WebScript page follows a top-down processing approach.
All displayed WebScript content exists within a "begin...end." pair. Prior to this, variables, constants, functions, and other .nxScript files to be called within this pair can be declared.
Here is some WebScript code which displays your typical "Hello World" example:
- <% ... %>
- These tags are used to encapsulate PascalScript code
- Any code outside of these are considered HTML/JavaScript/CSS
- HTML/JavaScript/CSS can be written inside these tags using
- Output.Writeln(<String>); OR
- Output.Write(<String>);
- Code commenting
- PascalScript portions of a WebScript page can be commented with
- // - text written after this on a line is ignored by the parser
- (* ... *) - text encapsulated within is ignored by the parser
- HTML portions of code are commented with the <!-- ... --> tags
- PascalScript portions of a WebScript page can be commented with
The structure of a WebScript page follows a top-down processing approach.
All displayed WebScript content exists within a "begin...end." pair. Prior to this, variables, constants, functions, and other .nxScript files to be called within this pair can be declared.
Here is some WebScript code which displays your typical "Hello World" example:
<%
(* *****************************************************************************
001 - Hello World Example
This is a very basic web page which does not connect to any NexusDB-related components, but shows the structure of a basic nxServer-compliant WebScript
***************************************************************************** *)
(*
Populating the web page with content is done within the main "begin" and "end." key words. Prior to this, a number of things can be declared:
- variables
- constants
- functions
- additional nxscript files containing additional functions, variables, and constants (to discuss later)
*)
var
myVar: String;
const
myConst = 'Hello World';
begin
(*
The entire contents of a web page must exist within the begin .. end. points, including defining the header and footer.
Writing to a page is done using Output.Write('...'); and Output.Writeln('...');
*)
myVar := myConst;
Output.Writeln('<html>');
Output.Writeln('<head>');
Output.Writeln('<title>001 - ' + myConst + ' Example</title>');
Output.Writeln('</head>');
Output.Writeln('<body>');
// HTML can be directly written to the web page like this
%>
<h1><% Output.Writeln(myVar); %></h1>
<p>Welcome to this rather bland web page</p>
<%
Output.Writeln('</body>');
Output.Writeln('</html>');
end.
%>
(* *****************************************************************************
001 - Hello World Example
This is a very basic web page which does not connect to any NexusDB-related components, but shows the structure of a basic nxServer-compliant WebScript
***************************************************************************** *)
(*
Populating the web page with content is done within the main "begin" and "end." key words. Prior to this, a number of things can be declared:
- variables
- constants
- functions
- additional nxscript files containing additional functions, variables, and constants (to discuss later)
*)
var
myVar: String;
const
myConst = 'Hello World';
begin
(*
The entire contents of a web page must exist within the begin .. end. points, including defining the header and footer.
Writing to a page is done using Output.Write('...'); and Output.Writeln('...');
*)
myVar := myConst;
Output.Writeln('<html>');
Output.Writeln('<head>');
Output.Writeln('<title>001 - ' + myConst + ' Example</title>');
Output.Writeln('</head>');
Output.Writeln('<body>');
// HTML can be directly written to the web page like this
%>
<h1><% Output.Writeln(myVar); %></h1>
<p>Welcome to this rather bland web page</p>
<%
Output.Writeln('</body>');
Output.Writeln('</html>');
end.
%>
In the above example I've combined various elements to show you various ways in which you can generate the web page.
The use of Output.Writeln(...) calls is reminiscent of writing command-line applications, except instead your output is a web browser.
The use of Output.Writeln(...) calls is reminiscent of writing command-line applications, except instead your output is a web browser.
TASK:
Copy and paste the above example into a text file named index.nxscript and place it in the WebApp folder you created in part 1. Then open your WebApp in a web browser using
localhost:<WebApp port>
The index.nxscript file is the default page for a WebApp if a specific .nxscript file isn't defined in the web browser address bar.
Copy and paste the above example into a text file named index.nxscript and place it in the WebApp folder you created in part 1. Then open your WebApp in a web browser using
localhost:<WebApp port>
The index.nxscript file is the default page for a WebApp if a specific .nxscript file isn't defined in the web browser address bar.
Incorporating JavaScript and CSS styling is also possible and can be integrated as it would on a HTML page. Integration of JavaScript and CSS will allow client-side processing of the web page, of which there are many great and wonderful things that can be done [beyond the scope of this part].
The above basics will allow you to generate a page with static content. However this is utilising a web server integrated into an NexusDB database server - we want DATABASE DRIVEN CONTENT!
In part 3 we will connect an existing database and display some basic data from a table. Proceed to part 3
The above basics will allow you to generate a page with static content. However this is utilising a web server integrated into an NexusDB database server - we want DATABASE DRIVEN CONTENT!
In part 3 we will connect an existing database and display some basic data from a table. Proceed to part 3