lua-users home
lua-l archive

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


Hi,

I thought I would share this little subvertive use of cgilua.

I found myself recently needing to write a small program that would
allow simple parameter substitution in a text file with values found
in a DB. I started out by using the $ substitution routine from PIL
(p.169) and global variables fed by an SQL select statement.

That worked a treat until I came across the problem of needing to have
some conditional expansion, and immediately on its heels, expansion
involving multiple results from the SQL select statement (i.e.,
looping).

It turns out I can get all of that by using the following little ditty
to do the expansion for me:

SAPI = {
	Response = {
		-- Headers
		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) io.stderr:write(s or "nil") 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) io.stderr:write(msg or "nil") end)
cgilua.seterroroutput(function (msg) SAPI.Response.errorlog(msg) end)

function script(name)
	SCRIPTER_RESULT = ""
	cgilua.includehtml(name)
	return SCRIPTER_RESULT
end


I can now "just" call the script() function and write my template file
in lovely cgi manner (with loops, conditionals, and the whole power of
lua at my fingertips).  And I can "sell" it much more easily to the
people who may end up having to write some of these templates: "It's
just like VBScript, just a bit simpler ..." ;-)

It only took a little bit of experimentation to get the error handling
to not constantly chatter html at me.

Robby