Sunteți pe pagina 1din 10

Ring Documentation, Release 1.5.

Usage:
load "mylanguage.ring"

MyLanguage.RunString('
print "Hello, World!"
')

Output:
Hello, World!

Example (2)
File: iwantwindow.ring
DefineNaturalCommand.SyntaxIsCommand([
:Package = "MyLanguage.Natural",
:Command = "i want window",
:Function = func {
See "Command: I want window" + nl
}
])

Usage:
load "mylanguage.ring"

MyLanguage.RunString('
i want window
')

Output:
Command: I want window

Example (3)
File: windowtitleis.ring
DefineNaturalCommand.SyntaxIsCommandString([
:Package = "MyLanguage.Natural",
:Command = "window title is",
:Function = func {
See "Command: Window title is " + Expr(1) + nl
}
])

Usage:
load "mylanguage.ring"

MyLanguage.RunString('
I want window and the window title is "Hello World"
')

Output:
Command: I want window
Command: Window title is Hello World

46.2. Defining Commands 365


Ring Documentation, Release 1.5.4

46.3 Natural Library - Operators

In the next example we uses the Count command without using operators
load "mylanguage.ring"

MyLanguage.RunString("
Hello
Count 1 5
Count 5 1
")

We can add more description


load "mylanguage.ring"

MyLanguage.RunString("
Hello, Please Count from 1 to 5 then count from 5 to 1
")

Also we can use operators like “(” and ”)” around the instruction
load "mylanguage.ring"

MyLanguage {
SetOperators("()")
RunString("
Here we will play and will try something
that looks like Lisp Syntax
(count (count 1 5) (count 20 15))
Just for fun!
")
}

46.4 Defining commands using classes

This section is related to the implementation details.


When we define new command, Each command is defined by the Natural Library as a class.
We have the choice to define commands using the simple interface provided by the DefineNaturalCommand object or
by defining new class as in the next examples.
If we used DefineNaturalCommand (More Simple), The class will be defined during the runtime.
File: hello.ring
Package MyLanguage.Natural

class Hello

func AddAttributes_Hello
AddAttribute(self,:hello)

func GetHello
See "Hello, Sir!" + nl + nl

File: count.ring

46.3. Natural Library - Operators 366


Ring Documentation, Release 1.5.4

Package MyLanguage.Natural

class Count

func Getcount
StartCommand()
CommandData()[:name] = :Count
CommandData()[:nExpr] = 0
CommandData()[:aExpr] = []

func BraceExprEval_Count nValue


if isCommand() and CommandData()[:name] = :Count {
if isNumber(nValue) {
CommandData()[:nExpr]++
CommandData()[:aExpr] + nValue
if CommandData()[:nExpr] = 2 {
Count_Execute()
}
}
}

func AddAttributes_Count
AddAttribute(self,:count)

func Count_Execute
if not isattribute(self,:count_times) {
AddAttribute(self,:count_times)
Count_Times = 0
}
if Expr(1) > Expr(2) {
nStep = -1
else
nStep = 1
}
if Count_Times = 0 {
see nl+"The Numbers!" + nl
Count_Times++
else
see nl + "I will count Again!" +nl
}
for x = Expr(1) to Expr(2) step nStep {
see nl+x+nl
}
CommandReturn(fabs(Expr(1)-Expr(2))+1)

46.4. Defining commands using classes 367


CHAPTER

FORTYSEVEN

WEB DEVELOPMENT (CGI LIBRARY)

In this chapter we will learn about developing Web applications using a CGI Library written in the Ring language.

47.1 Configure the Apache web server

We can use Ring with any web server that support CGI. In this section we will learn about using Ring with the Apache
HTTP Server.
You can download Apache from : http://httpd.apache.org/
Or you can get it included with other projects like
XAMPP : https://www.apachefriends.org/download.html
Install then open the file:
xampp\apache\conf\httpd.conf

search for
<Directory />

Then after it add


Options FollowSymLinks +ExecCGI

So we have
<Directory />
Options FollowSymLinks +ExecCGI

Search for the next line and be sure that it’s not commented
LoadModule cgi_module modules/mod_cgi.so

Search for : AddHandler cgi-script


Then add ”.ring” to the supported cgi extensions
Example
AddHandler cgi-script .cgi .ring

Example
AddHandler cgi-script .cgi .pl .asp .ring

368
Ring Documentation, Release 1.5.4

Run/Start the server


Create your web applications in a directory supported by the web server.
Example:
Apache2.2\htdocs\mywebapplicationfolder

Example:
xampp\htdocs\mywebapplicationfolder

Inside the source code file (*.ring), Add this line


#!c:\ring\bin\ring.exe -cgi

Note: Change the previous line based on the path to ring.exe in your machine

47.2 Ring CGI Hello World Program

The next program is the Hello World program


#!c:\ring\bin\ring.exe -cgi

See "content-type : text/html" +nl+nl+


"Hello World!" + nl

47.3 Hello World Program using the Web Library

We can use the web library to write CGI Web applications quickly
Example (1) :
#!c:\ring\bin\ring.exe -cgi

Load "weblib.ring"

Import System.Web

New Page
{
Text("Hello World!")
}

Example (2) :
#!c:\ring\bin\ring.exe -cgi

Load "weblib.ring"

Import System.Web

WebPage()
{
Text("Hello World!")
}

47.2. Ring CGI Hello World Program 369


Ring Documentation, Release 1.5.4

Tip: the difference between ex. 1 and ex. 2 is using WebPage() function to return the page object instead of creating
the object using new statement.

47.4 Web Library Features

The next features are provided by the Web library to quickly create web applications.
• Generate HTML pages using functions
• Generate HTML pages using objects
• HTTP Get
• HTTP Post
• Files Upload
• URL Encode
• Templates
• CRUD MVC Sample
• Users Logic & Registration Sample

47.5 HTTP Get Example

The Page User Interface


#!c:\ring\bin\ring.exe -cgi
Load "weblib.ring"
Import System.Web
New Page
{
Title = "Test HTTP Get"
divstart([ :style = StyleSizeFull() ] )
boxstart()
text( "Test HTTP GET" )
newline()
boxend()
divstart([ :style = Styledivcenter("600px","550px") +
StyleGradient(21) ])
divstart([:style = stylefloatleft() + stylesize("100px","100%") +
stylecolor("black") + stylegradient(58)])
formstart("ex5.ring")
tablestart([ :style = stylesize("65%","90%") +
stylemarginleft("35%") +
stylemargintop("30%") ])
rowstart([])
cellstart([])
text ( "Name : " )
cellend()
cellstart([])
cTextboxStyle = StyleMarginLeft("5%") +
StyleWidth("250px") +
StyleColor("black") +

47.4. Web Library Features 370


Ring Documentation, Release 1.5.4

StyleBackColor("white")
textbox([ :name = "Name", :style = cTextboxStyle ] )
cellend()
rowend()
rowstart([])
cellstart([])
text ( "Address : " )
cellend()
cellstart([])
textbox([ :name = "Address", :style = cTextboxStyle] )
cellend()
rowend()
rowstart([])
cellstart([])
text ( "Phone : " )
cellend()
cellstart([])
textbox([ :name = "Phone", :style = cTextboxStyle ])
cellend()
rowend()
rowstart([])
cellstart([])
text ( "Age : " )
cellend()
cellstart([])
textbox([ :name = "Age", :style = cTextboxStyle ])
cellend()
rowend()
rowstart([])
cellstart([])
text ( "City: " )
cellend()
cellstart([])
listbox([ :name = "City", :items = ["Cairo","Riyadh","Jeddah"],
:style = stylemarginleft("5%") + stylewidth("400px") ] )
cellend()
rowend()
rowstart([])
cellstart([])
text ( "Country : " )
cellend()
cellstart([])
combobox([ :name = "Country",
:items = ["Egypt","Saudi Arabia","USA"],
:style = stylemarginleft("5%") +
stylewidth("400px")+
stylecolor("black")+
stylebackcolor("white")+
stylefontsize("14px") ])
cellend()
rowend()
rowstart([])
cellstart([])
text ( "Note : " )
cellend()
cellstart([])
editbox([ :name = "Notes",
:style = stylemarginleft("5%") +

47.5. HTTP Get Example 371


Ring Documentation, Release 1.5.4

stylesize("400px","100px")+
stylecolor("black")+
stylebackcolor("white") ,
:value = "write comments here..." ] )
cellend()
rowend()
rowstart([])
cellstart([])
cellend()
cellstart([])
submit([ :value = "Send" , :Style = stylemarginleft("5%") ])
cellend()
rowend()
tableend()
formend()
divend()
divend()
divend()
}

Screen Shot:

47.5. HTTP Get Example 372


Ring Documentation, Release 1.5.4

The Response
#!c:\ring\bin\ring.exe -cgi
Load "weblib.ring"
Import System.Web
New Page
{
divstart([ :style = styledivcenter("800px","500px") ])
boxstart()
text ( "HTTP GET Response" ) newline()
boxend()
divstart([ :style = stylefloatleft()+stylewidth("10%")+

47.5. HTTP Get Example 373


Ring Documentation, Release 1.5.4

stylecolor("black")+stylegradient(58) ])
newline()
text ( "Name : " )
newline() newline()
text ( "Address : " )
newline() newline()
text ( "Phone : " )
newline() newline()
text ( "Age : " )
newline() newline()
text ( "City : " )
newline() newline()
text ( "Country : " )
newline() newline()
text ( "Note : " )
newline() newline()
divend()
divstart([ :style = stylefloatleft()+stylewidth("90%")+
stylecolor("black")+stylegradient(47) ])
divstart([ :style = stylefloatleft() + stylewidth("1%") ])
newline()
divend()
divstart([ :style = stylefloatleft() + stylewidth("95%") ])
newline()
text ( aPageVars["Name"] )
newline() newline()
text ( aPageVars["Address"] )
newline() newline()
text ( aPageVars["Phone"] )
newline() newline()
text ( aPageVars["Age"] )
newline() newline()
text ( aPageVars["City"] )
newline() newline()
text (aPageVars["Country"] )
newline() newline()
text ( aPageVars["Notes"] )
newline() newline()
divend()
divend()
divend()
}

Screen Shot:

47.5. HTTP Get Example 374

S-ar putea să vă placă și