Overview
LuaHtml is an interpreter that allows Lua code to be used in HTML and TeX documents through the use of scriplets. Lua code can be embedded into documents through the use of
<%
,
<%=
, and
%>
symbols. LuaHtml adds one fuction call to Lua's C API:
void luaHtml_call(lua_State *L, char *fileName)
.
Templating
LuaHtml templates documents using two possible pairs of symbols. The first pair is
<%=
and
%>
. Lua code between these two symbols will be evaluated, and the result
will be output.
Example
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<title></title>
</head>
<body>
<%= 2+2 %>
</body>
</html>
The result of this example will be:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<title></title>
</head>
<body>
4
</body>
</html>
The second pair of symbols is
<%
and
%>
. Lua code between these two symbols will be evaluated, and the result
will not be output.
Example
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<title></title>
</head>
<body>
<% if 2 + 2 == 5 then %>
Two plus two is five
<% else %>
Two plus two is not five
<% end %>
</body>
</html>
The result of this example will be:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<title></title>
</head>
<body>
Two plus two is not five
</body>
</html>
API
Both files in luahtml,
luahtml.h
and
luahtml.c
, should be included in your project.
LuaHtml uses a C API which consists of one function call. A Lua state must be setup and Lua libraries opened before using this function.
void luaHtml_call(lua_State *L, char *fileName)
L is a valid lua_State.
fileName is the file name of the LuaHtml document to be opened.
Typically before calling luaHtml_call, a call to
lua_open()
and subsequently
luaL_openlibs(L)
is all that is necessary to get started. Also before calling luaHtml_call, set any global variables used by your LuaHtml code.
The output of the LuaHtml execution is stored in a global Lua variable named
result
.
Simple Example
This example will execute template.lhtml
and output the result to stdout.
#include "luahtml.h"
int main(int argc, char **argv){
lua_State *L = lua_open();
luaL_openlibs(L);
luaHtml_call(L, "template.lhtml");
lua_getglobal(L, "result");
printf(lua_tostring(L, -1));
lua_pop(L, 1);
lua_close(L);
return 0;
}
Variables
Variables are easy to pass to LuaHtml using Lua's global variables. For example, to push a number (either an integer or floating point) use
lua_pushnumber
and
lua_setglobal
. Pushing arrays requires using
lua_createtable
and
lua_gettop
; for each value in the array, use
lua_pushnumber
(for a number) and
lua_rawseti
; finally set the array as a global value using
lua_setglobal
. See below for an example.
Example
#include "luahtml.h"
int main(int argc, char **argv){
int table; //Used in the creation of the array
int array[5] = {1,2,3,4,5};
double pi = 3.14;
lua_State *L = lua_open();
luaL_openlibs(L);
//Set pi
lua_pushnumber(L, pi);
lua_setglobal(L, "pi");
//Set the array
lua_createtable(L, 5, 0);
table = lua_gettop(L);
for(int i = 0; i < 5; i++){
lua_pushnumber(L, array[i]);
lua_rawseti(L, table, i+1);
}
lua_setglobal(L, "array");
luaHtml_call(L, "template.lhtml");
lua_getglobal(L, "result");
printf(lua_tostring(L, -1));
lua_pop(L, 1);
lua_close(L);
return 0;
}
Compiling
Compiling your project with LuaHtml is a matter of including
luahtml.c
with your project files and linking the lua library. Example in gcc:
gcc -llua main.c luahtml/luahtml.c