ONO Code Examples

On this page you'll find some very easy examples of how ONO modules and libraries can be used.

Please note that ONO can do much more, from manipulating simple data and files (via ONO::Lib) to building highly complex community platforms (via ONO::ToolBox and ONO::FW for example). Make sure to check out the documentation to see what's possible.

Hello World!

This example will generate a simple web page that just says "Hello World!".

In order to do so, we'll need to use the ONO::Render module. Please note that you'll need to specify the path to ONO (/ono/sys) using "use lib" - unlike most CPAN modules, the ONO package is stored locally under your document root. You may, of course, also circumvent this, using a symlink for example.

#!/usr/bin/perl
use strict;
use lib "$ENV{'DOCUMENT_ROOT'}/ono/sys";
use ONO::Render;
print ONO::Render->render_simple("Hello World!");

Hello World, more advanced...

In the following example, we'll use separate modules provided by ONO::Render, this will allow for greater flexibility later on.

Note that open(), head(), body() and close() are all required, the HTML code will be broken if one of those is missing.

#!/usr/bin/perl
use strict;
use lib "$ENV{'DOCUMENT_ROOT'}/ono/sys";
use ONO::Render;
print ONO::Render->open();
print ONO::Render->head();
print ONO::Render->body();
print "Hello World!";
print ONO::Render->close();

Hello World, even more advanced...

In this example, we'll retrieve all GET or POST input variables (via ONO:ToolBox::Input, all input data will be accessible via the %vars hash), we'll detect the website language (by analyzing the domain name via ONO:ToolBox::DomainLang), and we'll generate a cookie that will be valid for 3 months (via ONO:Lib::Web::Cookie).

We'll send the cookie and the website language info to be web browser via open(), and we'll indicate a page generator ('MyName') via head(). Furthermore, we'll load ONO/CSS and ONO/JSS via cssjs() to get the "ONO look and feel", and we'll set a background color via body();

#!/usr/bin/perl
use strict;
use lib "$ENV{'DOCUMENT_ROOT'}/ono/sys";
use ONO::Render;
use ONO::ToolBox::Input;
use ONO::ToolBox::DomainLang;
use ONO::Lib::Web::Cookie;
my ($vars_ref,%vars) = ONO::ToolBox::Input->get;
my $lang = ONO::ToolBox::DomainLang->get;
my $cookie = ONO::Lib::Web::Cookie->make('name','value','3m');
print ONO::Render->open($cookie,$lang);
print ONO::Render->head('MyName');
print ONO::Render->cssjs('','/site-data/demo/my-own-css.css');
print ONO::Render->title_key_desc_robots($title,$keywords,$description,$robots);
print ONO::Render->body("background:#333333");
print "Hello World!";
print ONO::Render->close();

ONO::IO

ONO::IO offers quick and easy access to files and directories within your document path. Most operations, such as reading, writing, moving or deleting files and directories, only require a single line of code. Most ONO::IO calls also respect special use cases, such as (re)moving thumbnails, previews, and meta information for example.

Traditional Perl I/O is no longer requied.

foreach my $file (ONO::IO->ls($dir)) {
   ...
}
$data = ONO::IO->load($file);
foreach my $line (ONO::IO->list($file)) {
   ...
}
ONO::IO->store($file,$data);
ONO::IO->append($file,$data);
ONO::IO->mkdir($dir);
ONO::IO->mkpath($path);
ONO::IO->cp($original,$copy);
ONO::IO->mv($old,$new);
ONO::IO->rm($file);
ONO::IO->rmdir($dir);

ONO::DB

ONO::DB offers quick and easy access to SQL databases, making it very easy to read, add, or update database records.

ONO::DB can be used with both MySQL and Microsoft SQL Server. The standard SQL syntax is MySQL, the Microsoft SQL syntax is automatically adjusted by ONO::DB in most cases.

$db = ONO::DB->connect($database);
ONO::DB->command($db,$sql_command);
foreach my $line (ONO::DB->select($db,$table,$where,$order)) {
   my @col = ONO::DB->readcols($line);
   ...
}

Community / Platform Header, using ONO::ToolBox::Init and / or ONO::FW::Community::Init

ONO::ToolBox::Init can return input variables (simple and quoted), site language and the basic language kit (BLK) using one single call, performing cookie-based user authentication at the same time, which is usually enough to get a simple website or platform running.

ONO:::FW::Community::Init returns some more stuff, like the $vpath (absolute document path), $domain, date and time, as well as precoded HTML sniplets ($script_url is the path to the current script including basic variables, $action_url is the path to the current script for use in forms (action), while $form contains basic HTML form data.

Note that ONO:::FW::Community::Init does NOT perform user authentication, while ONO::ToolBox::Init. In many cases, it may be useful to use both at a time.

#!/usr/bin/perl
use strict;
use lib "$ENV{'DOCUMENT_ROOT'}/ono/sys";
use ONO::ToolBox::Init
my $db = ONO::DB->connect("database_name");
my $community = "community_name";
my ($vars_ref,$sql_ref,$lang,$BLK_ref) = ONO:::ToolBox::Init->header($db,$community,"","");
my (
       $vpath,
       $lang,$domain,$community,
       $sec,$min,$hour,
       $mday,$mon,$year,
       $wday,$yday,$timestamp,
       $script_url,$action_url,$form
   ) = ONO:::FW::Community::Init->getvar($vars_ref);
my %vars = %$vars_ref;
my %BLK = %$BLK_ref;
my $HTML = qq~
       # HTML demo code:
       today is $year/$mon/$mday
       <a href="$script_url">link to myself</a>
       <$form>
          <input type="submit" name="form_submit" value="submit" class="button_green">
       </form>
       ~;

ONO/CSS

ONO CSS offers many predefined UI classes, so that it's very easy to create websites and platforms offering a consistent user experience.

<a href="" class="button_green">I'm a green button!</a>
<div class="box_paper">I'm a white, textured box with rounded corners.</div>
<span class="large bold italic">some large, bold italic text...</span>

Here is the same code, this time interpreted by your web browser:

I'm a white, textured box with rounded corners.
some large, bold italic text...

ONO/JS

ONO/JS offers a number of JS calls that make traditional DOM syntax (e.g. "document.getElementById...) mostly obsolete. ONO/JS has been optimized for dense, compact and easy to read JS code.

<script>
var value = onojs_getvalue('id');
onojs_setvalue('id',value);
onojs_hide('id');
onojs_block('id');
onojs_inline('id');
</script>

BLK - Basic Language Kit

The ONO BLK generates a hash containing a number of words (as well as some small expressions) and buttons in one of four languages (en - English, de - German, fr - French, lu - Luxembourgish).

Luxembourgish is part of the BLK for historic reasons, as ONO also powers oli.lu, which is the primary education portal in Luxembourg. Note that ONO is using "lu" as language code for luxembourgish, not "lb".

my ($BLK_ref) = ONO:::Lib::Lang::BLK->get($lang,$vars_ref);
my %BLK = %$BLK_ref;
# $BLK{'hello_world'} will say "Hello World"
# $BLK{'submit_save'} will display a "save" button, including a timeout feature