Hello World Open on a page of its own


Simple Hello World

print "Hello World!"
MaxScript works only inside web pages: thus the simple instruction print "Hello World!" taken by itself does not make much sense. You have to place it into a web page in the right place: this means that it must be embedded into standard HTML like this:
Mixed mode (ASP style)
<HTML>
<BODY>
<%
print "Hello World!"
%>
</BODY>
</HTML>
Classic Style (CGI/Clipper like) - (compare with PHP)
<%
set maxlib to "c:\maxsis\dbfreev41\libs\free.lib"
web()
openWebPage()
? "Hello World!"
closeWebPage()
%>

Notice the two tags <% and %> (called server-side tags) that instruct the server to pass everything is delimited by them to a server-side processor, in this case our MaxScript interpreter, and wait for a response.

The code shown above shows a classic "Hello World" (a more advanced version is presented here below) and it is perfectly functional, meaning that it will produce a plain web page with basic layout: anyway today things tend to be more complex than that (at least on HTML side): for example no one would ever use plain HTML like that on a real page: users expects web pages to be visual appealing, with attractive layouts, well shaped font faces, frames, pictures, colors and visual effects.

Today this part of the job is achieved by separating the content of the page from its presentation.
While the content is demanded to plain HTML its presentation is demanded to one or more external files, containing the styles to be used (font size, colors, font position, backgrounds,etc).

This is done by some instructions you add in the head section of the web page, like this:
<link href="/css/custom.css" rel="stylesheet">
In facts there are many other things you must add to the head section of a web page before to even start writing a single line of HTML contents.
This is a typical example:
<head>
<title>www.mysite.com</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> 
<meta http-equiv="Pragma" CONTENT="no-cache"> 
<link href="/css/custom.css" rel="stylesheet">
<script src="/js/jquery.js"></script>
</head>
Notice that custom.css is the file where you should put all the necessary CSS instuctions for the correct visualization of your page (implying that you must know how to write CSS). These CSS instructions are then applied to your page by using the tag class.

HTML Frameworks

CSS is intriguing, but is yet another thing to learn.
And if your goal is simply to publish some content on the web it is frustrating to find out that there is yet another technology to absorb before you begin your real job.
Here it is when HTML frameworks come to rescue.

An HTML framework is a collection of fonts, styles and effects you can use in your pages without actually knowing how it's been made (and virtually without knowing anything about CSS). This greatly simplifies things.
But even if you don't need to know how a CSS it's been made, you still need to know how to use it, what to use and when.

There are many HTML frameworks around (Less4, Titan, Gridiculo.us,Yaml4, Skeleton, Foundation, Kube,etc). DBFree adopted one of the most famous: Twitter Bootstrap.
And, furthermore, DBFree takes Bootstrap and tries to make it even simpler to use, by taking care of the how and what. So all that remains to you is to decide when (or - most appropriately - where).

A modern Hello World!

As seen, in the real world, there are lot of things to place into a web page other than your actual content.

And logically this should be not a matter that strictly concerns your language interpreter (that is there to do other things and thus usually tends to ignore all the HTML stuff).
But considered that all that stuff must be put in the page anyway, and it must be done in every single page of your web site, it makes perfectly sense to have DBFree taking care of this task.
Dealing with frameworks and CSS is not its core business, but if you like so MaxScript can help you writing code in place of you and making the code of your web page as simple as this:

<!DOCTYPE html><html>
<%
path := mspconfigvar("LIBDIR")            //-- tells where to find DBFree libraries 
set maxlib to ( path + "\free.lib")       //-- loads the choosen library 
web()                                     //-- tells DBFREE this page is for the web
preload("/common/header.h",cSeed,"XYZ","pagetitle","DBFree "+filebone(pagefile())) 
 
               //-- loads CSS styles,Javascripts etc. from a file 
print( openPage() )                       //-- print the necessary HTML code to open the web page
%>
Hello world!
<%=box("Hello World again!")              //-- example of dismissable info box %>
<%=alert("Hello World once more!")        //-- example of dismissable alert box %><%
print( closePage() )                      //-- print the necessary code for closing the page
%>

With this result:

(Notice that the two box, gray and pale yellow, are part of bootstrap theme which is activated inside the header header.h, which is called by the function preload()).

MaxScript vs. pure HTML

The previous example presented the code for a page written in a combination of MaxScript and HTML:
you might have expected MaxScript to be less wordy, but consider the alternative of building the same page using pure HTML (10 lines versus 33) :
Pure HTML
<!DOCTYPE html>
<head>
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="-1">
<meta charset="iso-8859-1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" href="/favicon.png">
<title>Just a test page</title>

<!-- Bootstrap -->
<link href="/css/bootstrap.css" rel="stylesheet"> 
<script src="/js/jquery.min.js"></script>
<script src="/js/bootstrap.min.js"></script> 

<!--[if lt IE 9]>
<script src="/js/html5shiv.min.js"></script>
<script src="/js/respond.min.js"></script>
<![endif]-->

</head>

<body>
<div class="container-fluid">
Hello world!

<div class="alert alert-dismissible alert-default" style="background-color:#efefef;color:black">
<button type="button" class="close" data-dismiss="alert">X</button>
Hello World again!
</div>

<div class="alert alert-dismissible alert-warning">
<button type="button" class="close" data-dismiss="alert">X</button>
<h4>Hello World once more!</h4>
</div>

</div><!-- container //->
</body>
</html>
MaxScript (ASP mode
<!DOCTYPE html>
<%
path := mspconfigvar("LIBDIR")     
set maxlib to ( path + "\free.lib") 
web()
preload("/common/header.h",cSeed,"XYZ","pagetitle","DBFree "+filebone(pagefile())) 
 
 
print( openPage() ) 
%>
Hello world!
<%=box("Hello World again!") %>
<%=alert("Hello World once more!") %><%
print( closePage() )
%>

PHP vs MaxScript

Obviously you will have heard of PHP. This is a brief comparison:

Basic page

<html>
<body>
<?php
echo "Hello World";
?>
</body>
</html>
Each code line in PHP must end with a semicolon. The semicolon is a separator and is used to distinguish one set of instructions from another.
<html>
<body>
<%
print "Hello World" 
%>
</body>
</html>
Instructions end at end of line The carriage return is used as separator.

Variables and control structures

In PHP, you use // to make a one-line comment or /* and */ to make a comment block.
<?php
//-- There is only one string operator in PHP: The concatenation operator . (the dot)
$txt1="Hello World!";
$txt2="What a nice day!";
echo $txt1 . " " . $txt2;

//-- if statement
$d=date("D");
if ($d=="Fri") echo "Have a nice weekend!";

//-- if..else..elseif construct
if ($d=="Fri")
  {
  echo "Have a nice weekend!";
  }
elseif ($d=="Sun")
  {
  echo "Have a nice Sunday!";
  }
else
  {
  echo "Have a nice day!";
  }
?>
In MaxScript we use // and * for one-line comments. Comment blocks are not supported (each comment ends invariably at the end of line)
<%
//-- In MaxScript you can use the concatenator | (vertical bar) for concatenating string.
txt1 = "Hello World!"
txt2 = "What a nice day!"
PRINT txt1 | " " | $txt2

//-- if statement
d=date()
if cdow(d) = "Friday"
   PRINT "Have a nice weekend!"
endif

//-- if..else..elseif construct
d=cdow(date())
if d = "Friday"
   ? "Have a nice weekend!"
elseif d = "Sun"
   ? "Have a nice sunday"
else
   ? "Have a nice day"
endif
%>

Branching control structures

$x=1;
switch ($x)
{
case 1:
  echo "Number 1";
  break;
case 2:
  echo "Number 2";
  break;
case 3:
  echo "Number 3";
  break;
default:
  echo "No number between 1 and 3";
}
x = 1
do case
   case x = 1
      ? "number 1"
   case x = 2
      ? "number 1"
   case x = 3
      ? "number 1"
   otherwise
      ? "No number between 1 and 3"
endcase
This construct can be used also to check different variables of different type with different conditions at the same time. First that matches causes the immediate end of verification.

Loops

In both languages we have the following looping statements: While loop
$i=1;
while($i<=5)
  {
  echo "The number is " . $i . "";
  $i++;
  }
i = 1
do while (i<=5)
   ? "The number is " | i
   i++
enddo

For loop
for ($i=1; $i<=5; $i++)
  {
  echo "The number is " . $i . "";
  }
for i=1 to 5 step 1
   ? "The number is " | i
next

Foreach loop
$x=array("one","two","three");
foreach ($x as $value)
  {
  echo $value . "";
  }
ainit("x","one","two","three")
for n=1 to len(x)
   value := x[n]
   print value
next
Note that this code was written specifically to resemble PHP: MaxScript can print directly to the elements without going through the variable value

Beyond Hello World

So far we introduced MaxScript by using the classic "Hello World" example.
Of course MaxScript is more than an abbreviator for HTML. It is a fully functional server-side scripting language, that takes you far beyond HTML (and Javascript) could ever take you.
For example see this code:
DBFree web pages can be made <b><%=popMsg('Hello World! Here is <%=fulldate()%>',"active")%></b>   
And its result:

DBFree web pages can be made active.
They may also support user interaction and read and write into databases depending by circumstances.
But - overall - DBFree's implementation of MaxScript is not for general purpose but it is specializing for Web Applications:

USe the in-place editing feature

See this code:
<iframe 
style="width:100%;height:600px;" 
src="http://dbfree.org/examples/dbbrowse.msp?VTABLE=customers&VACTION=LIST">
</iframe>
And its results:

DBFree Modular Pages Model

DBFree is a (large) superset of the standard MaxScript set of functions (while the commands remains substantially the same).
DBFree scripts (the web pages) can contain any valid MaxScript statements, but best practice is to follow the modular pages model with its various templates. Standard pages of DBFree are highly modular: the code can be taken from repository and used as is straight in the page.
There are many templates to accomodate different programming styles, while the modula page structure is almost identical for all of them.
Web Page structure

Classic CGI

Hybrid mode


DBFree Web Application standard structure



DBFree Web App Data Feeder model



For more informations about DBFree you may consult other pages of this site, starting from here.
Back to top Home Page About DBFree