lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


How cgilua should have been in the first place. I have
something much the same.

On 11/24/05, Robert Raschke <rrlua@tombob.com> wrote:
> I run a little program to allow some templating using the cgilua
> stuff.  The function script() takes a filename and returns the cgi'ed
> contents as a string. You'll have to decide what to do about errors.
>
> Robby
>
> scripter.lua:
> --[[
>         This is a shim layer around the scripting technology of CGILua.
>
>         The idea is to use the CGILua pretty much as a black box,
>         knowing only about the includehtml() function, which does the
>         <% %> magic.
>
>         To get this to work we set ourselves up as a Server API (SAPI,
>         part of the CGI Lua setup), which assembles the result of the
>         script as a string.  The original way is to write out the
>         result to standard output, which is not useful for us.
>         Additionally we put errors into the log.
> ]]
>
> local SCRIPTER_RESULT = ""
>
> SAPI = {
>         Response = {
>                 -- Headers, nuthin'
>                 contenttype = function (s) return end,
>                 redirect = function (s) return end,
>                 header = function (h, v) return end,
>                 -- Contents
>                 write = function (s) SCRIPTER_RESULT = SCRIPTER_RESULT .. (s or "") end,
>                 errorlog = function (s) do_something_about_the_error(s) end,
>         },
>         Request = {
>                 -- We do not deal with Input POST data
>                 getpostdata = function (n) return nil end,
>                 -- Input general information
>                 servervariable = function (n) return os.getenv(n) or "UNSET" end,
>         },
> }
> require"cgilua"
>
> cgilua.seterrorhandler(function (msg) do_something_about_the_error(msg) end)
> cgilua.seterroroutput(function (msg) SAPI.Response.errorlog(msg) end)
>
> put = SAPI.Response.write
>
> function file_exists(file_name)
>         local file = io.open(file_name)
>         if file then
>                 file:close()
>         end
>         return file ~= nil
> end
>
> function script(name)
>         assert( file_exists(name), "Cannot read template '" .. name .. "': No such file.")
>         SCRIPTER_RESULT = ""
>         cgilua.includehtml(name)
>         return SCRIPTER_RESULT
> end
>
>