-- Lua Source
require 'tier.template'
local a = tier.Template.load("templates/example2.html")
print(a:render{planet="Earth",
answer=function() return "42" end,
name="example2.html",
replaced="--replaced by a block--"})
Template Example "example2.html"
{%
include 'name.html'
%}
Planetary Intranet
Hello there, visitor from planet Earth
{{
planet
}}
Your planet's name repeated three times:
{{
("<li>"..planet.."</li>"):rep(3)
}}
The number of letters in your planet's name: 5
{{
#planet
}}
A three by three grid of values:
(x=1, y=1) |
(x=1, y=2) |
(x=1, y=3) |
(x=2, y=1) |
(x=2, y=2) |
(x=2, y=3) |
(x=3, y=1) |
(x=3, y=2) |
(x=3, y=3) |
{%
for x=1, 3, 1 do
%}
<tr>
{%
for y=1, 3, 1 do
%}
<td>(x=
{{
x
}}, y=
{{
y
}}
)</td>
{%
end
%}
</tr>
{%
end
%}
After the nested for loop:
The value of x is now 3
{{
x
}}
The value of y is now 3
{{
y
}}
All template variables are global to all templates being rendered. i.e. this template, the parent template and the parent of that template, and any templates that were included anywhere by any of those templates.
Also, block names and argument names are in the same namespace; You can replace a block of a template the same way you set variables of a template. See lua source at top.
--replaced by a block--
{%
block replaced
%}
gibberish
{%
endblock
%}
The answer to the universe and everything is: 42
{{
answer()
}}
Time taken to generate this template:
real 0m0.020s
user 0m0.015s
sys 0m0.003s