this documentation is work-in-progress, edit here (md).

Examples

sections in this chapter:
server-script
sophisticated-templates
fizzbuzz

In this chapter, I’ll try to list some example programs to give you an idea how xoscript works.

Server script

This is how a typical script on server might look:

#!/bin/xo
Server init.
# I prefer a central loader that loads
# webtools.xo and template.xo
Program use: ['app/libs/loader.xo'].
# create a new document
>> web-document := Web-Document new.
>> tpl := Template new: (
File new: ['app/templates/greeting.tpl'], read
).
# cut the message section
>> message := tpl cut: ['message'].
# add a message (will be encoded properly to avoid xss)
message greeting: ['hello world!'].
# paste the element at the designated slot
tpl paste: message at: ['messages'].
# output including http headers
web-document out: tpl.

This would render a HTML document with a hello world message. As you can see, this uses a very strict separation between HTML and presentation logic (using the default template engine), so a template developer can update the template without having to adjust any code.

You can see the result here:

https://xoscript.com/test.xo

Sophisticated Templates

This is an example of how you make a select in a form:

>> htmlstr := ['
<html>
<body>
<form>
<!-- cut:products -->
<select name="products">
<!-- cut:product -->
<option value="<!-- slot:value -->">
<!-- slot:name -->
</option>
<!-- /cut:product -->
<!-- paste:product -->
</select>
<!-- /cut:products -->
<!-- paste:products -->
</form>
</body>
</html>
'].
>> tpl := Template new: htmlstr.
>> menu := List new ; ['pizza'] ; ['pasta'].
>> products := tpl cut: ['products'], copy.
>> product := products cut: ['product'], copy.
menu each: { :n :item
products paste: (
product copy
value: n,
name: item
) at: ['product'].
}.
tpl paste: products at: ['products'].

FizzBuzz

This is a standard fizzbuzz example:

{ :i
(i = 0) continue.
(i % 15 = 0) true: {
Out write: ['FizzBuzz'], stop.
}, continue.
(i % 3 = 0) true: {
Out write: ['Fizz'], stop.
}, continue.
(i % 5 = 0) true: {
Out write: ['Buzz'], stop.
}, continue.
Out write: i, stop.
} * 101.

Output:

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
etc...

More examples will follow…