Aspect a compiling (to Lua code and bytecode) a template engine for Lua and LuaJIT. Adapted to work with OpenResty and Tarantool. Lua itself is a simple language, but with many limitations and conventions.
Aspect makes it easy to work with Lua, letting you focus on the design of templates. Template syntax is very popular. This syntax is used in Twig, Jinja, Django, Liquid.
Yet another template engine? Yes. But more powerful than any other template engines. Just check out all the features.
Synopsis
<!DOCTYPE html>
<html>
<head>
{% block head %}
<title>{{ page.title }}</title>
{% endblock %}
</head>
<body>
{% block content %}
<ul id="navigation">
{% for item in navigation %}
<li><a href="{{ item.href }}">
{{- item.caption|escape -}}
</a></li>
{% endfor %}
</ul>
<h1>My Webpage</h1>
{{ page.body }}
{% endblock %}
<footer>{% include "footer.view" %}</footer>
</body>
</html>
local aspect = require("aspect.template").new(options)
local result, error
= aspect:eval("<div>Hello, {{ username }}</div>", vars)
$ aspect /path/to/data.json /path/to/template.tpl
Installation
Using LuaRocks
Installing Aspect using LuaRocks is simple:
luarocks install aspect
Without LuaRocks
Or add src/?.lua
to package.path
:
package.path = '/path/to/aspect/src/?.lua;' .. package.path
Documentation links
For template designers:
Development
- Convention
- Code of conduct
- Debugging
- Components
Features
- Well known: Aspect uses the most popular template syntax. Twig for PHP (maximum compatibility), Jinja for Python (minor differences), Liquid for Ruby (minor syntax differences).
- Fast: Aspect compiles templates down to plain optimized Lua code. Moreover, Lua code compiles into bytecode - the fastest representation of a template. Aspect will translate your template sources on first load into Lua bytecode for best runtime performance.
- Safe: Aspect always runs templates in the sandbox (empty environment) from where it is impossible to access the system packages. This allows Aspect to be used as a template language for applications where users may modify the template design.
- Flexible: Aspect is powered by a flexible parser and compiler. This allows the developer to define their own custom tags, filters, functions, tests and operators.
- Supports lua 5.1/5.2/5.3 and luajit 2.0/2.1 (including OpenResty).
- Convenient. Aspect makes it easy for users to work with templates. It has automatic type casting, automatic checking of a variable and its keys, it changes some annoying behavior of lua.
- CLI: Aspect has a console command for the template engine. Generate configs and other files using popular syntax.
- Cache. Aspect has a multi-level cache.
- No dependencies. No FFI. Pure Lua.
- Secure. Aspect has powerful automatic HTML escaping system for cross site scripting prevention.
- Template inheritance makes it possible to use the same or a similar layout for all templates.
- Aspect provides a convenient debugging process.
- Iterator supported and countable objects.
- Date is supported.
- Chain rendering (renders data chunk by chunk).