Skip to content

dannote/lua-template

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 

lua-template

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

Installation

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.

Syntax

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

<body><%= content%></body>

Loops

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

Conditional

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

Template inclusion

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

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

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.

About

The simplest Lua template engine

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages