a barebones Lua templating library

Lyre is a very basic, Jinja-like templating language that directly uses Lua's self-modifying code feature to get by. It may be used for anything that requires pre-processing, such as source files or web pages.

Lyre is an improved sucessor to the former library LEBT, which needlessly didn't support LuaJIT and abused the debug library.

local Lyre = require"lyre"

local t = Lyre.compile("Hello, {{ name }}!", "Creative Name 1")

print(Lyre.render(t, { name = "World" }))

Hello, World!

Additionally, Lyre supports writing Lua statements within templates:

local t = Lyre.compile("x / {{ n }} is {% if n == 0 then %}undefined{% else %}defined{% end %}.", "Creative Name 2")

print(Lyre.render(t, { n = 0 }))
print(Lyre.render(t, { n = 1 }))

x / 0 is undefined.
x / 1 is defined.

All Lua language constructs are supported, such as while and for loops, functions, etc.

{% function article() %}
	Title: {{ title }}
	Author: {{ author }}
	{% content() %}
{% end %}

{% title = "Hamlet"; author = "Thomas Nashe" %}
{% function content() %}
	It was a dark and stormy night.
{% end %}

{% article() %}

	Title: Hamlet
	Author: Thomas Nashe
	It was a dark and stormy night.

Notice the whitespace gets copied also.

Lyre.render supports a third boolean argument, which strongly binds the template environment to whatever table you pass. This means any changes to the environment by the template will be reflected in your table. This is useful for sharing environments between templates, allowing them to include other templates.


Lyre is licensed under the BSD Zero Clause license, which is functionally equivalent to dedicating to the public domain. Terms are detailed in the source file. I would prefer no attribution. If I could ban attribution, I would.


Also available on LuaRocks as lyretemplates.