You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Peter Colberg d34bfae53a Update release notes 3 years ago
..
CHANGES.html Update release notes 3 years ago
CHANGES.mdwn Update release notes 3 years ago
INSTALL.html Update release notes 3 years ago
INSTALL.mdwn Update release notes 3 years ago
Makefile Examples: Include templates 4 years ago
README.html Update copyright notice 3 years ago
README.mdwn Add documentation in HTML format 4 years ago
contents.mdwn Update copyright notice 3 years ago
examples.html Update copyright notice 3 years ago
examples.mdwn Examples: Include templates 4 years ago
index.html Update release notes 3 years ago
index.mdwn Update release notes 3 years ago
lua-templet.png Migrate documentation to Pandoc and Markdown 5 years ago
lua-templet.ps Migrate documentation to Pandoc and Markdown 5 years ago
pandoc.css Update copyright notice 3 years ago
pandoc.html.in Add documentation in HTML format 4 years ago
reference.html Update copyright notice 3 years ago
reference.mdwn Add documentation in HTML format 4 years ago

README.mdwn

---
title: User's Guide
---

Templates may contain Lua statements and Lua expressions.

A statement is prefixed with `|`, and extends to the end of the line:

|for i = 1, 10 do
| if i > 5 then
Hello, World!
| end
|end

Note that a statement may be preceded by whitespace only.

An expression is enclosed with `${}`, and may appear anywhere on a line:

~~~
${hello}, ${world}!
~~~

A template is parsed using `templet.loadstring`:

~~~ {.lua}
local templet = require("templet")

local temp = templet.loadstring([[
|for i = 1, 10 do
| if i > 5 then
Hello, World!
| end
|end
]])
~~~

(Alternatively, a template is loaded from a file using `templet.loadfile`.)

This creates a template object, which is then rendered:

~~~ {.lua}
local result = temp()
io.write(result)
~~~

~~~
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
~~~

By default, a template is rendered in the global environment (`_G`),
i.e. variables in the template reference global variables. To substitute
variables with values, the environment may be set by passing a table as
the first argument:

~~~ {.lua}
local temp = templet.loadstring([[${hello}, ${world}!]])
print(temp({hello = "Hallo", world = "Welt"}))
print(temp({hello = "你好", world = "世界"}))
~~~

~~~
Hallo, Welt!
你好, 世界!
~~~

A template is rendered and returned as a string by default.
Alternatively, an output function may be specified as the second
argument, which is repeatedly called for the set of chunks that yield
the rendered template.

~~~ {.lua}
local temp = templet.loadstring([[
|for i = 1, 3 do
double x${i} = ${math.sqrt(i)};
|end
]])
temp({math = math}, io.write)
~~~

~~~
double x1 = 1;
double x2 = 1.4142135623731;
double x3 = 1.7320508075689;
~~~

Suppose we wish to substitute floating-point values in a template using
hexadecimal floating-point constants, which are an exact representation
of binary values. We pass an output function that checks whether a chunk
is a floating-point number, and converts to its hexadecimal
representation:

~~~ {.lua}
temp({math = math}, function(chunk)
if type(chunk) == "number" and math.floor(chunk) ~= chunk then
chunk = string.format("%a", chunk)
end
return io.write(chunk)
end)
~~~

~~~
double x1 = 1;
double x2 = 0x1.6a09e667f3bcdp+0;
double x3 = 0x1.bb67ae8584caap+0;
~~~

Note that hexadecimal constants require Lua 5.2 or LuaJIT.