The simplest Lua template engine
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
LICENSE.txt final changes Nov 16, 2014 Enabled HTML escaping by default Mar 28, 2016
template-0.2-3.rockspec Better nil handling Apr 12, 2016
template.lua Better nil handling Apr 12, 2016
templatec minify Nov 16, 2014


The simplest Lua HTML template engine in just a few lines of code


luarocks install template

Compiling templates

Templates can be compiled by either running

templatec template.tpl -o template.lua

nor by passing a string to template.compile.


In short, Lua expressions must be included between percent signs and Lua statements must be placed beetween question marks.

Variables and expressions

<a href="page-<%page + 2%>"><%next%></a>

Variables and expressions without HTML escaping



<? for i = 1, 3 do ?>
  <li>item #<%i%></li>
<? end ?> 


<? if 1 > 2 then ?>
<? else ?>
That's right!
<? end ?>

Template inclusion

Templates are compiled to a general Lua file and hence can be loaded by require statement:

  <script><% require "scripts" %></script>
  <style><% require "styles" %></style>

Evaluating templates

template.print takes three arguments: template function, a table with variables passed to the template and optionally a callback function, which handles string printing (print is used by default).

Compressing templates

template.compile has an optional minify argument and templatec has -m option.