Login or register Large RSS Icon

LuaScripts and LuaPages

Kepler 1.1 provides two mechanisms for sending a page back to the browser: LuaPage and LuaScript.

LuaPage is an HTML file with some Lua code in it, e.g.

<html>
  <head>...</head>
  <body>...</body>
    CGI Parameter X: <i><%= cgi.x %></i>

    A loop:

    <ul>
     <% for i=1,5 do %>
      <li>
       <%= i %>
      </li>
     <% end %>
    </ul>

</html>

This file should be saved with the .lp extension, e.g. showx.lp.

A LuaScript is just a Lua program that writes HTML through cgi.put:

cgilua.contentheader ("text", "html")

cgilua.put([[<html>
  <head>...</head>
  <body>...</body>]])

cgilua.put(string.format("CGI Parameter X: <i>%s</i>",
                         cgi.x)

cgilua.put("<ul>")

for i=1,5 do 
   cgilua.put("<li>")
   cgilua.put(i)
   cgilua.put("</li>")
end

cgilua.put("</ul>")
cgilua.put("</html>")

This should be saved with a .lua extension, e.g. showx.lua.

LuaPages can be nested inside other LuaPages using cgilua.lp.include(), e.g.

showx.lp:

<html>
  <head>...</head>
  <body>
    <% cgilua.lp.include ("justx.lp") %>
  </body>
</html>

justx.lp:

    CGI Parameter X: <i><%= cgi.x %></i>

    A loop:

    <ul>
     <% for i=1,5 do %>
      <li>
       <%= i %>
      </li>
     <% end %>
    </ul>

They can also be included in LuaScripts, e.g. we can rewrite showx.lua as:

cgilua.contentheader ("text", "html")

cgilua.put([[<html>
  <head>...</head>
  <body>...</body>]])

cgilua.lp.include ("justx.lp")

cgilua.put("</html>")

More practically, you might want to use a LuaScript to dispatch requests to multiple lua pages:

if cgi.action == "rss" then
   cgilua.contentheader ("application", "rss+xml")
else 
   cgilua.contentheader ("text", "html")
end

function figure_out_the_title(...) ... end
function figure_out_the_lua_page(...) ... end

new_title = figure_out_the_title(cgi)
lua_page = figure_out_the_lua_page(cgi)

cgi.title = params.new_title

if not cgi.action == "rss" then
   cgilua.lp.include("header.lp")
else
   cgilua.lp.include("rss-header.lp")
end

cgilua.lp.include(lua_page)

if not cgi.action == "rss" then
   cgilua.lp.include("footer.lp")
else
   cgilua.lp.include("rss-footer.lp")
end
Powered by Sputnik | XHTML 1.1