Programming Cws

Hello World !

Open a file with some text editor.
Below is the code : 1: <#cws
2: @print("Hello World !")
3: #>
1: Opening tag. In a file, you always have to open this tag before Cws instructions
2: A string is always surrounded by " ". This line will output the string. We'll see functions later.
! Before release 1.0 final, to output something, you have to put this on a simple line : 2: "Hello World !" instead of using function print which does not exist.
3: Ending tag. If you dn't put it, Cws will output an error message.

Result :
Hello World !

Note that you also may train with the command-line tool, you just don't have to open and close <#cws tags.

String concatenation

1: <#cws
2: @print("Hello" + " " + "World !")
3: #>
1: Opening tag
2: This line of code will concatenate the three strings Hello, [space] and World ! It will then output "Hello World !"
3: Ending tag

Result:
Hello World !

Variables

A variable is prefixed by a '$' char, like in perl. They are case sensitive. The first char after the '$' has to be either a letter [a-zA-Z] or an underscore [_], the next ones can be letters, underscores and digits.
In the current version (1.0), variables are only strings. It means that if you try to put a number, it will be translated into a string. 1: <#cws
2: $Home = "/home/groups/c/ce/celsiusws"
3: "With correct case: " + $Home
4: "With incorrect case: " + $home
5: #>
2: Assigns value "/home/groups/c/ce/celsiusws" to variable name Home
3: Gets the value of a var already set, Home
4: Gets the value of an unset var, home (because it's case sensitive)

Result:
With correct case: /home/groups/c/ce/celsiusws
With incorrect case:

Functions

Functions are prefixed with a '@'.
Function arguments are separated with a comma and may be whatever you put within one code line. (except if/while/for...)
To get a whole list of available functions, please got to functions page 1: <#cws
2: @include("footer.cws")
3: #>
2: reads and, if t's a cws file, parses it, then outputs it at the screen.

Result:
Depends on the content of footer.cws file

Here's another piece of code, illustrating composed functions 1: <#cws
2: @include(@securize($GET_page))
3: #>
2: This will get the value of the var GET_page (we'll see the get vars at the nex chapter). Then this value will be securized ('/' will be deleted), and the returned string will be passed as argument for function include.
If GET_page is "../file.html", then it will include "..file.html". If cws can't open this file, it will return an empty string ad an error message.

Result:
Content of file named by GET_page's value, with suppressed '/'.

Get and Cookie vars

Consider this url: http://celsiusws.sf.net/index.cws?page=doc&id=5
Get vars is the part after the '?'
At startup, and only if Cws is used as cgi, get vars will be evaluated, following the data provided after the '?'.
It's fairly simple and securised with Cws. In your scripts, you will be able to access these >variables with $GET_[subname], where [subname] is the name of the var you need. For "page=doc&id=5", $GET_page will contain "doc" and $GET_id will contain "5" URL = http://celsiusws.sf.net/index.cws?page=doc
1: <#cws
2: $data = @if_not_null($GET_page, @include("data/" + @securize($GET_page)),
@include("data/index"))
3: @if_null($data, $data = @include("data/404error"))
4: $data
5: #>
2: Initialises the var "data" and :
- if GET_page is set, includes this path after securising it
- if not set, includes file "data/index"
and assigns this value to "data"
3: If the requested file does not exist, and then "data" does not contain anything, will assign the contain of file "data/404error" to data
4: Outputs the content of "data"

In this script, we used the "if_null" and "if_not_null" functions, which are simple conditions functions, for in the 1.0 version we don't have other conditions. See next chapter ;)

Cookie vars are available with $COOKIE_name_of_cookie. A function "setcookie" is available to set them.

Conditions

In the current version, 1.0, there are no conditions, except what we've seen just before. I think it should be ok for version 1.1
These conditions are functions, so they have to be prefixed with a '@'.
if_null
if_not_null
They take two or three arguments as input.
The first argument is a variable. 1: <#cws
2: $foo = "bar"
3: @if_not_null($foo, "foo var is not null", "foo var is null")
4: @if_null($foo, "foo var is null", "foo var is not null")
5: @if_not_null($GET_request, @include(@securize($GET_request)))
6: #>
3: this will check whether $foo is empty or not. If it is, the third argument will be evaluated and returned, otherwise, the second one will be evaluated and returned.
Consider this functions like this : if_not_null($condition, true, false)
4: This function is precisely the opposite of the first one. if $foo is null, the it will evaluate the second argument, and vice-versa
5: You also may put only two args to one of these functions, it this case, if result is false, it won't return anything.

Result:
foo var is not null
foo var is not null
[depends on GET_request's value]

User Functions

There are currently no user-defined functions in Cws, but you can simulate them.
We have seen before that you can store the content of an included file into a variable.
Consider this: File : myFunction.cws
1: <#cws
2: @print(@replace($myFunction_toprint, "e", "a"))
3: #>
File: index.cws
1: <#cws
2: $myFunction_toprint = "Hello"
3: $data = @include("myFunction.cws")
4: @print($data)
5: #>
The file index.cws will include and parse myFunction.cws file, which will return Hallo.
Note that files do not HAVE to return strings, they also can modify variables.