<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>lua nova &#187; orbit</title>
	<atom:link href="http://www.luanova.org/tag/orbit/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.luanova.org</link>
	<description>welcome to the moon</description>
	<lastBuildDate>Thu, 23 Sep 2010 12:46:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Setting Orbit to use Apache2</title>
		<link>http://www.luanova.org/setting-orbit-to-use-apache2/</link>
		<comments>http://www.luanova.org/setting-orbit-to-use-apache2/#comments</comments>
		<pubDate>Wed, 08 Sep 2010 13:44:23 +0000</pubDate>
		<dc:creator>ryanpusztai</dc:creator>
				<category><![CDATA[Lua]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[orbit]]></category>
		<category><![CDATA[web frameworks]]></category>
		<category><![CDATA[webserver]]></category>

		<guid isPermaLink="false">http://luanova.org/?p=70</guid>
		<description><![CDATA[This article will cover how to install Lua Orbit on an Ubuntu 8.04+ server. This will explain how to setup Apache2 and Xavante on the server. This allows for production code to run through Apache2 and then develop using Xavante. To hook Orbit to Apache2 we will be using FastCGI for the best performance. NOTE: [...]]]></description>
			<content:encoded><![CDATA[<p>This article will cover how to install <a href="http://keplerproject.github.com/orbit/index.html">Lua Orbit</a> on an Ubuntu 8.04+ server. This will explain how to setup Apache2 and Xavante on the server. This allows for production code to run through Apache2 and then develop using Xavante. To hook Orbit to Apache2 we will be using <strong>FastCGI</strong> for the best performance.</p>

<p>NOTE: This is just one way to configure your server to run Orbit applications; it does require that you have root access.  There are also local sandboxed methods which we will cover in a future article.</p>

<h2>Installing Apache</h2>

<p>Connect to your server. Use ssh or however your host instructs you, and install Apache2 and support modules:</p>

<pre><code>$ sudo apt-get install apache2 libapache2-mod-fcgid libfcgi-dev build-essential
</code></pre>

<p>Enable required Apache2 modules:</p>

<pre><code> $ sudo a2enmod rewrite
 $ sudo a2enmod fcgid
 $ sudo /etc/init.d/apache2 force-reload
</code></pre>

<h2>Install LuaRocks</h2>

<p>If you are running a fresh distribution (Ubuntu Lucid or Debian Squeeze) then you can get a suitable LuaRocks from the repositories &#8211; otherwise you must install it directly, as described below. If unsure, look at ‘aptitude show luarocks’ &#8211; the luarocks version must be 2.0.x.  This will also make sure you have Lua and its development library:</p>

<pre><code>$ sudo apt-get install luarocks
</code></pre>

<p>Otherwise, you have to do it by hand:</p>

<p>Install Lua, using the appropriate methods for your system.</p>

<pre><code>$ sudo apt-get install lua5.1 liblua5.1-0-dev
</code></pre>

<p>Download and unpack the LuaRocks tarball (http://luarocks.org/releases).</p>

<pre><code>$ wget http://luarocks.org/releases/luarocks-2.0.2.tar.gz
$ tar xvfz luarocks-2.0.2.tar.gz
</code></pre>

<p>Change directories to the new extracted LuaRocks directory and configure. (This will attempt to detect your installation of Lua. If you get any error messages, the main problem will probably be the location of the Lua include files &#8211; this uses the standard Debian/Ubuntu locations.</p>

<pre><code>$ cd luarocks-2.0.2
$ ./configure --with-lua-include=/usr/include/lua5.1"
$ make
$ make install
</code></pre>

<p>Install <strong>WSAPI</strong>, <strong>fcgi</strong>, <strong>Orbit</strong>, and <strong>Xavante</strong></p>

<pre><code>$ sudo luarocks install orbit
$ sudo luarocks install wsapi-xavante
$ sudo luarocks install wsapi-fcgi
</code></pre>

<p>[Optional] Install support libraries; <strong>LuaSQL</strong>,<strong>Sqlite3</strong> and <strong>md5</strong>:</p>

<pre><code>$ sudo apt-get install libmysqlclient15-dev libsqlite3-dev
$ sudo luarocks install md5
$ sudo luarocks install luasql-sqlite3
$ sudo luarocks install luasql-mysql MYSQL_INCDIR=/usr/include/mysql
</code></pre>

<h2>Setting up Apache2</h2>

<p>Edit the site config file with your favourite editor. (default site is named ‘default’)</p>

<pre><code>$ sudo joe /etc/apache2/sites-available/default
</code></pre>

<p>Add this following section below the <code>&lt;Directory /var/www/&gt;</code> section of the config file. If this section has a ‘AllowOverride None’ then you need to change the ‘None’ to ‘All’ so that the .htaccess file can override the configuration locally.</p>

<pre><code>&lt;IfModule mod_fcgid.c&gt;
    AddHandler fcgid-script .lua
    AddHandler fcgid-script .ws
    AddHandler fcgid-script .op
    FCGIWrapper "/usr/local/bin/wsapi.fcgi" .ws
    FCGIWrapper "/usr/local/bin/wsapi.fcgi" .lua
    FCGIWrapper "/usr/local/bin/op.fcgi" .op
    #FCGIServer "/usr/local/bin/wsapi.fcgi" -idle-timeout 60 -processes 1
    #IdleTimeout 60
    #ProcessLifeTime 60
&lt;/IfModule&gt;
</code></pre>

<p>Restart the server.</p>

<p>To enable your application you need to add <strong>+ExecCGI</strong> to an .htaccess file in the root of your Orbit application &#8211; in this case, /var/www.</p>

<pre><code>Options +ExecCGI
DirectoryIndex index.ws
</code></pre>

<p>Here is a simple example of a “Hello World” orbit application. Put this in /var/www/index.ws</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">    #<span style="color: #66cc66;">!/</span>usr<span style="color: #66cc66;">/</span>bin<span style="color: #66cc66;">/</span>env wsapi.fcgi
&nbsp;
    <span style="color: #b1b100;">require</span><span style="color: #ff0000;">&quot;orbit&quot;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">-- Orbit applications are usually modules,</span>
    <span style="color: #808080; font-style: italic;">-- orbit.new does the necessary initialization</span>
&nbsp;
    module<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;hello&quot;</span>, package.seeall, orbit.new <span style="color: #66cc66;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">-- These are the controllers, each receives a web object</span>
    <span style="color: #808080; font-style: italic;">-- that is the request/response, plus any extra captures from the</span>
    <span style="color: #808080; font-style: italic;">-- dispatch pattern. The controller sets any extra headers and/or</span>
    <span style="color: #808080; font-style: italic;">-- the status if it's not 200, then return the response. It's</span>
    <span style="color: #808080; font-style: italic;">-- good form to delegate the generation of the response to a view</span>
    <span style="color: #808080; font-style: italic;">-- function</span>
&nbsp;
    <span style="color: #b1b100;">function</span> index<span style="color: #66cc66;">&#40;</span> web <span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">return</span> render_index<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">end</span>
&nbsp;
    <span style="color: #b1b100;">function</span> say<span style="color: #66cc66;">&#40;</span> web, name <span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">return</span> render_say<span style="color: #66cc66;">&#40;</span> web, name <span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">end</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">-- Builds the application's dispatch table, you can</span>
    <span style="color: #808080; font-style: italic;">-- pass multiple patterns, and any captures get passed to</span>
    <span style="color: #808080; font-style: italic;">-- the controller</span>
&nbsp;
    hello:dispatch_get<span style="color: #66cc66;">&#40;</span> index, <span style="color: #ff0000;">&quot;/&quot;</span>, <span style="color: #ff0000;">&quot;/index&quot;</span> <span style="color: #66cc66;">&#41;</span>
    hello:dispatch_get<span style="color: #66cc66;">&#40;</span> say, <span style="color: #ff0000;">&quot;/say/(%a+)&quot;</span> <span style="color: #66cc66;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">-- These are the view functions referenced by the controllers.</span>
    <span style="color: #808080; font-style: italic;">-- orbit.htmlify does through the functions in the table passed</span>
    <span style="color: #808080; font-style: italic;">-- as the first argument and tries to match their name against</span>
    <span style="color: #808080; font-style: italic;">-- the provided patterns (with an implicit ^ and $ surrounding</span>
    <span style="color: #808080; font-style: italic;">-- the pattern. Each function that matches gets an environment</span>
    <span style="color: #808080; font-style: italic;">-- where HTML functions are created on demand. They either take</span>
    <span style="color: #808080; font-style: italic;">-- nil (empty tags), a string (text between opening and</span>
    <span style="color: #808080; font-style: italic;">-- closing tags), or a table with attributes and a list</span>
    <span style="color: #808080; font-style: italic;">-- of strings that will be the text. The indexing the</span>
    <span style="color: #808080; font-style: italic;">-- functions adds a class attribute to the tag. Functions</span>
    <span style="color: #808080; font-style: italic;">-- are cached.</span>
    <span style="color: #808080; font-style: italic;">--</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">-- This is a convenience function for the common parts of a page</span>
&nbsp;
    <span style="color: #b1b100;">function</span> render_layout<span style="color: #66cc66;">&#40;</span> inner_html <span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">return</span> html
        <span style="color: #66cc66;">&#123;</span>
            head<span style="color: #66cc66;">&#123;</span> title<span style="color: #ff0000;">&quot;Hello&quot;</span> <span style="color: #66cc66;">&#125;</span>,
            body<span style="color: #66cc66;">&#123;</span> inner_html <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #b1b100;">end</span>
&nbsp;
    <span style="color: #b1b100;">function</span> render_hello<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">return</span> p.hello <span style="color: #ff0000;">&quot;Hello World!&quot;</span>
    <span style="color: #b1b100;">end</span>
&nbsp;
    <span style="color: #b1b100;">function</span> render_index<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">return</span> render_layout<span style="color: #66cc66;">&#40;</span> render_hello<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">end</span>
&nbsp;
    <span style="color: #b1b100;">function</span> render_say<span style="color: #66cc66;">&#40;</span> web, name <span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">return</span> render_layout<span style="color: #66cc66;">&#40;</span> render_hello<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> .. p.hello<span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#40;</span>
    eb.input.greeting <span style="color: #b1b100;">or</span> <span style="color: #ff0000;">&quot;Hello &quot;</span> <span style="color: #66cc66;">&#41;</span> .. name .. <span style="color: #ff0000;">&quot;!&quot;</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">end</span>
&nbsp;
    orbit.htmlify<span style="color: #66cc66;">&#40;</span> hello, <span style="color: #ff0000;">&quot;render_.+&quot;</span> <span style="color: #66cc66;">&#41;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> _M</pre></div></div>


<p>Now you should be able to launch your web browser and goto                http://hostname and you should see “Hello World!”</p>

<p>If you go to http://hostname/index.ws/say/yourname you should see:</p>

<pre><code>“Hello World!
Hello yourname!”
</code></pre>

<p>You are done!  You can now look at LuaNova&#8217;s <a href="http://luanova.org/orbit1-2/">Orbit Introduction</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.luanova.org/setting-orbit-to-use-apache2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>An introduction to Orbit</title>
		<link>http://www.luanova.org/orbit1-2/</link>
		<comments>http://www.luanova.org/orbit1-2/#comments</comments>
		<pubDate>Tue, 22 Dec 2009 16:57:41 +0000</pubDate>
		<dc:creator>stevejdonovan</dc:creator>
				<category><![CDATA[Lua]]></category>
		<category><![CDATA[Kepler]]></category>
		<category><![CDATA[orbit]]></category>
		<category><![CDATA[web frameworks]]></category>

		<guid isPermaLink="false">http://luanova.org/?p=41</guid>
		<description><![CDATA[Installing Orbit Orbit is a lightweight framework for Web applications written in Lua. Unlike Sputnik, Orbit gives you the basic tools for constructing applications built on WSAPI. If you want authentication, standard look-and-feel, wiki-like functionality, you are better off starting with Sputnik; but Orbit is a satisfying way to build web applications from scratch. The [...]]]></description>
			<content:encoded><![CDATA[<h2>Installing Orbit</h2>

<p><a href="http://orbit.luaforge.net/">Orbit</a> is a lightweight framework for Web applications written in Lua. Unlike <a href="http://luanova.org/sputnik/">Sputnik</a>, Orbit gives you the basic tools for constructing applications built on <a href="http://wsapi.luaforge.net/">WSAPI</a>.  If you want authentication, standard look-and-feel, wiki-like functionality, you are better off starting with Sputnik; but Orbit is a satisfying way to build web applications from scratch.</p>

<p>The easiest way to install Orbit on a Unix machine is using <a href="http://luarocks.org/">LuaRocks</a>, but here for a change I will assume that you already have <a href="http://luaforwindows.luaforge.net/">Lua for Windows</a> which already has most of the dependencies for Orbit.  Then download <a href="http://mysite.mweb.co.za/residents/sdonovan/lua/orbit-2.0.2-all.zip"> orbit-2.0.2-all.zip</a>, unzip it somewhere preserving folder names, and run  <strong>lua install-orbit.lua</strong> from this directory in a command prompt.</p>

<p>The examples in this tutorial are then in the <strong>orbit-2.0.2\samples\basic</strong> folder.</p>

<h2>The Basics</h2>

<p>The simplest Orbit application is one that serves up a single HTML page:</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">    <span style="color: #808080; font-style: italic;">-- basic1.lua</span>
    <span style="color: #b1b100;">require</span><span style="color: #ff0000;">&quot;orbit&quot;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">-- declaration</span>
    module<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;basic1&quot;</span>, package.seeall, orbit.new<span style="color: #66cc66;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">-- handler</span>
    <span style="color: #b1b100;">function</span> index<span style="color: #66cc66;">&#40;</span>web<span style="color: #66cc66;">&#41;</span>
      <span style="color: #b1b100;">return</span> render_index<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">end</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">-- dispatch</span>
    basic1:dispatch_get<span style="color: #66cc66;">&#40;</span>index, <span style="color: #ff0000;">&quot;/&quot;</span>, <span style="color: #ff0000;">&quot;/index&quot;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">-- render</span>
    <span style="color: #b1b100;">function</span> render_index<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span>
        <span style="color: #66cc66;">&lt;</span>head<span style="color: #66cc66;">&gt;&lt;/</span>head<span style="color: #66cc66;">&gt;</span>
        <span style="color: #66cc66;">&lt;</span>html<span style="color: #66cc66;">&gt;</span>
        <span style="color: #66cc66;">&lt;</span>h2<span style="color: #66cc66;">&gt;</span>Pretty Easy<span style="color: #66cc66;">!&lt;/</span>h2<span style="color: #66cc66;">&gt;</span>
        <span style="color: #66cc66;">&lt;/</span>html<span style="color: #66cc66;">&gt;</span>
        <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #b1b100;">end</span></pre></div></div>


<p>An Orbit application is a Lua module, declared in the usual way except for the extra &#8216;orbit.new&#8217;. Lua&#8217;s <strong>module</strong> function can take a number of extra arguments, which are all functions which modify the module table in some way: <strong>package.seeall</strong> makes the global environment accessible to the module, and <strong>orbit.new</strong> adds some extra methods to the new module (you may think of this as &#8216;inheritance&#8217;.)</p>

<p>Any HTML request is mapped onto handler functions &#8211; in this case, the result of doing a GET request on &#8216;/&#8217; or &#8216;/index&#8217;. <strong>index</strong> just asks <strong>render_index</strong> to actually generate the HTML for this particular page. You can then run this application using the default Xavante server:</p>

<pre><code>c:\basic&gt; orbit basic1.lua
Starting Orbit server at port 8080
</code></pre>

<p>And you can view the result by going to <strong>http://localhost:8080</strong> in your favourite browser.</p>

<p>Not perhaps very impressive, seeing as the result could be achieved by a simple static page, but the render function can return <em>any</em> dynamically created string. And this example will work with Xavante, FastCGI or CGI, because Orbit is a WSAPI application.</p>

<p>All the usual web request variables are parsed and available to you. The <strong>web</strong> variable returned from the handler contains a table <strong>GET</strong> which has all the parameters passed to the server from a <strong>GET</strong> request. Call this script with <strong>http://localhost:8080/?cat=felix&amp;dog=fido</strong> and you will see that <strong>cat</strong> and <strong>dog</strong> are fields of <strong>web.GET</strong>, appropriately translated from URL encoding.</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">    <span style="color: #808080; font-style: italic;">-- basic3.lua</span>
    <span style="color: #b1b100;">require</span><span style="color: #ff0000;">&quot;orbit&quot;</span>
&nbsp;
    module<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;basic3&quot;</span>, package.seeall, orbit.new<span style="color: #66cc66;">&#41;</span>
&nbsp;
    basic3:dispatch_get<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">function</span><span style="color: #66cc66;">&#40;</span>web<span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">local</span> ls <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>
        <span style="color: #b1b100;">for</span> k,v <span style="color: #b1b100;">in</span> <span style="color: #b1b100;">pairs</span><span style="color: #66cc66;">&#40;</span>web.GET<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">do</span>
            <span style="color: #b1b100;">table.insert</span><span style="color: #66cc66;">&#40;</span>ls,<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'&lt;li&gt;%s = %s&lt;/li&gt;'</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #b1b100;">format</span><span style="color: #66cc66;">&#40;</span>k,v<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">end</span>
        ls <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'&lt;ul&gt;'</span>..<span style="color: #b1b100;">table.concat</span><span style="color: #66cc66;">&#40;</span>ls,<span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #66cc66;">&#41;</span>..<span style="color: #ff0000;">'&lt;/ul&gt;'</span>
        <span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span>
        <span style="color: #66cc66;">&lt;</span>html<span style="color: #66cc66;">&gt;&lt;</span>head<span style="color: #66cc66;">&gt;&lt;/</span>head<span style="color: #66cc66;">&gt;&lt;</span>body<span style="color: #66cc66;">&gt;</span>
        Web Variables <span style="color: #66cc66;">&lt;</span>br<span style="color: #66cc66;">/&gt;</span>
        <span style="color: #66cc66;">%</span>s
        <span style="color: #66cc66;">&lt;/</span>body<span style="color: #66cc66;">&gt;&lt;/</span>html<span style="color: #66cc66;">&gt;</span>
        <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #b1b100;">format</span><span style="color: #66cc66;">&#40;</span>ls<span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">end</span>, <span style="color: #ff0000;">&quot;/&quot;</span>, <span style="color: #ff0000;">&quot;/index&quot;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>


<p>This is not a pretty script; it&#8217;s only virtue is that it is short. It is ugly for two basic reasons; first, generating HTML with regular string operations is awkward, and second, it does not separate the logical parts out neatly. The HTML problem will be discussed in the next section, but the next example separates the various operations into their own sections.</p>

<p>This also shows how Orbit can serve up static content as well, in this case anything inside the <strong>/images</strong> directory:</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">    <span style="color: #808080; font-style: italic;">-- basic2.lua</span>
    <span style="color: #b1b100;">require</span><span style="color: #ff0000;">&quot;orbit&quot;</span>
&nbsp;
    module<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;basic2&quot;</span>, package.seeall, orbit.new<span style="color: #66cc66;">&#41;</span>
&nbsp;
    <span style="color: #b1b100;">function</span> index<span style="color: #66cc66;">&#40;</span>web<span style="color: #66cc66;">&#41;</span>
      <span style="color: #b1b100;">return</span> render_index<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">collectgarbage</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;count&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">end</span>
&nbsp;
    basic2:dispatch_get<span style="color: #66cc66;">&#40;</span>index, <span style="color: #ff0000;">&quot;/&quot;</span>, <span style="color: #ff0000;">&quot;/index&quot;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #808080; font-style: italic;">-- any file in this directory will be served statically</span>
    basic2:dispatch_static <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;/images/.+&quot;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
    <span style="color: #b1b100;">local</span> template <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span>
    <span style="color: #66cc66;">&lt;</span>head<span style="color: #66cc66;">&gt;&lt;/</span>head<span style="color: #66cc66;">&gt;</span>
    <span style="color: #66cc66;">&lt;</span>html<span style="color: #66cc66;">&gt;</span>
    <span style="color: #66cc66;">%</span>s
    <span style="color: #66cc66;">&lt;/</span>html<span style="color: #66cc66;">&gt;</span>
    <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span>
&nbsp;
    <span style="color: #b1b100;">function</span> render_page<span style="color: #66cc66;">&#40;</span>contents<span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">return</span> template:<span style="color: #b1b100;">format</span><span style="color: #66cc66;">&#40;</span>contents<span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">end</span>
&nbsp;
    <span style="color: #b1b100;">function</span> render_index<span style="color: #66cc66;">&#40;</span>mem<span style="color: #66cc66;">&#41;</span>
       <span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span>
        <span style="color: #66cc66;">&lt;</span>img src<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;/images/lua.png&quot;</span><span style="color: #66cc66;">/&gt;</span>
        <span style="color: #66cc66;">&lt;</span>h2<span style="color: #66cc66;">&gt;</span>Memory used by Lua is <span style="color: #66cc66;">%</span>6.0f kB<span style="color: #66cc66;">&lt;</span>h2<span style="color: #66cc66;">&gt;</span>
       <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #b1b100;">format</span><span style="color: #66cc66;">&#40;</span>mem<span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">end</span></pre></div></div>


<p>Note that <strong>dispatch_static</strong> is passed a Lua <em>string pattern</em>, not a file mask; in this case, anything in the directory.</p>

<p>We are now presenting true dynamic content (the amount of memory managed by Lua) and made a first start to prettifying the result. It is convenient to only generate the &#8216;inner&#8217; HTML and use a template to generate the full page, so <strong>render_page</strong> has been factored out.</p>

<p>Orbit follows the Model-View-Controller (MVC) pattern that separates an application into three distinct parts. The Controller acts as the executive, the Model manages the data, and the View presents the data to the user; here the dispatch rules calls <strong>index</strong> which uses the renderer <strong>render_index</strong> to present the HTML form of the data. This pattern comes from a basic principle, <a href="http://en.wikipedia.org/wiki/Separation_of_concerns">Separation of Concerns</a>. You do not want to mix database access, application logic and visual presentation together because the result is not clear and may become unmaintainable quickly.</p>

<h2>HTML the Orbit Way</h2>

<p>The HTML problem is usually solved using templates, such as <a href="http://cosmo.luaforge.net/">Cosmo</a> as discussed in the last <a href="http://luanova.org/sputnik">article</a> on Sputnik. Orbit provides another option, which is that Lua code can generate HTML:</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">    <span style="color: #808080; font-style: italic;">-- html1.lua</span>
    <span style="color: #b1b100;">require</span><span style="color: #ff0000;">&quot;orbit&quot;</span>
&nbsp;
    <span style="color: #b1b100;">function</span> generate<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">return</span> html <span style="color: #66cc66;">&#123;</span>
            head<span style="color: #66cc66;">&#123;</span>title <span style="color: #ff0000;">&quot;HTML Example&quot;</span><span style="color: #66cc66;">&#125;</span>,
            body<span style="color: #66cc66;">&#123;</span>
                h2<span style="color: #66cc66;">&#123;</span><span style="color: #ff0000;">&quot;Here we go again!&quot;</span><span style="color: #66cc66;">&#125;</span>
            <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #b1b100;">end</span>
&nbsp;
    orbit.htmlify<span style="color: #66cc66;">&#40;</span>generate<span style="color: #66cc66;">&#41;</span>
&nbsp;
    <span style="color: #b1b100;">print</span><span style="color: #66cc66;">&#40;</span>generate<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
    <span style="color: #66cc66;">==&gt;</span>
    C:\basic<span style="color: #66cc66;">&gt;</span>lua html1.lua
    <span style="color: #66cc66;">&lt;</span>html <span style="color: #66cc66;">&gt;&lt;</span>head <span style="color: #66cc66;">&gt;&lt;</span>title <span style="color: #66cc66;">&gt;</span>HTML Example<span style="color: #66cc66;">&lt;/</span>title<span style="color: #66cc66;">&gt;&gt;&lt;/</span>head<span style="color: #66cc66;">&gt;&lt;</span>body <span style="color: #66cc66;">&gt;&lt;</span>h2 <span style="color: #66cc66;">&gt;</span>
    Here we go again<span style="color: #66cc66;">!&lt;/</span>h2<span style="color: #66cc66;">&gt;&lt;/</span>body<span style="color: #66cc66;">&gt;&lt;/</span>html<span style="color: #66cc66;">&gt;</span></pre></div></div>


<p>This certainly reads better if you are used to Lua, but the magic involved needs some explanation. You may imagine that Orbit has introduced a whole bunch of little functions into the module, but this is not how it is done.  Instead, <strong>orbit.htmlify</strong> modifies the environment of the function <strong>generate</strong> so that any undefined variable is assumed to be a HTML tag and becomes a suitable function. So you can generate anything, even with unknown tags. So if <strong>generate</strong> was:</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">    <span style="color: #b1b100;">function</span> generate<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">return</span> sensors <span style="color: #66cc66;">&#123;</span>
            sensor <span style="color: #66cc66;">&#123;</span>name<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;one&quot;</span><span style="color: #66cc66;">&#125;</span>,
            sensor <span style="color: #66cc66;">&#123;</span>name<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;two&quot;</span><span style="color: #66cc66;">&#125;</span>,
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #b1b100;">end</span></pre></div></div>


<p>Then the result would be this syntactically valid XML:</p>

<pre><code>&lt;sensors &gt;&lt;sensor name="one"&gt;&lt;/sensor&gt;
&lt;sensor name="two"&gt;&lt;/sensor&gt;&lt;/sensors&gt;
</code></pre>

<p>In presenting the resulting markup I&#8217;ve inserted a few line breaks for making reading a bit easier.  Naturally you might like to <a href="http://n2.nabble.com/Orbit-htmlify-beautification-tc3889818.html#a3889818">beautify</a> the output for debugging purposes; web browsers do not care. <a href="http://infohound.net/tidy/">HTML Tidy</a> is another option. which will also give you a host of other diagnostic checks.</p>

<p>One problem to watch out for is that a mistyped tag name will give you odd HTML, not a Lua error. And there are some HTML tags which are valid Lua globals, such as <strong>table</strong> &#8211; the solution is to say <strong>H(&#8216;table&#8217;)</strong>, etc.  But HTML can now be generated without making your eyes bleed, as with the ugly web variables script. This little snippet uses the fact that tag functions take table arguments:</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">    <span style="color: #b1b100;">function</span> generate <span style="color: #66cc66;">&#40;</span>web<span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">local</span> list <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>
        <span style="color: #b1b100;">for</span> name,value <span style="color: #b1b100;">in</span> <span style="color: #b1b100;">pairs</span><span style="color: #66cc66;">&#40;</span>web.GET<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">do</span>
            <span style="color: #b1b100;">table.insert</span><span style="color: #66cc66;">&#40;</span>list,li<span style="color: #66cc66;">&#40;</span>name..<span style="color: #ff0000;">&quot; = &quot;</span>..value<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">end</span>
        <span style="color: #b1b100;">return</span> ul<span style="color: #66cc66;">&#40;</span>list<span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">end</span></pre></div></div>


<p>Any technique can produce over-elaborate code, as this program which generates an HTML form formatted with a table:</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">    <span style="color: #b1b100;">require</span><span style="color: #ff0000;">&quot;orbit&quot;</span>
&nbsp;
    <span style="color: #b1b100;">function</span> wrap <span style="color: #66cc66;">&#40;</span>inner<span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">return</span> html<span style="color: #66cc66;">&#123;</span> head<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>, body<span style="color: #66cc66;">&#40;</span>inner<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#125;</span>
    <span style="color: #b1b100;">end</span>
&nbsp;
    <span style="color: #b1b100;">function</span> test <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">return</span> wrap<span style="color: #66cc66;">&#40;</span>form <span style="color: #66cc66;">&#40;</span>H<span style="color: #ff0000;">'table'</span> <span style="color: #66cc66;">&#123;</span>
            tr<span style="color: #66cc66;">&#123;</span>td<span style="color: #ff0000;">&quot;First name&quot;</span>,td<span style="color: #66cc66;">&#40;</span> input<span style="color: #66cc66;">&#123;</span><span style="color: #b1b100;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'text'</span>, name<span style="color: #66cc66;">=</span><span style="color: #ff0000;">'first'</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#125;</span>,
            tr<span style="color: #66cc66;">&#123;</span>td<span style="color: #ff0000;">&quot;Second name&quot;</span>,td<span style="color: #66cc66;">&#40;</span>input<span style="color: #66cc66;">&#123;</span><span style="color: #b1b100;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'text'</span>, name<span style="color: #66cc66;">=</span><span style="color: #ff0000;">'second'</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#125;</span>,
            tr<span style="color: #66cc66;">&#123;</span> td<span style="color: #66cc66;">&#40;</span>input<span style="color: #66cc66;">&#123;</span><span style="color: #b1b100;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'submit'</span>, value<span style="color: #66cc66;">=</span><span style="color: #ff0000;">'Submit!'</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>,
                td<span style="color: #66cc66;">&#40;</span>input<span style="color: #66cc66;">&#123;</span><span style="color: #b1b100;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'submit'</span>,value<span style="color: #66cc66;">=</span><span style="color: #ff0000;">'Cancel'</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#125;</span>,
        <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">end</span>
&nbsp;
&nbsp;
    orbit.htmlify<span style="color: #66cc66;">&#40;</span>wrap,test<span style="color: #66cc66;">&#41;</span>
&nbsp;
    <span style="color: #b1b100;">print</span><span style="color: #66cc66;">&#40;</span>test<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>


<p>Not quite eye-bleeding, but getting there!  The solution is to capture common patterns in a library. The <strong>util</strong> module in the <strong>basic</strong> folder makes for much more readable code:</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">    <span style="color: #b1b100;">function</span> show_form <span style="color: #66cc66;">&#40;</span>web<span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">return</span> wrap<span style="color: #66cc66;">&#40;</span>form <span style="color: #66cc66;">&#123;</span>
            htable <span style="color: #66cc66;">&#123;</span>
                <span style="color: #66cc66;">&#123;</span>text<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;First name&quot;</span>,<span style="color: #ff0000;">'first'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#125;</span>,
                <span style="color: #66cc66;">&#123;</span>text<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Second name&quot;</span>,<span style="color: #ff0000;">'second'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#125;</span>,
                <span style="color: #66cc66;">&#123;</span>submit <span style="color: #ff0000;">'Submit'</span>, submit <span style="color: #ff0000;">'Cancel'</span><span style="color: #66cc66;">&#125;</span>,
            <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">end</span></pre></div></div>


<h2>Databases the Orbit Way</h2>

<p>Orbit can create an Object-Relational Mapping (ORM) between the tables of a relational database and Lua tables. The restriction is that the SQL table is required to have a numeric key called <strong>id</strong>. In the following discussion, we must switch between two very different meanings of the word <strong>table</strong>, so I&#8217;ll use &#8216;array&#8217; to mean &#8216;an array-like Lua table&#8217; and &#8216;map&#8217; to mean &#8216;a hash-like Lua table&#8217;.</p>

<p>Consider the following SQL table definition found in the <strong>blog</strong> example database:</p>

<pre><code>CREATE TABLE blog_post
       ("id" INTEGER PRIMARY KEY NOT NULL,
       "title" VARCHAR(255) DEFAULT NULL,
       "body" TEXT DEFAULT NULL,
       "n_comments" INTEGER DEFAULT NULL,
       "published_at" DATETIME DEFAULT NULL);
</code></pre>

<p>This code uses LuaSQL&#8217;s sqlite3 driver to dump out a particular row of this table by defining a mapper:</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">    <span style="color: #808080; font-style: italic;">-- ORM1.lua</span>
    <span style="color: #b1b100;">require</span> <span style="color: #ff0000;">&quot;orbit&quot;</span>
    <span style="color: #b1b100;">require</span> <span style="color: #ff0000;">&quot;luasql.sqlite3&quot;</span>
    <span style="color: #b1b100;">local</span> env <span style="color: #66cc66;">=</span> luasql.sqlite3<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
    <span style="color: #b1b100;">function</span> dump <span style="color: #66cc66;">&#40;</span>t<span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">if</span> #t <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">0</span> <span style="color: #b1b100;">then</span>
            <span style="color: #b1b100;">for</span> _,row <span style="color: #b1b100;">in</span> <span style="color: #b1b100;">ipairs</span><span style="color: #66cc66;">&#40;</span>t<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">do</span>
                dump<span style="color: #66cc66;">&#40;</span>row<span style="color: #66cc66;">&#41;</span>
            <span style="color: #b1b100;">end</span>
        <span style="color: #b1b100;">else</span>
            <span style="color: #b1b100;">for</span> field,value <span style="color: #b1b100;">in</span> <span style="color: #b1b100;">pairs</span><span style="color: #66cc66;">&#40;</span>t<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">do</span>
                <span style="color: #b1b100;">if</span> <span style="color: #b1b100;">type</span><span style="color: #66cc66;">&#40;</span>value<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">==</span> <span style="color: #ff0000;">'string'</span> <span style="color: #b1b100;">then</span>
                    value <span style="color: #66cc66;">=</span> value:sub<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">60</span><span style="color: #66cc66;">&#41;</span>
                <span style="color: #b1b100;">end</span>
                <span style="color: #b1b100;">print</span><span style="color: #66cc66;">&#40;</span>field,<span style="color: #b1b100;">type</span><span style="color: #66cc66;">&#40;</span>value<span style="color: #66cc66;">&#41;</span>,value<span style="color: #66cc66;">&#41;</span>
            <span style="color: #b1b100;">end</span>
            <span style="color: #b1b100;">print</span> <span style="color: #ff0000;">'-----'</span>
        <span style="color: #b1b100;">end</span>
    <span style="color: #b1b100;">end</span>
&nbsp;
    mapper <span style="color: #66cc66;">=</span> orbit.model.new<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    mapper.conn <span style="color: #66cc66;">=</span> env:connect<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;../blog/blog.db&quot;</span><span style="color: #66cc66;">&#41;</span>
    mapper.driver <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;sqlite3&quot;</span>
    mapper.table_prefix <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'blog_'</span>
&nbsp;
    posts <span style="color: #66cc66;">=</span> mapper:new <span style="color: #ff0000;">'post'</span>  <span style="color: #808080; font-style: italic;">-- maps to blog_post table</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">-- print out the second post</span>
    second <span style="color: #66cc66;">=</span> posts:find<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
    dump<span style="color: #66cc66;">&#40;</span>second<span style="color: #66cc66;">&#41;</span>
&nbsp;
    <span style="color: #66cc66;">==&gt;</span>
    published_at	number	<span style="color: #cc66cc;">1096923480</span>
    title	<span style="color: #b1b100;">string</span>	The Care And Feeding Of Your Sleeping Bicycle
    id	number	<span style="color: #cc66cc;">2</span>
    n_comments	number	<span style="color: #cc66cc;">0</span>
    body	<span style="color: #b1b100;">string</span>
    Thunder, thunder, thundercats, Ho<span style="color: #66cc66;">!</span> Thundercats are on the m
    <span style="color: #808080; font-style: italic;">-----</span></pre></div></div>


<p><strong>posts:find(2)</strong> gets the database row with <strong>id</strong> equal to 2 and returns a map with corresponding fields and values.</p>

<p>SQL is a more strongly-typed language than Lua, so that both <strong>published_at</strong> and <strong>n_comments</strong> are mapped to the <strong>number</strong> type. However, the mapper does preserve this information in its field <strong>meta</strong>; for instance, <strong>mapper.meta.published_at.type</strong> is &#8216;datetime&#8217;.</p>

<p>Orbit&#8217;s ORM mappers simplify database access by bridging the gap between the underlying database and the language; the most involved code here is just to dump out Lua maps and arrays.</p>

<p>As always, the interactive prompt is the best way to explore the available mapper methods:</p>

<pre><code>C:\basic&gt;lua -lmapper
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
&gt; posts = mapper:new 'post'
&gt; dump(posts:find_first('n_comments &gt; 0'))
published_at    number  1097769720
title   string  'Star Wars' As Written By A Princess
id      number  4
n_comments      number  1
body    string
Ten years ago a crack commando unit was sent to prison by a
-----
</code></pre>

<p><strong>find_first</strong> also returns a row, but by an explicit condition; <strong>find_all</strong> is similar, but returns an array of all matching rows. Note that the condition is a SQL expression, not Lua; this is effectively a more friendly way to create an SQL query:</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">    dump<span style="color: #66cc66;">&#40;</span>posts:find_all<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;id = ? or id = ?&quot;</span>,
        <span style="color: #66cc66;">&#123;</span><span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">4</span>,order <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;published_at asc&quot;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>


<p>ORM mapping works particularly nicely with dynamic languages, but there are some downsides.  consider the case that the table <strong>blog_post</strong> had many thousands of rows; the default mapper will grab all the columns, including the potentially rather large <strong>body</strong>. So opportunities for optimizing queries are lost, if we were only interested in searching the title for a match. It&#8217;s possible to reorganize things so that <strong>blog_post</strong> only contained &#8216;meta&#8217; data about each post (plus perhaps a short summary) and a new table <strong>blog_text</strong> would contain the actual text of each post, indexable using the same id.</p>

<h2>Putting it Together</h2>

<p>The next example will bring together these three concerns: request pattern matching (Controller), ORM mapping to a database (Model) and Orbit htmlification (View). It uses the database from the blog example but presents it in summary form.  For each row, it puts out a nicely formatted summary.  The actual rows are selected by a user set of keywords.</p>

<p>All our HTML-generating functions begin with &#8216;render_&#8217;. <strong>orbit.htmlify</strong> can be called in another way, where you give the module first, and then specify a string pattern to match the functions in the module that you want to htmlify.</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">	orbit.htmlify<span style="color: #66cc66;">&#40;</span>blog1,<span style="color: #ff0000;">'render_.+'</span><span style="color: #66cc66;">&#41;</span></pre></div></div>


<p>Initially, the actual model-controller part of the application is simply this:</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">    <span style="color: #b1b100;">function</span> index<span style="color: #66cc66;">&#40;</span>web<span style="color: #66cc66;">&#41;</span>
      <span style="color: #b1b100;">return</span> render_index<span style="color: #66cc66;">&#40;</span>posts:find_all<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">end</span>
&nbsp;
    blog1:dispatch_get<span style="color: #66cc66;">&#40;</span>index, <span style="color: #ff0000;">&quot;/&quot;</span>, <span style="color: #ff0000;">&quot;/index&quot;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>


<p>And the view part:</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">    <span style="color: #b1b100;">local</span> <span style="color: #b1b100;">function</span> limit <span style="color: #66cc66;">&#40;</span>s,maxlen<span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">if</span> #s <span style="color: #66cc66;">&gt;</span> maxlen <span style="color: #b1b100;">then</span>
            <span style="color: #b1b100;">return</span> s:sub<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,maxlen<span style="color: #66cc66;">&#41;</span>..<span style="color: #ff0000;">'...'</span>
        <span style="color: #b1b100;">else</span>
            <span style="color: #b1b100;">return</span> s
        <span style="color: #b1b100;">end</span>
    <span style="color: #b1b100;">end</span>
&nbsp;
    <span style="color: #b1b100;">function</span> render_page<span style="color: #66cc66;">&#40;</span>contents<span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">return</span> html <span style="color: #66cc66;">&#123;</span>
            head <span style="color: #66cc66;">&#123;</span> title <span style="color: #ff0000;">'Blog Example'</span> <span style="color: #66cc66;">&#125;</span>,
            body <span style="color: #66cc66;">&#40;</span>contents<span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #b1b100;">end</span>
&nbsp;
    <span style="color: #b1b100;">function</span> render_post<span style="color: #66cc66;">&#40;</span>post<span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">return</span> div <span style="color: #66cc66;">&#123;</span>
            h3 <span style="color: #66cc66;">&#40;</span>post.id .. <span style="color: #ff0000;">' '</span> .. post.title<span style="color: #66cc66;">&#41;</span>,
            p <span style="color: #66cc66;">&#40;</span>limit<span style="color: #66cc66;">&#40;</span>post.body,<span style="color: #cc66cc;">128</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>,
            em<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">os.date</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'%a %b %d %H:%M %Y'</span>,post.published_at<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>,
            <span style="color: #ff0000;">' '</span>,post.n_comments,<span style="color: #ff0000;">' comments'</span>
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #b1b100;">end</span>
&nbsp;
    <span style="color: #b1b100;">function</span> render_index<span style="color: #66cc66;">&#40;</span>posts<span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">local</span> res <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>
        <span style="color: #b1b100;">for</span> i,post <span style="color: #b1b100;">in</span> <span style="color: #b1b100;">ipairs</span><span style="color: #66cc66;">&#40;</span>posts<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">do</span>
            res<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> render_post<span style="color: #66cc66;">&#40;</span>post<span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">end</span>
        <span style="color: #b1b100;">return</span> render_page<span style="color: #66cc66;">&#40;</span>res<span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">end</span></pre></div></div>


<p>The heart of the view is <strong>render_post</strong>, which formats each row from the model. You will only achieve date formatting happiness once you have learned the formating characters for <strong>os.date</strong>; these are the same as those used in the C <a href="http://msdn.microsoft.com/en-us/library/fe06s4ak%28VS.80%29.aspx">strftime</a> function.</p>

<p>All in all, not bad for a 55-line program. The next step is to add simple keyword searching, which takes an extra line:</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">    <span style="color: #b1b100;">function</span> index<span style="color: #66cc66;">&#40;</span>web<span style="color: #66cc66;">&#41;</span>
      <span style="color: #b1b100;">local</span> keyword <span style="color: #66cc66;">=</span> web.GET.keyword <span style="color: #b1b100;">or</span> <span style="color: #ff0000;">''</span>
      <span style="color: #b1b100;">if</span> keyword <span style="color: #66cc66;">==</span> <span style="color: #ff0000;">''</span> <span style="color: #b1b100;">then</span> <span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>
      <span style="color: #b1b100;">else</span>
        <span style="color: #b1b100;">return</span> render_index<span style="color: #66cc66;">&#40;</span>posts:find_all<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'title like ?'</span>,<span style="color: #66cc66;">&#123;</span><span style="color: #ff0000;">'%'</span>..keyword..<span style="color: #ff0000;">'%'</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #b1b100;">end</span>
    <span style="color: #b1b100;">end</span></pre></div></div>


<p>The SQL <strong>like</strong> operator works with patterns, so that finding a title containing the word &#8216;Chicken&#8217; would be &#8220;title like &#8216;%Chicken%&#8217;&#8221;, where &#8216;%&#8217; means &#8216;any sequence of characters&#8217;.  The keyword is passed as a GET request variable: enter the following URL to see the results of the search: <strong>http://localhost:8080/?keyword=Chicken</strong>.</p>

<p>It would be useful to have <em>some</em> user interface, especially when we start passing multiple keywords.  <strong>render_index</strong> becomes:</p>


<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">    <span style="color: #b1b100;">function</span> render_index<span style="color: #66cc66;">&#40;</span>posts<span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">local</span> res <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>
        append<span style="color: #66cc66;">&#40;</span>res,p <span style="color: #66cc66;">&#40;</span>form <span style="color: #66cc66;">&#123;</span>
            input <span style="color: #66cc66;">&#123;</span><span style="color: #b1b100;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'text'</span>,name<span style="color: #66cc66;">=</span><span style="color: #ff0000;">'keyword'</span><span style="color: #66cc66;">&#125;</span>,
            input <span style="color: #66cc66;">&#123;</span><span style="color: #b1b100;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'submit'</span>,value<span style="color: #66cc66;">=</span><span style="color: #ff0000;">'Submit'</span><span style="color: #66cc66;">&#125;</span>,
        <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        append<span style="color: #66cc66;">&#40;</span>res,hr<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        append<span style="color: #66cc66;">&#40;</span>res,h2<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Found %d posts'</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #b1b100;">format</span><span style="color: #66cc66;">&#40;</span>#posts<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">for</span> _,post <span style="color: #b1b100;">in</span> <span style="color: #b1b100;">ipairs</span><span style="color: #66cc66;">&#40;</span>posts<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">do</span>
            append<span style="color: #66cc66;">&#40;</span>res,render_post<span style="color: #66cc66;">&#40;</span>post<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">end</span>
        <span style="color: #b1b100;">return</span> render_page<span style="color: #66cc66;">&#40;</span>res<span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">end</span></pre></div></div>


<p>By default, HTML forms submit their values to the same URL as the page, so this works fine.</p>

<p>This introduction was intended to help you begin reading the Orbit samples, documentation and tutorials. In the next in this series, we will discuss building more complicated applications, caching generated pages, and Orbit Pages, which is a powerful Lua-powered template engine for generating HTML.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.luanova.org/orbit1-2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

