Forum Index   -   Topic Index
   Tools
     XOOPServer: PHP with LUA Scripts and Lua Pages
Register To Post

Threaded | Newest First Previous Topic | Next Topic | Bottom
Poster Thread
Mikhail
Posted on: 2009/3/26 5:14
Home away from home
Joined: 2005/12/30
From: Rio de Janeiro, Brasil
Posts: 444
XOOPServer: PHP with LUA Scripts and Lua Pages
Last year, I did a very small and singular version of xoopserver pack: with Lua pre-installed.

Download:

http://underpop.free.fr/x/xoopserver/xoopserver.zip

Contain:

- Apache 2.2.11
- Lua 5.1.2
- PHP 5.2.8
- mySQL 5.1.30
- PhpMiniAdmin


Only 4.63MB!!!


Note: the Lua module (mod_lua) has pulled into the future Apache 2.4 trunk stable release. This pack runs Lua via Lua-CGI and Kepler.


LUA Scripts & Lua Pages



Lua Scripts (ex: index.lua )

Lua Scripts are text files containing valid Lua code. This style of usage adopts a more "raw" form of web programming, where a program is responsible for the entire generation of the resulting page. Lua Scripts have a default .lua extension.

To generate a valid web document (HTML, XML, WML, CSS etc) the Lua Script must follow the expected HTTP order to produce its output, first sending the correct headers and then sending the actual document contents.

CGILua offers some functions to ease these tasks, such as cgilua.htmlheader to produce the header for a HTML document and cgilua.put to send the document contents (or part of it).

For example, a HTML document which displays the sentence "Hello World!" can be generated with the following Lua Script:

Code:

cgilua.htmlheader()
cgilua.put([[
<html>
<head>
  <title>Hello World</title>
</head>
<body>
  <strong>Hello World!</strong>
</body>
</html>]])

It should be noted that the above example generates a "fixed" page: even though the page is generated at execution time there is no "variable" information. That means that the very same document could be generated directly with a simple static HTML file. However, Lua Scripts become especially useful when the document contains information which is not known beforehand or changes according to passed parameters, and it is necessary to generate a "dynamic" page.

Another easy example can be shown, this time using a Lua control structure, variables, and the concatenation operator:

Code:

cgilua.htmlheader()  

if cgilua.QUERY.language == 'english' then
  greeting = 'Hello World!'
elseif cgilua.QUERY.language == 'portuguese' then
  greeting = 'Olá Mundo!'
else
  greeting = '[unknown language]'
end

cgilua.put('<html>')  
cgilua.put('<head>')
cgilua.put('  <title>'..greeting..'</title>')
cgilua.put('</head>')
cgilua.put('<body>')
cgilua.put('  <strong>'..greeting..'</strong>')
cgilua.put('</body>')
cgilua.put('</html>')


In the above example the use of cgilua.QUERY.language indicates that language was passed to the Lua Script as a CGILua parameter, coming from the URL used to activate it (via GET). If you were using a form, the parameter would be available in cgilua.POST.language. CGILua automatically decodes such QUERY and POST parameters so you can use them at will on your Lua Scripts and Lua Pages.

Lua Pages (ex: index.lp )


A Lua Page is a text template file which will be processed by CGILua before the HTTP server sends it to the client. CGILua does not process the text itself but look for some special markups that include Lua code into the file. After all those markups are processed and merged with the template file, the results are sent to the client.

Lua Pages have a default .lp extension. They are a simpler way to make a dynamic page because there is no need to send the HTTP headers. Usually Lua Pages are HTML pages so CGILua sends the HTML header automatically.

Since there are some restrictions on the uses of HTTP headers sometimes a Lua Script will have to be used instead of a Lua Page.

The fundamental Lua Page markups are:

<?lua chunk ?>
Processes and merges the Lua chunk execution results where the markup is located in the template. The alternative form <% chunk %> can also be used.
<?lua= expression ?>

Processes and merges the Lua expression evaluation where the markup is located in the template. The alternative form <%= expression %> can also be used.

Note that the ending mark could not appear inside a Lua chunk or Lua expression even inside quotes. The Lua Pages pre-processor just makes global substitutions on the template, searching for a matching pair of markups and generating the corresponding Lua code to achieve the same result as the equivalent Lua Script.

The second example on the previous section could be written using a Lua Page like:

Code:

<html>
<?lua
if cgilua.QUERY.language == 'english' then
  greeting = 'Hello World!'
elseif cgilua.QUERY.language == 'portuguese' then
  greeting = 'Olá Mundo!'
else
  greeting = '[unknown language]'
end
?>
<head>
  <title><%= greeting %></title>
</head>
<body>
  <strong><%= greeting %></strong>
</body>
</html>

HTML tags and Lua Page tags can be freely intermixed. However, as on other template languages, it's considered a best practice to not use explicit Lua logic on templates. The recommended aproach is to use only function calls that returns content chunks, so in this example, assuming that function getGreeting was definied in file functions.lua as follows:

Code:

function getGreeting()
  local greeting
  if cgilua.QUERY.language == 'english' then
    greeting = 'Hello World!'
  elseif cgilua.QUERY.language == 'portuguese' then
    greeting = 'Olá Mundo!'
  else
    greeting = '[unknown language]'
  end
  return greeting
end

the Lua Page could be rewriten as:

<?lua
assert (loadfile"functions.lua")()
?>
<html>
<head>
  <title><%= getGreeting() %></title>
</head>
<body>
  <strong><%= getGreeting() %></strong>
</body>
</html>

Another interesting feature of Lua Pages is the intermixing of Lua and HTML. It is very usual to have a list of values in a table, iterate over the list and show the items on the page.

A Lua Script could do that using a loop like:

Code:

cgilua.put("<ul>")
for i, item in ipairs(list) do
    cgilua.put("<li>"..item.."</li>")
end
cgilua.put("</ul>")

The equivalent loop in Lua Page would be:

Code:

<ul>
    <% for i, item in ipairs(list) do %>
    <li><%= item %></li>
    <% end %>
</ul>


----------------
XOOPS Brasil

minahito
Posted on: 2009/8/24 14:23
Home away from home
Joined: 2005/10/6
From: Japan
Posts: 398
Re: XOOPServer: PHP with LUA Scripts and Lua Pages
I have used some embed script languages including LUA in video game development. But, I did not know that we can use LUA as server side script! That's very interesting!


----------------
minahito
Homepage (poor English )

Threaded | Newest First Previous Topic | Next Topic | Top

Register To Post
 
Activity | Contributions



Who's Online
21 user(s) are online (4 user(s) are browsing Forums)

Members: 0
Guests: 21

more...

Welcome | News | Overview | Documentation | Forum | Tutorialstop
Brasilian | French | German | Greek | Japanese | Korean | Russian | T-Chinese
Powered by XOOPS Cube © 2001-2011 The XOOPS Cube Project